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