Android broadcast receiver broadcast receiver-android learning Journey (12)

Source: Internet
Author: User

First inherit the Broadcastreceiver class and register it in the manifest
publicclass MyReceiver extends BroadcastReceiver {    publicMyReceiver() {    }    @Override    publicvoidonReceive(Context context, Intent intent) {        thrownew UnsupportedOperationException("Not yet implemented");    }}

Register in Mainifest

<receiver            android:name=".MyReceiver"            android:enabled="true"            android:exported="true" >        </receiver>
Dynamic registration and cancellation of broadcast receivers

On the code:
Receiver section:

public   class  myreceiver  extends  broadcastreceiver  { public  static  final     String ACTION =  "Peng.liu.testview.intent.action.MyReceiver" ; public  myreceiver  () {}  @Override  public  void  onreceive  (context context, Intent Intent) {System.out.println    (Intent.getstringextra ( "data" ) + "hello" ); }}

Main class section:

 Public  class mainactivity extends Activity implements View. Onclicklistener {    PrivateMyreceiver receiver =NULL;PrivateButton Send,reg,unreg;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main); Findviewbyid (r.id.send). Setonclicklistener ( This); Findviewbyid (R.id.reg). Setonclicklistener ( This); Findviewbyid (R.id.unreg). Setonclicklistener ( This); }@Override     Public void OnClick(View view) {Switch(View.getid ()) { CaseR.id.send:intent Intent =NewIntent (myreceiver.action); Intent.putextra ("Data","Jiekxueyuan"); Sendbroadcast (Intent); Break; CaseR.id.reg:if(Receiver = =NULL) {receiver =NewMyreceiver (); Registerreceiver (receiver,NewIntentfilter (myreceiver.action)); } Break; CaseR.id.unreg:if(Receiver! =NULL) {unregisterreceiver (receiver); Receiver =NULL; } Break; }    }}

Layout code:

<linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"xmlns:tools="Http://schemas.android.com/tools"Android:layout_width="Match_parent"android:layout_height="Match_parent"android:paddingleft="@dimen/activity_horizontal_margin"android:paddingright="@dimen/activity_horizontal_margin"android:paddingtop="@dimen/activity_vertical_margin"Android:paddingbottom="@dimen/activity_vertical_margin"tools:context=". Mainactivity "android:orientation="Vertical"> <button android:layout_width="Wrap_content"android:layout_height="Wrap_content"Android:text="Reg"Android:ID="@+id/reg"/> <button android:layout_width="Wrap_content"android:layout_height="Wrap_content"Android:text="Unreg"Android:ID="@+id/unreg"/> <button android:layout_width="Wrap_content"android:layout_height="Wrap_content"Android:text="Send"Android:ID="@+id/send"/></linearlayout>
Priority of Broadcast

This time we are statically registered in the manifest

<receiverandroid:name=". Myreceiver "android:enabled=" true "android:exported=" true " >                                                <intent-filter android:priority="8">                <action android:name="Peng.liu.testview.intent.action.MyReceiver"/>            </intent-filter>        </receiver>        <receiverandroid:name=". MyReceiver2 "android:enabled=" true "android:exported=" true " >                                                <intent-filter android:priority="9">                <action android:name="Peng.liu.testview.intent.action.MyReceiver"/>            </intent-filter>        </receiver>

Android:priority: Used to set the priority, the higher the number, the higher the priority level.

High-priority End-broadcast
//发送部分注意是发送sendOrderedBroadcast(intent,null);new Intent(MyReceiver.ACTION);                intent.putExtra("data","jiekxueyuan");                sendOrderedBroadcast(intent,null);

Receiving part

publicclass MyReceiver2 extends BroadcastReceiver {    publicMyReceiver2() {    }    @Override    publicvoidonReceive(Context context, Intent intent) {        System.out.println(intent.getStringExtra("data"));        //这一户用于中断后面的低优先级的接受        abortBroadcast();    }}

Android broadcast receiver broadcast receiver-android learning Journey (12)

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.