-
- Intent Introduction
- The composition of the intent object
- Explicit intent and implicit intent
- An explicit intent
- Implicit intent
Intent Introduction
Intent is used to jump between components, it connects two components while the program is running, intent is divided into explicit intent and implicit intent, explicit intent is commonly used in the application between the components of the jump, the implicit intent often for different applications between the components of the jump. With intent, you can submit a request to Android, and Android will choose the appropriate component to respond to the request based on intent's wishes. This article does not describe the transfer of data between different applications through intent, and introduces explicit intent and implicit intent
The composition of the intent object
Intent Object
- Target components
- Actions (Action)
- Categories (category)
- (data)
- Additional data (extras)
- Flags (Flags)
Explicit intent and implicit intent are introduced here
Explicit intent and implicit Intent1-explicit intent
Specifies the intent of the component property (called SetComponent (componentname) or SetClass (Context, Class). Notifies the app to launch the corresponding component by specifying a specific component class. By specifying the target component name and looking for the target component, it is generally applied to the case of the calling program and the target component in the same application;
Explicit intent are called directly by the specified component, and intent can be implemented explicitly intent in the following way
setComponent(ComponentName name)The component class is a component encapsulation class provided by the Android system and requires a package name, class name, context object
setClassName(String packageName,String classNameInThatPackage)Provide the class name and Target component class name in this package – must be a full class name, package name + class name
setClassName(Context context,String classNameInThatContext)The context object and the Target component class name – must be a full class name, a package name + class name, a Conponent object is created in Setclassname, and the Component object, if passed in as a context object, The package name is obtained through Context.getpackagename ().
setClass(Context context,Class classObjectInThatContext)The context object and the Target component class type, new Intent(MainActivity.this,OtherActivity.class); just like the arguments passed by F
The way we often use explicit intent is created by the intent constructor, as follows:
Intent intent=new Intent(MainActivity.this,OtherActivity.class);
To see the source code, you know, the thing inside the constructor is to create a new ComponentName object, such as:
The constructor new componentname an object and assigns it to the mcomponent (followed by mcomponent to specify the ComponentName object intent created). Then by startActivity(intent) starting the target component, when we create the intent object without passing any arguments, we need to manually set the ComponentName object, which is the same as above by passing the context object and the class object, the code is as follows:
Intent intent=new Intent(MainActivity.this,OtherActivity.class);com=new ComponentName("com.sujz.intent_test","com.sujz.intent_test.OtherActivity"));intent.setComponent(com)startActivity(intent);
The ComponentName object passes two parameters, the first parameter passes the package name of the current Activity object, and the second parameter passes the full class name of the target object (package name + class name)
setComponentNameThe Mcomponent method is actually assigned to the value in. So by Setcomponentname method the same way as above
setClassNamesetComponentSimilarly, setClassName an ComponentName object is constructed internally and then assigned to the Mcomponent object, and other methods are not mentioned.
2. Implicit intent
The
Implicit Intent object is a Intent that does not specify mcomponent, that is, in the process of building Intent, the Intent intent=new Intent (Mainactivity.this, Otheractivity.class) , setcomponent (componentname name) ' Setclassname (String packagename,string Classnameinthatpackage) ' SetClass (Context context,class classobjectinthatcontext) and so on to specify the ComponentName object, Note is no, no, no, important thing to say 3 times.
When a Mcomponent object is not specified, sufficient information is required to match the target component, including Action,category,data . These 3 messages require the intent of the current component and the intent of the target component to be configured, as long as two components of 3 information matching, the target component will be launched, of course, it may also be matched to multiple target components, it is necessary for users to choose, such as the chestnut below, is a intent match to multiple target components. This approach not only launches the relevant components in the application, but also launches related components for other applications, for example, a chestnut, when multiple browsers are installed on our phone, such as UC, Baidu, etc. when we click on a link in an app, Does it jump out of a selection box to let you choose which browser to use to open the link (if there is no pop-up selection box, there are two possible: 1. Your phone has only one browser.) 2. You have previously checked the default use of the browser to open the link). All right, the chestnuts are done. , this approach is set by implicit intent. So how do we set it up to start another component? Keep looking. The
First we set the target component's intent, just say that we need to match, the current component and the target component are set intent, the target component intent is set in Androidmanifest.xml. Here we have two applications, one is intent_test and the other is host, now booting from host to Intent_test, that is, the target component Inent_test MainActivity1 is the target component, We need to configure the following in Androidmanifest:
<activityandroid:name="Com.sujz.intent_test. MainActivity1 "android:label=" @string/title_activity_main_activity1 " > <intent-filter> <action android:name="Com.sujz.aa" /> <category android:name="Android.intent.category.DEFAULT" /> </intent-filter></activity>
In the androidmanifest of the target component, you need to configure the Intent-filter node under the Activity node, which requires at least one action and one category,action and category can have multiple You only need to match to one of them, there is no data configured here, that is, data can not be configured. The main <category android:name="android.intent.category.DEFAULT" /> reason why this must be configured is because this item is configured by default in the component that sends the intent request.
The code in host is as follows:
Intent intent=new Intent(); intent.setAction("com.sujz.aa"); startActivity(intent);
Some people may ask, there is no category configured here, why can match to. This is because Android sets the category in StartActivity (intent). Internally intent.addCategory("android.intent.category.DEFAULT"); , the action name of the intent-filter that needs to pass the target component in Setaction is called.
You can also create your own category, taking the above as an example, as shown below, in Intent_test:
<activityandroid:name="Com.sujz.intent_test. MainActivity1 "android:label=" @string/title_activity_main_activity1 " > <intent-filter> <action android:name="Com.sujz.aa" /> <category android:name="Android.intent.category.DEFAULT" /> <category android:name="android.intent.category.bb" /> </intent-filter></activity>
In Host:
Intent intent=new Intent(); intent.setAction("com.sujz.aa"); intent.addCategory("android.intent.category.bb"); startActivity(intent);
It is not necessary intent.addCategory("android.intent.category.DEFAULT"); because startActivity(intent); it has already been added.
Okay, the implicit intent is done.
Android Intent use