Intent in Android is simple and practical (jump page)

Posted by matjaz on Fri, 22 Nov 2019 16:55:41 +0100

1. Intent can jump to another page and carry corresponding data or not. Here are several common methods of intent

2. Including using Intent to explicitly start another Activity and implicitly start another Activity

① to explicitly start another Activity with Intent, you need to configure the Activity to jump in AndroidManifest.xml

btnSecond.setOnClickListener(new OnClickListener() {
	@Override
	public void onClick(View paramView) {
	Intent myintent = new Intent(MainActivity.this,SecondActivity.class);
		startActivity(myintent); // Start target Activity
	}
});
<action android:name="com.scott.intent.action.TARGET2"/> 

② implicit opening of another Activity is mainly to access external devices and external resources. If it is only a simple implicit opening of a specific Activity, it is not very common

The following is to open the corresponding web page and external device, the complete code is as follows:

MainActivity.java Code:

package com.cws.intentdemo;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button btnSecond = (Button) findViewById(R.id.btnSecond);// Open the second interface
		Button btnSecond2 = (Button) findViewById(R.id.btnSecond2);// Open the second interface
		Button btnSecond3 = (Button) findViewById(R.id.btnSecond3);// Open the second interface
		
		Button btn_web = (Button) findViewById(R.id.btnweb);// Open web page
		Button btn_call = (Button) findViewById(R.id.btncall);// dial
		Button btn_sms = (Button) findViewById(R.id.btnsms);// Sending SMS
		Button btn_image = (Button) findViewById(R.id.btnimage);// Play multimedia
		Button btn_map = (Button) findViewById(R.id.btnmap);// Open map
		Button btn_pic = (Button) findViewById(R.id.btnpic);// Open photos
		Button btn_set = (Button) findViewById(R.id.btnSet);
		// Display to open the second interface
		btnSecond.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View paramView) {
				Intent myintent = new Intent(MainActivity.this,
						SecondActivity.class);
				startActivity(myintent); // Start target Activity
			}
		});
		
		// Open the second interface implicitly
		btnSecond2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View paramView) {
				try {
					Intent myintent = new Intent("com.scott.intent.action.TARGET");
					startActivity(myintent); // Start target Activity
				} catch (Exception e) {
					Toast.makeText(MainActivity.this, "This interface does not exist!",
							Toast.LENGTH_LONG).show();
				}
			}
		});
		btnSecond3.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View paramView) {
				try {
					Intent myintent = new Intent("com.scott.intent.action.TARGET2");
					myintent.setData(Uri.parse("scott://com.scott.intent.data:7788/target"));
					startActivity(myintent); // Start target Activity
				} catch (Exception e) {
					Toast.makeText(MainActivity.this, "This interface does not exist!",
							Toast.LENGTH_LONG).show();
				}
			}
		});

		// 1. Open the web page
		btn_web.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View paramView) {
				Uri uri = Uri.parse("http://www.baidu.com "); / / define uri
				Intent myintent = new Intent();
				myintent.setAction(Intent.ACTION_VIEW); // Specify Action
				myintent.setData(uri); // Setting data
				startActivity(myintent);  
			}
		});

		// 2, dial up
		btn_call.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View paramView) {
				try {
					Uri uri = Uri.parse("tel:10086");
					// Intent.action.call dials the specified phone number, intent.action.dial
					Intent intent = new Intent(Intent.ACTION_DIAL, uri);
					startActivity(intent);
				} catch (Exception e) {
					Toast.makeText(MainActivity.this, "This interface does not exist!",
							Toast.LENGTH_LONG).show();
				}
			}
		});

		//3. Send SMS
		btn_sms.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Uri uri = Uri.parse("smsto:15200006059");// Specify the mobile number to receive SMS
				Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
				intent.putExtra("sms_body", "testtest");// Specify SMS content
				startActivity(intent);
			}
		});

		//4. Play multimedia
		btn_image.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_PICK);
				intent.setType("image/*");
				if (intent.resolveActivity(getPackageManager()) != null) {
					startActivity(intent);
				} else {
					Toast.makeText(MainActivity.this, "The picture does not exist!",
							Toast.LENGTH_LONG).show();
				}

			}
		});

		//5. Open the map
		btn_map.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View paramView) {
				try {
					Uri uri = Uri.parse("geo:39.895874,116.321");
					Intent intent = new Intent(Intent.ACTION_VIEW, uri);
					startActivity(intent);
				} catch (Exception e) {
					Toast.makeText(MainActivity.this, "This interface does not exist!",
							Toast.LENGTH_LONG).show();
				}
			}
		});
		btn_pic.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View paramView) {
				try {

					Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
					startActivity(intent);
					 
				} catch (Exception e) {
					Toast.makeText(MainActivity.this, "This interface does not exist!",
							Toast.LENGTH_LONG).show();
				}
			}
		});
		
		btn_set.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View paramView) {
				try {

					Intent intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
					startActivity(intent);
					 
				} catch (Exception e) {
					Toast.makeText(MainActivity.this, "This interface does not exist!",
							Toast.LENGTH_LONG).show();
				}
			}
		}); 
	}
}

SecondActivity.java

package com.cws.intentdemo;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.second);
	}
}

ThirdActivity.java

package com.cws.intentdemo;
import android.app.Activity;
import android.os.Bundle;
public class ThirdActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.third); 
	}
}

 

Topics: Android Java xml Mobile