Differences between Android display intent and implicit intent

Source: Internet
Author: User

Differences between Android explicit and implicit intent
Intent is very important in android Application Development. After understanding the role and use of intent, it will be of great help to development. If you do not understand the intention, you will feel something is missing in the future.
Purpose:
1. Activate Components

2. Carrying data
3. Intention matching (apply to implicit intent)

The basic design concept of android is to encourage the reduction of coupling between components. Therefore, android provides Intent and activates other components with Intent. Intent provides a general messaging system that allows you to transmit Intent between your application and other applications to execute and generate events. Using Intent, You can activate three core components of the android app: Activity, service, and broadcast receiver. Intent represents an idea to be executed, something to do.
Intentions can be divided into explicit intent and implicit intent.

Explicit Intent: Call Intent. setComponent () or Intent. the setClass () method explicitly specifies that the Intent of the component name is an explicit Intent, and the explicit Intent explicitly specifies that the Intent should be passed to that component.
For example, Intent intent = new Intent ();

Intent. setAction (Activity1.this, Activity2.class );
StartActivity (intent );
The explicit intent is very simple and will not be repeated here.

Implicit Intent: the Intent of the component name is not explicitly specified as an implicit Intent. The android system will find the most suitable component to process the intent based on the action, category, and data (URI and data type) set in the implicit intent.

For example, call Dialing: Intent intent = new Intent ();

Intent. setAction ("android. intent. action. CALL ");

Intent. setData ("Uri. parse (" tel: ") + mobile"); // mobile indicates the phone number (a number)
StartActivity (intent );

How can I find a component by using an implicit intent to activate the component?

Explanation: for example, when the phone dial is defined, the Intent filter is defined as follows:




Five tips for product managers: Product thinking product design interaction experience












1. First, there is an action name defined internally by the dial. You can also define your own developed applications.
2. Category. No category is set in the Code. Cause: When we call the startActivity method to pass intent to the operating system, the startActivity method adds the intent to a category, which is android. intent. category. DEFAULT.

3. Data: data contains the Uri and Data Type represented by the data. In this example, there is no data type, only Uri, uri is composed of three parts: scheme, host name, and path. For Uri matching, you only need to match a given part. In this example, you only need to match scheme.

In this case, the code can match the intent filter. Therefore, it will call the Activity of the filter to implement the dialing function.

So where are explicit and implicit intentions used?

Explicit intent is generally used inside the application. Because the component name is known inside the application, You can activate it directly.

When one application needs to activate another Activity (the source code is not visible), only implicit intent can be used to create an intent based on the intent filter configured by the Activity, make the values of parameters in the intent match the filters, so that Activity in other applications can be activated. Therefore, the implicit intent is used between the application and the application. (If you want an application developed by yourself to allow access from other applications to an Activity, define an intent filter for the Activity. Then, other applications can set intent objects based on the parameters in the intent filter and pass them to the operating system. Then, the operating system can find the filters that match the intent, find the filter to activate the Activity with the intent)
The following is an example:

In FirstActivity, click the button to jump to SecondActivity (with implicit intent ):
In AndroidManifest


















Android: host = "www.itcast.cn"
Android: path = "/person"/>




Code: onCreate part

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 );
}
});
}

Specifically, the action name can be set at will, and the category name can also be set at will, but android. intent. category. DEFAULT is automatically generated internally when startActivity is called.

There can be multiple actions and categories in the intent filter, but the intent can only define one action, so as long as the defined action and any matching in the intent filter can be successful. The same is true for category. You only need to match one of them, so you do not have to match all of them. However, if the defined category cannot be found in the intent filter, an error is reported, that is, the action and intent defined in the intent must exist in the filter.

NOTE: If intent. setDataAndType (Uri. parse ("itcast: // www.itcast.cn/person"), "image/gif"); Separate write, namely: intent. setData (), intent. when setType () (something in parentheses is omitted), an error is returned even if all matches. Cause: In the setType () document, there is a sentence: This method automatically clears any data that was previusly set by setData (Uri). that is, when setType is set, content in setData is automatically cleared. Therefore, the solution is to write intent. setDataAndType (Uri. parse ("itcast: // www.itcast.cn/person"), "image/gif.

Related Article

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.