Android Learning Tutorial Six----One of the four components--broadcastreciever

Source: Internet
Author: User

Blog for a long time has not been updated, to do a basic summary, continue before, warm and so know new! The series is an introductory article, the Great God can bypass !

Hello Everyone, today to explain the Android Broadcastreceiver. On Android, in Android, broadcast is a wide

Pan The mechanism used to transfer information between applications is a widely used The mechanism of transmitting information between programs is essentially a global monitoring

Listen to to listen to the system's global broadcast messages.

Broadcastreceiver There are two ways to register: One: Static Registration method, two: Dynamic registration method

One: The static registration method is configured in Anroidmanifest.xml :

Example:

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

Send the Activity key code as follows:

String actionname = "android.intent. myfirstbroadcast"; Define an action

Intent broadcastintent = new Intent (actionname);
Sendbroadcast (broadcastintent); Send broadcast

The Accept message broadcast class needs to inherit broadcastreceiver example myreceiver: The key code is as follows:

public class Myreceiver extends broadcastreceiver{

public void OnReceive (Context arg0, Intent arg1) {
TODO auto-generated Method Stub
Toast Toast = Toast.maketext (Context, "received broadcast is eliminated as" +intent.getaction, Toast.length_short);
Toast.show ();
}


Two: Dynamic registration method: Register such event by the application itself processing, usually in the Onresume event through the Registerreceiver Register,

Unregister through Unregisterreceiver in events such as OnPause. This way of registering makes it possible to stay focused on events during operation.

     protected void Onresume () {
                 Super.onresume ();
                System.out.println ("====onresume=====");
                Intentfilter Intentfilter = new Intentfilter ();
                Intentfilter.addaction ("Android.intent.myfirstbroadcast");

               myreceiver  Myfisrtreceiver =new myreceiver ();//Define Receiver object
                 registerreceiver (Myfirstreceiver, Intentfilter);        //Register
       }
       protected void OnPause () {
                 Super.onpause ();
                System.out.println ("====onpause====");
                Unregisterreceiver (Myfistreceiver);            // Anti-registration in OnPause
       }

The corresponding broadcast message accepts the class Myreceiver:

Public Myreceiver extends Broadcastreceiver {

@Override
public void OnReceive (context context, Intent Intent) {
Toast Toast = Toast.maketext (context, "Send dynamic Register broadcast", Toast.length_short);
Toast.show ();
}

};
Two forms of registration have been finished, about in-depth understanding of the broadcasting mechanism, please see the Luo Shenyang Broadcast registration monitoring mechanism source analysis, here is the main introductory explanation.

the context provides two ways to send broadcasts:

Sendbroadcast: Used to send a normal broadcast

Sendorderedbroadcast: Used to send an ordered bradcast.

Android Learning Tutorial Six----One of the four components--broadcastreciever

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.