Broadcast Broadcast code example

Source: Internet
Author: User

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

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.