Android中BroadcastReceiver的基本使用

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值