Android Activity and Intent mechanism learning notes (1)

Source: Internet
Author: User

Activity

In Android, Activity is the foundation of all programs, and the processes of all programs run in the Activity. The Activity has its own life cycle. For details, refer to "lifecycle ).

For an Activity, the key is to grasp its life cycle.) The second is to save and restore the onSaveInstanceState onRestoreInstanceState, and to redirect between the activities and transmit data intent ).

Common functions in Activity include SetContentView () findViewById () finish () startActivity (). The following are the functions involved in the lifecycle:

 
 
  1. void onCreate(Bundle savedInstanceState)  
  2. void onStart()  
  3. void onRestart()  
  4. void onResume()  
  5. void onPause()  
  6. void onStop()  
  7. void onDestroy() 

Note thatTo use the Activity, you must add the corresponding <Activity> in the Manifest file and set its attributes and intent-filter.

Intent

Android provides an Intent mechanism to assist in interaction and communication between applications. Intent describes the actions, actions involving data, and additional data of an application, android finds the corresponding component based on the description of the Intent, passes the Intent to the called component, and calls the component. Intent can be used not only between applications, but also between activities/services within the application. Therefore, Intent acts as a media intermediary here, providing information about component calls to each other to decouple callers from callers. The function of Intent is shown in the SDK as follows:

Start an Activity through Context. startActivity () implements tietong. startActivityForResult;
Start a service through Context. startService (), or use Context. bindService () to interact with the background service;
Send the broadcast method (such as Context. sendBroadcast (), Context. sendOrderedBroadcast (), and Context. sendStickyBroadcast () to broadcast receivers.
Settings of Intent attributes include the following: the settings are defined in XML, and can also be obtained and set using the Intent class method)

1) Action, that is, the Action to be executed

The SDk defines some standard actions, including

Of course, you can also customize the action. When using a custom action, you must add the package name as the prefix, such as "com. example. project. SHOW_COLOR), and you can define the corresponding Activity to process our custom actions.

2) Data, that is, the Data to be operated by the execution action

Android uses a URI pointing to data. For example, in a contact application, a URI pointing to a contact may be: content: // contacts/1. For different actions, the URI Data type is different. You can set the type attribute to specify a specific type of Data. For example, ACTION_EDIT specifies that Data is the file URI and calls tel: URI, the access network is http: URI, while the data provided by the content provider is content: URIs.

(3) type data type), explicitly specifying the Intent data type MIME ).Generally, the Intent data type can be determined based on the data itself. However, by setting this attribute, You can forcibly use the explicitly specified type instead of derivation.

(4) category.For example, LAUNCHER_CATEGORY indicates that the receiver of Intent should appear as a top-level application in Launcher, while ALTERNATIVE_CATEGORY indicates that the current Intent is one of a series of optional actions that can be executed on the same piece of data. There are other

Constant Meaning

5) component ),Specifies the Class Name of the Intent's target component. Generally, Android searches for other attributes in Intent, such as action, data/type, and category, and finds a matched target component. However, if this attribute of component is specified, the component specified by component will be used directly without executing the above search process. After this attribute is specified, all other Intent attributes are optional.

6) extras additional information ),Is a collection of all other additional information. You can use extras to provide extended information for components. For example, if you want to perform the "send email" action, you can save the email title and body in extras, send to the email sending component.

One of the keys to understanding Intent is to understand the two basic usage of Intent: an explicit Intent, that is, specifying the receiver when constructing the Intent object; the other is an implicit Intent, that is, when the Intent sender constructs an Intent object, it does not know or care about who the receiver is. This helps reduce the coupling between the sender and the receiver.

Android does not need to parse explicit Intent because the target component is clear. Android needs to parse implicit Intent, map Intent to the Activity, IntentReceiver, or Service that can process the Intent.

The Intent parsing mechanism mainly finds the matching Intent by finding all intentfilters registered in AndroidManifest. xml and the Intent defined in them. In this parsing process, Android uses the Intent action, type, and category attributes to determine the attributes. The judgment method is as follows:

If Intent specifies an action, the action must be included in the IntentFilter action list of the target component; otherwise, the action cannot be matched;
If the Intent does not provide a type, the system obtains the data type from the data. Like action, the Data Type list of the target component must contain the Intent data type. Otherwise, the data type does not match.
If the data in the Intent is not a content: type URI and the Intent does not explicitly specify its type, it will match according to the scheme of the data in the Intent, such as http: or mailto. Similarly, the scheme of Intent must appear in the scheme list of the target component.
If Intent specifies one or more category categories, these categories must all appear in the Set category list. For example, Intent contains two types:LAUNCHER_CATEGORYAndALTERNATIVE_CATEGORYThe parsed target component must contain at least these two categories.
Intent-Filter Definition

Examples of setting attributes:

 
 
  1. <action android:name="com.example.project.SHOW_CURRENT" /> 
  2. <category android:name="android.intent.category.DEFAULT" /> 
  3. <data android:mimeType="video/mpeg" android:scheme="http" . . . />   
  4. <data android:mimeType="image/*" /> 
  5. <data android:scheme="http" android:type="video/*" /> 

Complete instance

 
 
  1. <activity android:name="NotesList" android:label="@string/title_notes_list">   
  2. <intent-filter>                  
  3. <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> 
  4. </intent-filter>   
  5. <intent-filter>          
  6. <action android:name="android.intent.action.VIEW" /> 
  7. <action android:name="android.intent.action.EDIT" />     <action android:name="android.intent.action.PICK" />     <category 
  8. android:name="android.intent.category.DEFAULT" />   
  9. <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />   
  10. </intent-filter>              
  11. <intent-filter>                  
  12. <action android:name="android.intent.action.GET_CONTENT" />      <category 
  13. android:name="android.intent.category.DEFAULT" />        <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />              
  14. </intent-filter>         
  15. </activity> 


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.