首先继承BroadcastReceiver类,并在manifest中注册

public class MyReceiver extends BroadcastReceiver {    public MyReceiver() {    }    @Override    public void onReceive(Context context, Intent intent) {        throw new UnsupportedOperationException("Not yet implemented");    }}

在mainifest中注册

<receiver  android:name=".MyReceiver" android:enabled="true" android:exported="true" >        </receiver>

动态注册和取消广播接收器

上代码:
Receiver部分:

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");    }}

主类部分:

public class MainActivity extends Activity implements View.OnClickListener{    private MyReceiver receiver = null;    private Button 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()){            case R.id.send:                Intent intent = new Intent(MyReceiver.ACTION);                intent.putExtra("data","jiekxueyuan");                sendBroadcast(intent);                break;            case R.id.reg:                if (receiver == null){                    receiver = new MyReceiver();                    registerReceiver(receiver,new IntentFilter(MyReceiver.ACTION));                }                break;            case R.id.unReg:                if (receiver != null){                    unregisterReceiver(receiver);                    receiver = null;                }                break;        }    }}

布局代码:

<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>

广播的优先级

这次我们在manifest中静态注册

<receiver  android: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>        <receiver  android: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:用于设置优先级,数字越大,优先级越高。

高优先级的终端广播

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

接收部分

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

更多相关文章

  1. Android(安卓)day_10-02 (广播接收者的使用 五个小案例)
  2. Android(安卓)app的登录和注册功能
  3. Android(安卓)使用广播(BroadcastReceiver)传递数据
  4. Android(安卓)day_10-02 (广播接收者的使用 五个小案例)
  5. Android监听应用程序安装和卸载
  6. Android(安卓)R- CarAudioService之registerAudioPolicy动态注册
  7. android 广播机制(1) 注册广播
  8. Android开机启动Activity或者Service方法
  9. Android(安卓)四大组件 简介

随机推荐

  1. Android中Context的使用总结
  2. Unity与Android——AS打aar包供Unity调用
  3. Android 使用三方库android-gif-drawable
  4. 四种基本布局
  5. maven 学习笔记(五)-创建简单的eclipse+and
  6. Android 定位
  7. [Boot]Android系统启动-综述
  8. 新势力--Android,开发环境轻松搭建
  9. Android开发无法打开模拟器的问题:use '@f
  10. Android怎么获取ListView的值