May 21, 2021 Android
Android intent is an abstract description of what to do. It can start an activity by startActivity, broadcastIntent to send a broadcast to any broadcast receiver component that is interested in it, startService, or bindService (ServiceConnection, int) to communicate with the background service.
The intent itself (an Intent object) is a passive data structure that holds an abstract description of the action to be performed.
For example, you have an activity that requires you to open the mail client and send messages through an Android device. F or this purpose, your activity needs to send a user with the appropriate selector ACTION_SEND to the Android Intent Handler. The specified selector gives the appropriate interface for the user to decide how to send his mail data.
Intent email = new Intent(Intent.ACTION_SEND, Uri.parse("mailto:")); email.putExtra(Intent.EXTRA_EMAIL, recipients); email.putExtra(Intent.EXTRA_SUBJECT, subject.getText().toString()); email.putExtra(Intent.EXTRA_TEXT, body.getText().toString()); startActivity(Intent.createChooser(email, "Choose an email client from..."));
The syntax above calls the startActivity method to turn on mail activity, and the code runs like this:
For example, you have an activity that requires a URL to be opened on an Android device through a browser. F or this purpose, your activity sends ACTION_WEB_SEARCH intent to the Android Intent processor to open a given URL in the browser. T he intent processor resolves a series of activities and selects an activity that best suits your intent, in this case a Web browser activity. The intent processor passes your web address to the web browser and opens the web browser activity.
String q = "http://www.uprogrammer.cn"; Intent intent = new Intent(Intent.ACTION_WEB_SEARCH ); intent.putExtra(SearchManager.QUERY, q); startActivity(intent);
The example above looks for "www.uprogrammer.cn" on the Android search engine and gives the results of keywords at an event.
For each component -activity, service, broadcast receiver has a separate mechanism to pass the intent.
Serial number | Method and description |
---|---|
1 | Context.startActivity(): Intent to pass to this method to start a new activity or to have an existing activity do something new. |
2 | Context.startService(): Intended to pass to the method, initialize a service, or new information to a persistent service. |
3 | Context.sendBroadcast(): The intent is passed to the method, and the information is passed to all broadcast receivers interested in it. |
An intent object is a package of information that a component receives with the same intent as an Android system.
Intent objects include components such as the following, depending on what you want to communicate or perform.
This is a necessary part of the intent object and is represented as a string. I n the intended broadcast, the action will be reported as soon as it occurs. T he action will largely determine how the rest of the intent is organized. T he Intent class defines a series of action constants that correspond to different intents. Here is a list of standard actions for Android intent.
Actions in the intent object can be set by the setAction() method and read by the getAction() method.
Add data specifications to the intent filter. T his specification can be just a data type, such as a meta-type property, a URI, or include both a data type and a URI. URIs are specified by properties in different parts.
These properties that specify the URL format are optional, but are also independent of each other -
The setData() method can only specify data in URIs, setType() can specify data only in metatypes, and setDataAndType() can specify both URIs and metatypes. The URI is read by getData() and the type is read by getType().
Here are some examples of actions/data groups -
Serial number | Actions/data sets and descriptions |
---|---|
1 | ACTION_VIEW content://contacts/people/1: Displays information about users with IDs of 1. |
2 | ACTION_DIAL content://contacts/people/1: Displays the phone dialer and populates user 1 data. |
3 | ACTION_VIEW tel:123: Displays the phone dialer and populates the given number. |
4 | ACTION_DIAL tel:123: Displays the phone dialer and populates the given number. |
5 | ACTION_EDIT content://contacts/people/1: Edit user information with an ID of 1. |
6 | ACTION_VIEW content://contacts/people/: Displays a list of users for viewing. |
7 | ACTION_SET_WALLPAPER: Show select wallpaper settings. |
8 | ACTION_SYNC: Sync data, the default value is: android.intent.action.SYNC |
9 | ACTION_SYSTEM_TUTORIAL: Open a tutorial defined by the platform (default tutorial or start tutorial) |
10 | ACTION_TIMEZONE_CHANGED: Notice when the area is changed |
11 | ACTION_UNINSTALL_PACKAGE: Run the default offloader |
A category is an optional part of an intent, a string that contains additional information about the intent that the type component needs to process. T he addCategory() method adds categories to the intent object, the removeCategory() method removes the previously added categories, and getCategory() gets all the categories that are set into the intent object. Here is a list of standard categories of Android intent.
You can check the intent filters in the following sections to see how we use categories to select the right activity with the corresponding intent.
This is additional information that is passed to the key-to-value description of the component that needs to handle the intent. R ead by putExtras() method settings, getExtras() method. Here is a list of Android Intent Standard Additional Data.
These tags are an optional part of the intent, explaining how Android can start activities, how to handle them after startup, and so on.
Serial number | Tags and instructions |
---|---|
1 | FLAG_ACTIVITY_CLEAR_TASK: If set in intent and passed through Context.startActivity, this tag will cause all existing tasks associated with the activity to be emptied before the activity starts. T he activity becomes the root of an empty task, and all old activities are ended. The tag can be used FLAG_ACTIVITY_NEW_TASK the tag. |
2 | FLAG_ACTIVITY_CLEAR_TOP: If you set the tag, the activity will be started in the task that is currently running. This does not start a new activity instance, and all activities above it are closed, this intent is passed as a new intent to the existing (currently at the top) activity. |
3 | FLAG_ACTIVITY_NEW_TASK: This tag is typically used to make activities work in a "starter"-style behavior: to provide users with data that can be run independently, and to start a full child-independent activity. |
The component name object is an optional domain that represents an activity, service, or broadcast sink class. I f set, the intent object is passed to an instance that implements the designed class, otherwise Android uses additional information from other intents to locate an appropriate target. Component names are set by setComponent(), setClass() or setClassName(), and obtained through getComponent().
Android supports two types of intent.
Explicit intent is used to connect the inner world of the application, assuming that you need to connect one activity to another, we can connect the first activity to the second activity by displaying the intent, as shown in the following image by clicking a button.
These intentions specify the target component by name and are typically used for internal application information - such as an activity to start a subordinate activity or to start a brother activity. For example:
// 通过指定类名的显式意图 Intent i = new Intent(FirstActivity.this, SecondAcitivity.class); // 启动目标活动 startActivity(i);
These intents are not named for the target, and the domain of the component name is empty. I mplicit intent is often used to activate components of other applications. For example:
Intent read1=new Intent(); read1.setAction(android.content.Intent.ACTION_VIEW); read1.setData(ContactsContract.Contacts.CONTENT_URI); startActivity(read1);
The code above will give the following results:
The target component receives intent and can use the getExtras() method to obtain additional data sent by the source component. For example:
// 在代码中的合适位置获取包对象 Bundle extras = getIntent().getExtras(); // 通过键解压数据 String value1 = extras.getString("Key1"); String value2 = extras.getString("Key2");
The following example demonstrates the ability to use Android intent to launch various Android built-in applications.
Steps | Describe |
---|---|
1 | Create an Android app with the Android Studio IDE and name it Inent filter, with the package name cn.uprogrammer.intentfilter. When you create a project, make sure that the target SDK and compile with the latest version of the Android SDK use advanced APIs. |
2 | Modify the src/cn.uprogrammer.intentfilter/MainActivity.java file and add code to define two listeners to correspond to the two buttons "Start Browser" and "Start Phone" |
3 | Modify the res/layout/activity_main.xml layout file and add 3 buttons to the linear layout. |
4 | Launch the Android simulator to run the app and verify the results of the changes made by the app. |
Here's what the src/cn.uprogrammer.intentfilter/MainActivity .java file contains:
package cn.uprogrammer.intentfilter; import android.content.Intent; import android.net.Uri; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; public class MainActivity extends ActionBarActivity { Button b1,b2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b1=(Button)findViewById(R.id.button); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://www.uprogrammer.cn")); startActivity(i); } }); b2=(Button)findViewById(R.id.button2); b2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("tel:9510300000")); startActivity(i); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
Here's what the res/layout/activity_main.xml file is about:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="意图实例" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:textSize="30dp" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="www.uprogrammer.cn" android:textColor="#ff87ff09" android:textSize="30dp" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageButton" android:src="@drawable/ic_launcher" android:layout_below="@+id/textView2" android:layout_centerHorizontal="true" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText" android:layout_below="@+id/imageButton" android:layout_alignRight="@+id/imageButton" android:layout_alignEnd="@+id/imageButton" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="启动浏览器" android:id="@+id/button" android:layout_alignTop="@+id/editText" android:layout_alignRight="@+id/textView1" android:layout_alignEnd="@+id/textView1" android:layout_alignLeft="@+id/imageButton" android:layout_alignStart="@+id/imageButton" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="启动电话" android:id="@+id/button2" android:layout_below="@+id/button" android:layout_alignLeft="@+id/button" android:layout_alignStart="@+id/button" android:layout_alignRight="@+id/textView2" android:layout_alignEnd="@+id/textView2" /> </RelativeLayout>
Below is the content of res/values/strings/xml, which defines two new constants.
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Intent filter</string> <string name="action_settings">Settings</string> </resources>
Here's what the default Android .xml content:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.uprogrammer.intentfilter" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="22" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/Base.Theme.AppCompat" > <activity android:name="cn.uprogrammer.intentfilter.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Let's run the Intent filter application that we just modified. L et me assume that you created the AVD when you installed the environment. O pen the active file in your project and click the icon in the toolbar to run the app in Android Studio. A ndroid Studio installs the app on the AVD and launches it. If all goes well, it will appear on the emulator window as follows:
Now click the "Start Browser" button, which will launch a browser based on the configuration and display the http://www.uprogrammer.cn as follows:
Similarly, you can click the "Start Phone" button to open the phone interface, which will allow you to dial a phone number that you have already given.
You've seen how to use intent to invoke additional activities. T he Android operating system uses filters to specify a range of activities, services, and broadcast receiver processing intents, relying on actions, categories, and data patterns specified by the intent. In the manifest file, the corresponding actions, categories, and data types are listed in the activity, service, and broadcast receivers using the element slt;intent-filter.
The following example shows part of the .xml Manifest file, specifying an activity cn.uprogrammer.intentfilter.CustomActivity that can be called by setting the action, category, and data:
<activity android:name=".CustomActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <action android:name="com.example.MyApplication.LAUNCH" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="http" /> </intent-filter> </activity>
When an activity is defined by the filter above, other activities can call it in the following way. Use android.intent.action.VIEW, use cn.uprogrammer.intentfilter.LAUNCH action, and provide android.intent.category.DEFAULT.
The element specifies the type of data expected by the activity to be called. In the example above, the data expected by the custom activity begins with http:// "
In this case, through the filter, the intent will be passed to multiple activities or services, and the user will be asked which component to start. If the target component is not found, an exception occurs.
Before calling the activity, there is a series of Android check tests:
The following example is some of the modifications to the above instance. Here we'll see how Android resolves conflicts if an intent to invoke two activities defined, how filters are used to invoke custom activities, and exceptions occur if there are no appropriate activities defined for the intent.
Steps | Description |
---|---|
1 | Create an Android app using the Android Studio IDE and name it Inent filter, which is called cn.uprogrammer.intentfilter. When you create a project, make sure that the target SDK and compile with the latest version of the Android SDK use advanced APIs. |
2 | Modify the src/cn.uprogrammer.intentfilter/MainActivity.java file and add code to define three listeners to correspond to the three buttons defined in the layout file. |
3 | Add src/cn.uprogrammer.intentfilter/CustomActivity.java file to contain an activity that can be called with different intents. |
4 | Modify the res/layout/activity_main.xml file to add three buttons to the linear layout. |
5 | Add a res/lauout/custom_view.xml layout file, and add a simple to display the data passed through intent. |
6 | Modify the Android Manifest .xml file and add a rule that defines intent to invoke a custom activity. |
7 | Launch the Android simulator to run the application and verify the results of the changes made by the application. |
Here's what src/cn.uprogrammer.intentfilter/MainActivity .java content:
package cn.uprogrammer.intentfilter; import android.content.Intent; import android.net.Uri; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; public class MainActivity extends ActionBarActivity { Button b1,b2,b3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b1=(Button)findViewById(R.id.button); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("http://www.uprogrammer.cn")); startActivity(i); } }); b2=(Button)findViewById(R.id.button2); b2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent("cn.uprogrammer.intentfilter.LAUNCH",Uri.parse("http://www.uprogrammer.cn")); startActivity(i); } }); b3=(Button)findViewById(R.id.button3); b3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent("cn.uprogrammer.intentfilter.LAUNCH",Uri.parse("https://www.uprogrammer.cn")); startActivity(i); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
Here's what src/cn.uprogrammer.intentfilter/CustomActivity .java content:
package cn.uprogrammer.intentfilter; import android.app.Activity; import android.net.Uri; import android.os.Bundle; import android.widget.TextView; public class CustomActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.custom_view); TextView label = (TextView) findViewById(R.id.show_data); Uri url = getIntent().getData(); label.setText(url.toString()); } }
Here's what res/layout/activity_main.xml do:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="意图实例" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:textSize="30dp" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="www.uprogrammer.cn" android:textColor="#ff87ff09" android:textSize="30dp" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageButton" android:src="@drawable/ic_launcher" android:layout_below="@+id/textView2" android:layout_centerHorizontal="true" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText" android:layout_below="@+id/imageButton" android:layout_alignRight="@+id/imageButton" android:layout_alignEnd="@+id/imageButton" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="通过View动作启动浏览器" android:id="@+id/button" android:layout_alignTop="@+id/editText" android:layout_alignRight="@+id/textView1" android:layout_alignEnd="@+id/textView1" android:layout_alignLeft="@+id/imageButton" android:layout_alignStart="@+id/imageButton" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="通过Launch动作启动浏览器" android:id="@+id/button2" android:layout_below="@+id/button" android:layout_alignLeft="@+id/button" android:layout_alignStart="@+id/button" android:layout_alignRight="@+id/textView2" android:layout_alignEnd="@+id/textView2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="异常情况" android:id="@+id/button3" android:layout_below="@+id/button2" android:layout_alignLeft="@+id/button2" android:layout_alignStart="@+id/button2" android:layout_alignRight="@+id/textView2" android:layout_alignEnd="@+id/textView2" /> </RelativeLayout>
Here's what the res/layout/custom_view.xml file is about:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/show_data" android:layout_width="fill_parent" android:layout_height="400dp"/> </LinearLayout>
Here's what the res/values/strings .xml file:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">My Application</string> <string name="action_settings">Settings</string> </resources>
Here's what Android .xml files are:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.uprogrammer.intentfilter" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="22" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/Base.Theme.AppCompat" > <activity android:name="cn.uprogrammer.intentfilter.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="cn.uprogrammer.intentfilter.CustomActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <action android:name="cn.uprogrammer.intentfilter.LAUNCH" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="http" /> </intent-filter> </activity> </application> </manifest>
Let's run the Intent filter application that we just modified. L et me assume that you created the AVD when you installed the environment. O pen the active file in your project and click the icon in the toolbar to run the app in Android Studio. A ndroid Studio installs the app on the AVD and launches it. If all goes well, it will appear on the emulator window as follows:
Click the first button to "Start browser with View action". Here we define our custom activities to include "android.intent.action.VIEW", and the Android system has defined the default activity to correspond to the VIEW action to launch the web browser, so Android displays the following options to select the activity you want to start:
If you choose a browser, Android will launch a web browser and open www.uprogrammer.cn website. If you select the IntentDemo option, Android will launch CustomActivity, which does nothing but capture and display the passed data in TextView.
Now, by going back to the button and clicking the "Start browser with Launch action" button, the Android app filter here selects the defined activity and simply launches the custom activity.
Use the return button again to go back and click the "Exception Conditions" button, where Android tries to find a valid filter given by intent, but does not find a defined valid activity. B ecause we use https instead of http data, and given the correct action, Android produces an exception at once. As follows: