Android中BroadcastReceiver的基本使用

来源:互联网 发布:手机怎么隐藏网络 编辑:程序博客网 时间:2024/04/30 01:08

Android中BroadcastReceiver的基本使用

今天在项目中运用到Android中的广播,下面就来介绍下BroadcastReceiver的基本使用。

作用

BroadcastReceiver是用来监听程序或者系统发出的Broadcast Intent的,可以实现不同组件间的数据通信

分类

BroadcastReceiver分为“有序广播”和无序广播两类
1. 无序广播:
* 定义:所有跟广播的intent匹配的广播接收者都可以收到该广播,并且是没有先后顺序(同时收到)。
* 发送方式:sendBroadcast(Intent intent)
2. 有序广播:
* 定义:所有跟广播的intent匹配的广播接收者都可以收到该广播,但是会按照广播接收者的优先级来决定接收的先后顺序 。
* 发送方式:sendOrderedBroadcast(Intent intent,
String receiverPermission)
* 有优先级,根据优先级决定广播的接收顺序(-1000~1000)
* 可以通过abortBroadcast()来阻断广播的传播

注册方式

  • 在AndroidManifest.xml文件中注册
<receiver    android:name=".Broadcast.SecondReceiver"    android:enabled="true"    android:exported="true">    <intent-filter android:priority="10">            <action android:name="com.jhtwl.googlestore.mybroadsast" />    </intent-filter></receiver>
  • 通过代码注册
IntentFilter intentFilter = new IntentFilter();intentFilter.setPriority(20);intentFilter.addAction("com.jhtwl.googlestore.mybroadsast");registerReceiver(new MyReceiver(), intentFilter);

广播的解除

unregisterReceiver(BroadcastReceiver receiver)

系统广播消息对应的Intent

Intent 描述 android.intent.action.TIME_SET 系统时间被改变 android.intent.action.DATE_CHANGED 系统日期被改变 android.intent.action.TIMEZONE_CHANGED 系统时区被改变 android.intent.action.BOOT_COMPLETED 系统启动完成 android.intent.action.PACKAGE_ADDED 系统添加包 android.intent.action.PACKAGE_CHANGED 系统包改变 android.intent.action.PACKAGE_REMOVED 系统包被删除 android.intent.action.PACKAGE_RESTARTED 系统包被重启 android.intent.action.PACKAGE_DATA_CLEARED 系统包数据被清空 android.intent.action.BATTERY_CHANGED 电量改变 android.intent.action.BATTERY_LOW 电量低 android.intent.action.ACTION_POWER_DISCONNECTED 连接电源 android.intent.action.ACTION_POWER_DISCONNECTED 断开电源 android.intent.action.ACTION_SHUTDOWN 系统被关闭

Demo

  • 在一个Activity的xml文件中布局两个按钮
<Button   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:onClick="sendBroadcast"   android:text="发送广播"/><Button   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:onClick="unregisterBroadcast"   android:text="解除注册广播"/>
  • Activity中的代码
// 发送    public void sendBroadcast(View v) {        // 代码注册广播        IntentFilter intentFilter = new IntentFilter();        intentFilter.setPriority(20);        intentFilter.addAction("com.jhtwl.googlestore.mybroadsast");        myReceiver = new MyReceiver();        registerReceiver(new MyReceiver(), intentFilter);        Intent intent = new Intent();        intent.setAction("com.jhtwl.googlestore.mybroadsast");        intent.putExtra("message", "传递的数据");        // 发送无序广播        // sendBroadcast(intent);        // 发送有序广播        sendOrderedBroadcast(intent, null);    }    public void unregisterBroadcast(View v) {        unregisterReceiver(myReceiver);    }
  • MyReceiver中的代码
public class MyReceiver extends BroadcastReceiver {    public MyReceiver() {    }    @Override    public void onReceive(Context context, Intent intent) {        Toast.makeText(context, "接收到第一条广播", Toast.LENGTH_SHORT).show();        Bundle bundle = new Bundle();        bundle.putString("MyReceiver", "第一条广播接受者增加的数据");        setResultExtras(bundle);        // 终止广播//        abortBroadcast();    }}
  • SecondReceiver中的代码
public class SecondReceiver extends BroadcastReceiver {    public SecondReceiver() {    }    @Override    public void onReceive(Context context, Intent intent) {        Bundle bundle = getResultExtras(true);        Toast.makeText(context, bundle.getString("MyReceiver"), Toast.LENGTH_SHORT).show();    }}
  • AndroidManifest.xml中的配置
<receiver            android:name=".Broadcast.SecondReceiver"            android:enabled="true"            android:exported="true">            <intent-filter android:priority="10">                <action android:name="com.jhtwl.googlestore.mybroadsast" />            </intent-filter>        </receiver>
  • 运行效果

这里写图片描述

0 0