The difference between Android display intent and implicit intent

Source: Internet
Author: User

The intention is very important in Android application development, understand the intention of the role and use, the development will be a great help. If you do not understand the intent, future development applications will feel missing something.
Effect of the intention:
1. Activating the component

2. Carry data
3. Matching of intentions (applied to implicit intent)

Android's basic design philosophy is to encourage the reduction of coupling between components, so Android provides intent (intent) to activate other components with intent. Intent provides a common messaging system that allows you to pass intent between your application and other applications to execute and generate events. Use intent to activate the three core components of the Android app: active, service, and broadcast receivers. Intent represents an idea to be executed, something to do.
The intent can be divided into explicit intent and implicit intent.

Explicit intent: Calling the Intent.setcomponent () or Intent.setclass () method explicitly specifies that the component name is intent to explicit intent, explicitly intending to explicitly specify that the intent should be passed to that component.
such as: Intent Intent = new Intent ();

Intent.setaction (Activity1.this,activity2.class);
StartActivity (Intent);
The explicit intent is simple and is not to be mentioned here.

Implicit intent: No explicitly specified component name is intent as implicit intent. The Android system finds the most appropriate component to handle this intent based on the action (action), category, data (URI, and data type) set in the implicit intent.

If calling phone dialing: Intent Intent = new Intent ();

Intent.setaction ("Android.intent.action.CALL");

Intent.setdata ("Uri.parse (" Tel: ") + mobile")//mobile for telephone number (number)
StartActivity (Intent);

How do I find a component if I use an implicit intent to activate the component?

Explanation: For example, when the phone Dialer is defined, the intent filter (Intent-filter) is defined as




Product managers must learn five ways to do product thinking product design interactive experience






<Intent-filter>
<action android:name= "Android.intent.action.CALL"/>
<category android:name "Android.intent.category.DEFAULT"/>
<data android:scheme= "Tel"/>
</Intent-filter>

1, first there is an action name, <action android:name= "Android.intent.action.CALL"/> is defined by the internal dialer, their own development of the application can also be defined by themselves.
2, category, in the code does not set the category, Reason: When we call the StartActivity method to intent intent to the operating system, the StartActivity method of the internal will be intent this intention to add a category, This category is Android.intent.category.DEFAULT.

3, data, data contains two aspects, the data represents the URI and the type of data, this example does not have the type of data, only Uri,uri by scheme, host name, path three parts, for the URI match, as long as a given part of the match is OK, In this case, just match the scheme.

At this point, the code can be matched with the intent filter, so it invokes the activity of the filter, thereby implementing the phone dialing function.

So, where are the explicit intentions and implicit intentions used in each occasion?

Explicit intent is generally used internally in the application, because the name of the component is already known within the application and is activated directly.

When an app wants to activate activity in another app (no source code), it can only use implicit intent to create an intent based on the intent filter configured by the activity so that the values of each parameter in the intent match the filter so that activity in the other app can be activated. Therefore, implicit intent is used between the application and the application. (If you're developing an app that wants some activity to be accessible to other apps, define an intent filter for the activity, then other apps can set the intent object based on the parameters in the intent filter and then pass it to the operating system, The operating system will be able to find a filter that matches this intent, and when the filter is found, it can activate the activity of the intent.
Here's an example:

Click the button in Firstactivity to jump to secondactivity (with implicit intent):
In the Androidmanifest











<activity android:name= ". Secondactivity "android:label=" @string/app_name ">
<intent-filter>
<action android:name= "Cn.itcast.xiao.li"/>
<action android:name= "Cn.itcast.xiao.zhang"/>

<category android:name= "Android.intent.category.DEFAULT"/>
<category android:name= "Cn.itcast.category.li"/>
<data android:scheme= "Itcast"
Android:host= "www.itcast.cn"
Android:path= "/person"/>

<data android:mimetype= "Image/gif"/>
</intent-filter>

Code: OnCreate Section

public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);


Button button = (button) This.findviewbyid (R.id.button);
Button.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
Intent Intent = new Intent ();
Intent.setaction ("Cn.itcast.xiao.li");
Intent.setdataandtype (Uri.parse ("Itcast://www.itcast.cn/person"),











"Image/gif");

StartActivity (Intent);
}
});
}

The name of the action is arbitrarily set, and the category name can be arbitrarily set, but Android.intent.category.DEFAULT is automatically generated internally when calling StartActivity.

There can be multiple actions and categories in the intent filter, but the intent is to define only one action, so any match between the defined action (action) and the intent filter can be successful. Category is also, as long as the match one of the lines, do not have to match all. However, if the defined category is not found in the intent filter, an error is noted, meaning that the action and intent defined in the intent must be in the filter.

Note: If Intent.setdataandtype (Uri.parse ("Itcast://www.itcast.cn/person"), "Image/gif"), separate writing, namely: Intent.setdata (), Intent.settype () (something in parentheses), even if the match is also an error. Reason: In the Settype () document There is a sentence, this method automatically clears any data, is previously set by SetData (Uri). In other words, when the Settype is set Automatically clears the contents of the SetData. So, the solution: Write Intent.setdataandtype (Uri.parse ("Itcast://www.itcast.cn/person"), "image/gif");

Differences between the explicit and implicit intentions of Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.