Android Intent usage

Source: Internet
Author: User

Android Intent usage

Intent introduction Intent object composition explicit Intent and implicit Intent explicit Intent

Intent Introduction

Intent is used to jump between components. It connects two components when the program is running. intent is divided into explicit intent and implicit intent. explicit intent is usually used to jump between components of the application, implicit intent is usually used to redirect components between different applications. With Intent, you can submit a request to Android. Android will select an appropriate component to respond to the request based on Intent's wishes. This article does not describe how to transmit data between different applications through intent.

Intent object Composition

Intent object

Target component Components Action (Action) category data (data) additional data (extras) flag (Flags)

The explicit Intent and implicit Intent are described here.

Explicit Intent and implicit Intent1. explicit Intent

Specifies the Intent of the component Attribute (call setComponent (ComponentName) or setClass (Context, Class ). Notify the application to start the corresponding component by specifying a specific component class. By specifying the name of the target component, you can find the target component, which is generally used when the caller and the target component are in the same application;
Explicit Intent is called directly by specifying components. Intent can implement explicit Intent through the following methods:

setComponent(ComponentName name)The Component class is a Component encapsulation class provided by the Android system. The package name, class name, and context object must be provided. setClassName(String packageName,String classNameInThatPackage)Provide the class name and the class name of the target component in this package-it 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 the full class name, package name + class name. A Conponent object will be created in setClassName, and if the Component object is a context object, by using context. getPackageName () to get the package name setClass(Context context,Class classObjectInThatContext)Type of the Context object and target component class, and new Intent(MainActivity.this,OtherActivity.class);The passed parameters are the same.

We often use the explicit Intent method to create it through the Intent constructor, as shown below:
Intent intent=new Intent(MainActivity.this,OtherActivity.class);
You can see from the source code that what the constructor does is to create a new ComponentName object, such:

The constructor creates a ComponentName object and assigns it to the mComponent (mComponent is used later to specify the ComponentName object created by intent ). Then passstartActivity(intent)To start the target component. When we do not pass any parameters to create an Intent object, we need to manually set the ComponentName object, which is the same as the previous one by passing the context object and Class object. The Code is as follows:

Intent intent=new Intent(MainActivity.this,OtherActivity.class);ComponentName 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 is the package name of the current Activity object, and the second parameter is the full class name of the target object (package name + class name)
setComponentNameThe method actually assigns a value to mComponent. So the above method is the same through setComponentName
setClassNameAndsetComponentSimilarly,setClassNameInternally, A ComponentName object is constructed and assigned to the mComponent object. Other methods are not described in detail.

2. Implicit Intent

The implicit Intent object is an Intent without mComponent specified. That is to say, during Intent buildingIntent intent=new Intent(MainActivity.this,OtherActivity.class);,setComponent(ComponentName name)``setClassName(String packageName,String classNameInThatPackage)``setClass(Context context,Class classObjectInThatContext)And other methods to specify the ComponentName object. Note that NO, important things are said three times.
When no mComponent object is specified, sufficient information is required to match the target component. The information includesAction, Category, Data. The three pieces of information need to be configured for both the Intent and the Intent of the current component. The target component will be started only when the three pieces of information of the two components match, of course, multiple target components may also be matched at the same time. In this case, you need to select the target components by yourself. The following example shows that an Intent matches multiple target components. In this way, you can not only start the relevant components of this application, but also the components of other applications. For example, when multiple browsers are installed on our mobile phones, such as UC, baidu, etc. When we click a link in an application, will a selection box pop up asking you to select which browser to open the Link (if no selection box is displayed, there are two possibilities: 1. your mobile phone has only one browser. 2. You have selected the default browser to open the link ). All right, the chestnuts are finished. This method is set by implicit Intent. How can we set it to start another component? Continue.
First, we first set the Intent of the target component. When talking about matching, we need to set Intent for both the current component and the target component, and set the Intent of the target component in AndroidManifest. xml. Here we have two applications: Intent_test and Host. Now we start from Host to Intent_test. That is to say, MainActivity1 in Inent_test is the target component, we need to configure the following in AndroidManifest:

      
                      
         
   
  

In AndroidManifest of the target component, intent-filter nodes need to be configured under the Activity node. At least one Action and one Category must be configured for this node. There can be multiple actions and Category, you only need to match one of them. Data is not configured here, that is, Data is not configured. Configuration is required here. The reason is that 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);

Someone may ask why category is not configured here. This is because android sets category in startActivity (intent. Internal callintent.addCategory("android.intent.category.DEFAULT");In setAction, the Intent-filter action name of the target component needs to be passed.
You can also create your own category, as shown in the following example in intent_test:

      
                      
             
          
    
   
  

In Host:

        Intent intent=new Intent();        intent.setAction("com.sujz.aa");        intent.addCategory("android.intent.category.bb");        startActivity(intent);

Nointent.addCategory("android.intent.category.DEFAULT");Because instartActivity(intent);Already added.

Okay, the implicit Intent is finished.

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.