Android study note 33: intent introduction and usage of intent in Activity

Source: Internet
Author: User

This blog post mainly introduces the concepts of intent and the usage of intent in activity.

 

1. Intent implementation process

In Android, intent can be used not only for ApplicationsProgramInteraction between activities and services in an application.

Intent describes an operation in an application. The description includes the action and the data involved in the action. The intent mechanism in Android uses this description to find the corresponding component, the intent is passed to the called component to complete a call to the component.

This is the implementation process of intent. It can be seen that the intent provides information about component calls to each other and decouples callers from callers.

2. Intent application scenarios

In summary, there are three main intent application scenarios:

2.1 start an activity

(1) activity. startactivity (intent); // start an activity

(2) activity. startactivityforresult (intent, int requestcode); // start an activity with a request code. When the activity ends, the onactivityresult () method of the original activity is called back and a result code is returned.

2.2 start a service

(1) Context. startservice (intent Service );

(2) Context. bindservice (intent service, serviceconnection Conn, int flags );

2.3 start a broadcast

(1) sendbroadcast (intent );

Sendbroadcastasuser (intent, userhandle user );

(2) sendstickybroadcast (intent );

Sendstickybroadcastasuser (intent, userhandle user );

(3) sendorderedbroadcast (intent, string receiverpermission );

Sendorderedbroadcastasuser (intent, userhandle user, string receiverpermission,

Broadcastreceiver resultreceiver,Handler scheduler, int initialcode, string initialdata, bundle initialextras );

 

3. Intent attribute settings

Intent attributes include: Action, data, and type), category (additional information for executing the action), component (specify the Class Name of the intent target component), and extras (a set of all other additional information ).

3.1 action (action to be executed)

The SDK defines a series of standard actions, as shown in Part 1.

Figure 1 partial standard action

Among them, action_call indicates the application that calls the call; action_edit indicates that the editor is called; action_sync indicates that the data is synchronized.

3.2 data (data operated by the Action)

In intent, data is represented by the URI pointing to the data. For example, in the Contact application, the URI pointing to the contact list is content: // contacts/people /.

3.3 type (explicitly specifying the intent data type)

For different actions, the URI data type is different.

Generally, the intent data type can be determined based on its own data. However, by setting this attribute, explicitly specified types can be forcibly used.

3.4 category (additional information for executing an action)

Category indicates additional information about the executed action. For example, if we want to receive the executed action, the top-level application is located at the top-level of all other applications, and we can use the additional information launcher_category.

3.5 component (specify the Class Name of the intent target component)

Component specifies the Class Name of the intent target component. Generally, Android searches for other attributes (such as action, data/type, and category) contained in the intent and finds a matched target component. However, if we set the Component Attribute and explicitly specify the Class Name of the intent target component, the above search process will not be executed.

3.6 extras (a set of all other additional information)

You can use extras to provide extended information for components.

4. Intent parsing process

When intent is used, intent objects are divided into two types based on whether the intent object receiver is explicitly specified. One is an explicit intent, that is, the receiver is specified when the intent object is constructed; the other is an implicit intent, that is, the receiver is not specified when the intent object is constructed.

For an explicit intent, Android does not need to parse the intent, because the target component is clear. For an implicit intent, Android needs to parse it and map the intent to the activity, service, or broadcast that can process the intent.

The intent parsing Mechanism finds the most matched intent by finding all intentfilters registered in the androidmanifest. xml file and intentfilters defined by intentfilter.

In the parsing process, Android identifies the most matched intent by judging the intent action, type, and category attributes. The specific judgment method is as follows:

(1) If intent specifies action, the action must be included in the action list of the intentfilter component; otherwise, the action cannot be matched;

(2) If the intent does not provide the type, the system will obtain the data type from the data. The data type list of the target component must contain the intent data type. Otherwise, the data type does not match.

(3) If the data in the intent is not content: URI and the intent does not explicitly specify its type, it will match according to the scheme (such as http: or mailto :) of the data in the intent. Similarly, the scheme of intent must appear in the scheme list of the target component; otherwise, it cannot match.

(4) If intent specifies one or more category categories, these categories must all appear in the category list of the target component; otherwise, they cannot match.

 

5. Intent instance

The following describes several intent instances.

5.1 call other applications

You can use intent to call and start other applications. For example, you can use the following method to call the dialing program:

 1  /*  2   * Function: Call the call procedure. 3   * Author: blog Park-still indifferent  4  */  5  Public   Void  Intentdemo_call (){  6 Intent intent_call = New Intent (); //  Create an intent  7 Intent_call.setaction (intent. action_call ); // Set intent to call  8 Intent_call.setdata (URI. parse ("Tel: 110 ")); //  Set phone number  9 Startactivity (intent_call ); //  Start Intention  10 }

Of course, because the call function is used here, we also need to add the resource permission to apply for a call in the androidmanifest. xml file. The specific implementation method is as follows:

1<! --Call permission-->2<Uses-Permission
Android: Name= "Android. Permission. call_phone"/>

For permission requests in Android, see Android Data Manual 02: Android. Permission permission requests summary.

5.2 jump to another activity

By using intent, you can not only call other applications, but also redirect activities between applications. For exampleCodeYou can jump from mainactivity to secondaryactivity, and pass two data names and age to secondaryactivity.

 1      /*  2   * Function: Jump to secondaryactivity  3   * Author: blog Park-still indifferent  4        */  5       Public   Void  Intentdemo_gotosecondaryactivity (){  6 Intent intent_tosecondary = New Intent (); // Create an intent  7 Intent_tosecondary.setclass ( This , Secondaryactivity. Class ); //  Jump to secondaryactivity  8 Intent_tosecondary.putextra ("name", "Jack "); //  Set transfer content name  9 Intent_tosecondary.putextra ("Age", 23 ); //  Set transfer content age 10 Startactivity (intent_tosecondary ); //  Start Intention  11 }

So, how do I receive the content (name and age) from mainactivity in secondaryactivity? The following code provides an implementation scheme.

 1  /*  2   * Function: receives intent_tosecondary from mainactivity.  3   * Author: blog Park-still indifferent  4        */ 5       Public   Void  Acceptintent (){  6 Intent intent_accept = getintent (); //  Create a receiving intent  7 Bundle bundle = intent_accept.getextras (); //  Create a bundle object to receive intent data  8 String name = bundle. getstring ("name "); //  Get intent content name 9           Int Age = bundle. getint ("Age "); //  Get intent content age  10 }

5.3 send an intent with callback Method

Sometimes, we may need to define a control in mainactivity to start secondaryactivity. When secondaryactivity ends, a execution result is returned to mainactivity.

To implement the above functions, you only need to complete the following three steps.

(1) In mainactivity, the intention to send a request code to secondaryactivity is as follows:

 1  /*  2  * Function: sends a request code to secondaryactivity.  3   * Author: blog Park-still indifferent  4        */  5       Public   Void  Intentdemo_request (){  6 Intent intent_request = New Intent (); //  Create an intent  7 Intent_request.setclass (This , Secondaryactivity. Class ); //  Jump to secondaryactivity  8 Startactivityforresult (intent_request, request_code ); //  Start intention with Request Code  9 }

(2) receive intent_request in secondaryactivity, fill in the content to be returned to mainactivity in intent, and set a return code. The specific implementation method is as follows:

 1  /*  2  * Function: receives intent_request from mainactivity and returns a result code.  3   * Author: blog Park-still indifferent  4        */  5       Public   Void  Acceptintentandreturn (){  6 Intent intent = getintent (); //  Create a receiving intent  7 Intent. putextra ("back", "data of secondaryactivity ");//  Set intent content  8 Setresult (result_code, intent ); //  Set the result code  9 Finish (); //  End secondaryactivity and return mainactivity  10 }

(3) When secondaryactivity is completed, the program will return to the mainactivity interface. At this time, the onactivityresult () method in mainactivity will be called back, and all we need to do is implement this method. In this example, the method is implemented as follows:

 1  /* 2   * Function: onactivityresult callback Method  3   * Author: blog Park-still indifferent  4        */  5       Protected   Void Onactivityresult ( Int Requestcode, Int  Resultcode, intent data ){  6           If (Requestcode = request_code & resultcode =Secondaryactivity. result_code ){  7 Bundle bundle = Data. getextras ();  8 String STR = bundle. getstring ("back" );  9 Toast. maketext ( This , "The returned value from secondaryactivity is:" + STR, Toast. length_long). Show ();  10   }  11 }

In the above code, we can determine requestcode and resultcode to uniquely identify the callers in mainactivity and those in secondaryactivity, and establish a one-to-one correspondence relationship. In addition, we get the content returned to mainactivity from secondaryactivity through the bundle object, and output it through toast, as shown in 2.

Figure 2 returned content from secondaryactivity

As shown in figure 2, the returned content from secondaryactivity is the string "data of secondaryactivity" defined in secondaryactivity ".

6. Summary

This blog post mainly introduces the concepts of intent and three usage methods of intent in activity. The usage of intent in service and broadcast will be introduced later in the learning process of service and broadcast.

 

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.