Read "Android from getting Started to mastering" (--intent) category

Source: Internet
Author: User

Intent classification

Explicit Intent:intent ("Android.intent.action.CALL", Uri.parse ("Tel:" + string));
Need to specify a name to start. For in-Program multi-Activity interactions, the pass is often used within the application's internal messages, known as Action Intent.


Implicit Intent: Generally does not specify the name, but uses the broadcast form, generally is broadcast Intent.

Action Intent:

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center ">

The action largely determines how the rest of the Intent is built, especially the data and additional (extras) fields, just as a method name determines the number of parameters and the return value.

Therefore, the Intent object action is set after setaction. The detailed Action has a detailed data format requirement. For example: The Action_edit data field will include the Url;action_call for editing the document is Tel:url. Also, you should understand the type of data format. For example, the data obtained is audio, video, text, image, or other, which requires MIME to be specified by SetType. Frequently used Category such as the following:

Category_browsable
Category_gadget
Category_home
Category_launcher
Category_preference

Addcategory is used to join a kind to Intent. Accordingly, Removecategory is used to delete the previous category. A Intent can have multiple category,getcategories to get all the kinds in Intent

Broadcast Intent

Action Intent can only be responded to by a specified Activity, assuming it is necessary to push notification of this broadcast message. You need broadcast Intent

Broadcast Intent processing process

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center ">

Register broadcast Intent steps to inherit Broadcastreceiver and override the Onreceiver method:

Package Com.sweetlover.camera2basic;import Android.content.broadcastreceiver;import Android.content.Context; Import Android.content.intent;public class Myreceiver extends Broadcastreceiver {@Overridepublic void OnReceive ( Context arg0, Intent arg1) {//TODO auto-generated method stub}}
According to Intentfilter broadcast Intentjava register

Intentfilter myfilter = new Intentfilter ("Android.provider.Telephony.SMS_RECEIVER"); Myreceiver myreceiver = new Myreceiver (); Context.registerreceiver (Myreceiver, myfilter);

XML Brochure

<receiver android:name= ". Myreceiver "><intent-filter><action android:name=" <receiver android:name= ". Myreceiver "></intent-filter></receiver>
Broadcasting


Receive

Broadcast receiver infers after receiving the Intent and responds to the Onreceiver method in accordance with the condition

public void Onreceiver (Context mycontext, Intent myintent) {if (Myintent.getaction (). Equals (Intent.action_battery_low ) {//TODO switch to power-saving mode when low, turn off WIFI and GPS}}

Destroyed

Each time receiver responds to a Intent, it is self-destruct, receiver has a temporal limit, and the program is unresponsive when timed out

Detailed Demo sample Complete program: Http://download.csdn.net/detail/sweetloveft/94685201.MainActivity.java

@Overrideprotected void OnStart () {//TODO auto-generated method Stubsuper.onstart (); try {thread.sleep ()} catch ( Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} Final Intent Intent = new Intent (my_new_lifeform); Sendbroadcast (Intent);}

2.receiver.java

Package Com.sweetlover.activity;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import Android.widget.toast;public class Receiver extends Broadcastreceiver {@Overridepublic void OnReceive (Context context, Intent Intent) {//TODO auto-generated Method Stubtoast.maketext (Context, "Receive broadcast message", Toast . Length_short). Show ();}}

3.activity_main.xml

Add an empty linearlayout layout to

4.androidmanifest.xml

Add in <application> tags

        <receiver android:name= "Com.sweetlover.activity.Receiver" >            <intent-filter>                <action Android:name= "Com.china.ui.NEW_LIFEFORM"/>            </intent-filter>        </receiver>

Read "Android from getting Started to mastering" (--intent) category

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.