Android Learning Note (three or four): Talk intent (UP)-some knowledge

Source: Internet
Author: User

In the Android learning Note (vii): Multiple activity and intent, we learned how to use intent to evoke activity in code. Learn here in a deep step.

What is intent

Intent is an abstract description of an operation's processing. Context can use StartActivity (intent) to launch a actvivity, as we do in the study notes (vii), and the most common way to integrate activity into our applications , can be broadcast to any interested broadcastreceiver through Sentbroast (intent), can start the service through StartService (intent), can be bindservice (intent, serviceconnection, int) communicates with a background service.

Basic elements

Intent has two basic requirements, one is action, one is data, that is, which processing action is used, and provides the data passed to the processing action. The ACTION is a constant, such as action_view for viewing the resource, Action_edit for editing the resource, and Action_pick is making a selection. Data is the way the URI is.  For example action_dial Content://contacts/people/1,action_view tel:123,action_edit Content://contacts/people/1,action_view content://contacts/people/and so on. One of the Android system's desktop icon to launch action for Action_main is not with data.

Look at one of the intent constructors: Intent = new Intent (intent.action_sendto ,URI). This is our example in the note (vii), which contains two important messages: action (action handled) and data (parameters passed to the processing action).

Basic properties of Intent

We can set properties in the Minifest.xml file, or in code writing, by means of intent.

category: The main activity uses the Launcher category, which indicates that it will be displayed in the Launcher menu, other general use of default or alternative. Look at an example in Manifest.xml.

<manifest xmlns:android= "http://schemas.android.com/apk/res/android" package= " Com.commonsware.android.skeleton,
    <application>
         <activity android:name= ". Now "android:label=",
            < Intent-filter>
                 < action  android:name= "Android.intent.action.MAIN"/>
                 < category   Android:name= "Android.intent.category.LAUNCHER"/>
             </intent-filter>
        </activity>
     </application>
</manifest>

The relevant intent information is defined in <intent-filter>. Intent-filter belongs to the activity. This example of our definition in Intent-filter indicates that this is the main activity in an application, with category is launcher, that is, there is an icon in the main menu of Android.
One can have multiple actions and multiple categories in Intent-filter, which indicates that this component, which is the activity, can handle many types of intent.

MIME type: Indicates the URI data type, which is the type of resource to be manipulated. MIME Type/data is a good way to do this if we want to use the services provided by third-party code.

<activity android:name= ". Tourviewactivity ">
<intent-filter>
<action android:name= "Android.intent.action.VIEW"/>
<category android:name= "Android.intent.category.DEFAULT"/>
<data Android:mimetype= "Vnd.android.cursor.item/vnd.commonsware.tour"/>
</intent-filter>
</activity>

This is a non-active activity in the app, and his URI type is vnd.android.cursor.item/vnd.commonsware.tour.

Component: The class type of activity to be received by intent, but generally we do not need to set up, Android will be based on intent information, such as Action,data/mime type, and catergory to automatically select the desired build. If this property is set, it will be mandatory unless we are well aware of it, or have a special designation (we do not use the system default method to use our own processing, and our processing should be within our app, component's name in our current app is private), generally do not set this.

Extras: Information passed through bundles, such as the message content of an email.

Receiver Receiver

Sometimes it is desirable to trigger some processing in the service, rather than to evoke an activity, in some cases, the arousal is conditionally selected, and in condition A is the arousal of acivitym, and in condition B, the activity N is aroused. For these cases, you can use the inherited Broadcastreceiver interface to handle it.

This intent receiver is a one-off, used to receive intent, especially the intent of this broadcast mode (for example, insufficient broadcast power), and then action. Action typically launch additional intent to trigger actvivty, service, and other componment processing logic.
Broadcastreceiver There is only one method OnReceive (), the way to add a receiver in manifest is:

<receiver android:name= ". Myintentreceiverclassname "/>

Broadcastreceiver will remain alive until the execution of the OnReceive (), End of life cycle, no longer used. This is something we should pay attention to when we use, for example, do not have any callback in OnReceive (), in other words, not with the service, and not to open a dialog.

If you want to trigger a long-lived component, such as a actvity or service, you need receiver to have the same survival time as these activity. In this case, do not define receiver in manifest, but in the code. Call Registerreceiver () in the activity's Onresume () to indicate interest in a intent, call Unregisterreceiver () in OnPause () to indicate that these intents are not needed, Because we don't need to receive intent during pause, this saves money. Be careful not to unregister in activity.onsaveinstancestate () so that it is not called when the user returns through the back key.

RELATED Links: My Android development related articles

Android Learning Note (three or four): Talk intent (UP)-some knowledge

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.