Learn more about Intent in Android

  • 2020-11-18 06:27:32
  • OfStack

Intent provides a generic messaging system that allows you to execute actions and generate events in your application using Intent. Using Intent you can activate three types of core components of Android applications: active Activity, Service Service, and broadcast receiver Broadcast.

Intent is divided into hermit intention and display intention.

Display intent: Intent calling intent.setComponent (), intent.setClassName () or intent.setClass () methods that explicitly specify the component name is display intent, indicating which component is to be activated.

Hermit intent: There is no explicit formulation of the component's name; Here is an example of a hermit's intentions

You should first add the corresponding ES24en-ES25en to the manifest file


<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
      android:label="@string/app_name"
      android:name=".IntentActivity" >
      <intent-filter >
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>

    <activity android:name=".OtherActivity" android:label="@string/windowtitle">
      <intent-filter >
        <action android:name="cn.itcast.zhangxx"/>
        <action android:name="cn.itcast.laoli"/>
        <category android:name="cn.itcast.category.java"/> 
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="cn.itcast.category.php"/>
        <data android:scheme="itcast" android:host="www.itcast.cn" android:path="/liming"/>
        <data android:mimeType="image/*"/>
      </intent-filter>      
    </activity>
  </application>

In MainActivity. java


public void openActivity(View v){
    /**
     * ( If there are no data parameters ) As long as Intent In the Action and Category in Intent-Filter , can match it, otherwise the match fails 
     */
    Intent intent = new Intent();// Implicit intent activation Activity
    intent.setAction("cn.itcast.zhangxx");
    intent.addCategory("cn.itcast.category.java");  

    // intent.setData(Uri.parse("itcast://www.itcast.cn/liming"));
    // intent.setType("image/jpeg");
    // According to the Android the API You can know 
    //This method automatically clears any data that was previously set (for example by setData(Uri)). 
    // In other words, if it's set setData Method in Settings setType Method, then setType method clers off setData methods 
    // The solution is as follows: 

    intent.setDataAndType(Uri.parse("itcast://www.itcast.cn/liming"), "image/jpeg");
    startActivity(intent);// Method inside is Intent added android.intent.category.DEFAULT category 
  }

Here are the matching rules for hermits intentions:

Intent Filter (Intent filter) is actually used to match implicit Intent. When an intent object is matched by an intent filter, only three aspects are referenced: action, data (URI and data type), and category.

Action test (Action test)

An intent object can specify only one action name, while a filter may enumerate more than one action name. If the intent object or filter does not specify any action, the result will be as follows:

The & # 8226; If the filter does not specify any action, all intents will be blocked, so all intents will fail to test. No intention can pass through this filter. The & # 8226; On the other hand, an intent object that does not specify an action automatically passes this test as long as the filter contains at least one action

Category test (Category test)

For an intent to pass the category match test, the category in the intent object must match the category in the filter. This filter can enumerate additional categories, but it cannot omit any categories in this intent.

In principle, an intent object without a category should always pass the match test, regardless of what is in the filter. This is true most of the time. But there is one exception, Android pass all to startActivity () implicit intention as they contain at least one category: "android. intent. category. DEFAULT" (CATEGORY_DEFAULT constants). Want to receive the implicit intention activity, therefore, must include "in their intent filter android. intent. category. DEFAULT". (take "android. intent. action. MAIN" and "android. intent. category. LAUNCHER" set filters are exceptions)

Data testing (Data test)

When URI in 1 intent object is used to compare URI in 1 filter, the components of URI are compared. For example, if the filter specifies only one scheme, all URIs of that scheme can match the filter; If the filter specifies 1 scheme, hostname but no route section, all URIs with the same scheme and hostname can match the filter regardless of their route; If the filter specifies 1 scheme, hostname, and path, only URIs with the same scheme, hostname, and path will match the filter. Of course, the path specification in a filter can contain wildcards, so only partial matches are required.

The data test compares both the intent object and the URI and data type specified in the filter.

The rules are as follows:

a. 1 Intent object that contains neither URI nor data type will pass the test only if the filter also does not specify any URIs and data type.

An ES104en. 1 intent object that contains URI but has no data type will only pass if its URI matches an URI in a filter that also has no data type specified. This usually happens with URIs like mailto: and tel: : they do not cite actual data.

c. 1 intent object that contains a data type but does not contain URI will pass the test only if the filter enumerates the same data type and does not specify an URI.

d. 1 An intent object that contains both URI and a data type (or data type can be inferred from URI) can pass the test if its type matches the type listed in the filter. It also passes the test if its URI matches one of the URI in this filter or if it has a content content: or file file: URI and the filter does not specify an URI. In other words, a component is assumed to support content: and file: data if its filter enumerates only one data type.

Above is for Android Intent detailed introduction, especially suitable for beginners to learn, hope to help you learn.


Related articles: