I. Most basic custom broadcast reception
1. Mainactivity Code
public class Mainactivity extends Activity implements
Onclicklistener {
Private Button button1;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
button1= (Button) This.findviewbyid (R.id.button1);
Button1.setonclicklistener (this);
}
public void OnClick (View v) {
TODO auto-generated Method Stub
Intent intent=new Intent ();
Intent.setaction ("Aaa.action.MyBroadcast");
Sendorderedbroadcast (intent, NULL); Define ordered broadcasts
}
}
2. Receiver Code
public class MyBroadcastReceiver2 extends Broadcastreceiver {
Static final String ACTION = "Aaa.action.MyBroadcast";
@Override
public void OnReceive (context context, Intent Intent) {
TODO auto-generated Method Stub
if (Intent.getaction (). Equals (ACTION)) {
Toast.maketext (Context, "Login failed ...", Toast.length_long). Show ();
LOG.I ("Mybroadcast", "1111MyBroadcast");
}
}
}
public class MyBroadcastReceiver3 extends Broadcastreceiver {
Static final String ACTION = "Aaa.action.MyBroadcast";
@Override
public void OnReceive (context context, Intent Intent) {
TODO auto-generated Method Stub
if (Intent.getaction (). Equals (ACTION)) {
Toast.maketext (Context, "Login failed ...", Toast.length_long). Show ();
LOG.I ("Mybroadcast", "22222MyBroadcast");
}
}
}
3. Registration
<receiver android:name= ". MyBroadcastReceiver2 ">
<intent-filter android:priority= ">"
<action android:name= "Aaa.action.MyBroadcast"/>
</intent-filter>
</receiver>
<receiver android:name= ". MyBroadcastReceiver3 ">
<intent-filter android:priority= ">"
<action android:name= "Aaa.action.MyBroadcast"/>
</intent-filter>
</receiver>
Two. Define the boot-up broadcast
1.
public class Mybroadcastreceiver extends Broadcastreceiver {
Static final String ACTION = "Android.intent.action.BOOT_COMPLETED";
@Override
public void OnReceive (context context, Intent Intent) {
TODO auto-generated Method Stub
if (Intent.getaction (). Equals (ACTION)) {
Intent intent2=new Intent (context, mainactivity.class);
Context.startactivity (Intent2);
}
}
}
<uses-permission android:name= "Android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver
Android:name= ". Mybroadcastreceiver ">
<intent-filter >
<action android:name= "Android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
Broadcast Broadcast code example