BroadcastReceiver

本文介绍了BroadcastReceiver在Android中的作用,作为四大组件之一,它需要继承BroadcastReceiver基类并重写onReceive()方法。广播涉及到消息订阅者、发布者和消息中心三个角色。注册广播有两种方式:静态注册和动态注册,动态广播建议在Activity的onResume()和onPause()中进行。此外,广播分为普通、系统、有序、粘性和本地五种类型。

BroadcastReceiver:
1.广播接收器(四大组件之一)2.继承BroadcastReceivre基类 3.必须复写抽象方法onReceive()方法

三个角色:
1.消息订阅者(广播接收者)

       String aaa = intent.getExtras().getString("msg");
        Bundle bundle=getResultExtras(false);
        MainActivity.textView.setText(aaa);
        Bundle bundle1=new Bundle();
        bundle1.putString("msg","aaaaaa");
        setResultExtras(bundle1);

2.消息发布者(广播发布者)

        Intent intent=new Intent();
        intent.setAction("a");
        Bundle bundle=new Bundle();
        bundle.putString("msg","发送广播");
        intent.putExtras(bundle);
        系统的的不用发

3.消息中心

注册的方式分为两种:
1.静态注册

 <receiver
        //必须写 .MyReceiver为继承 BroadcastReceivre的类
        android:name=".MyReceiver"
            >
        <intent-filter>
           //系统的
            <action android:name="android.intent.action.AIRPLANE_MODE" />
        </intent-filter>
    </receiver>

2.动态注册(动态广播最好在Activity 的 onResume()注册、onPause()注销,否则会导致内存泄露)

// 选择在Activity生命周期方法中的onResume()中注册
@Override
  protected void onResume(){
      super.onResume();

    // 1. 实例化BroadcastReceiver子类 &  IntentFilter(过滤器)
     mBroadcastReceiver mBroadcastReceiver = new mBroadcastReceiver();
     IntentFilter intentFilter = new IntentFilter();

    // 2. 设置接收广播的类型(调频)
    系统:
    intentFilter.addAction(android.net.conn.CONNECTIVITY_CHANGE);
    自定义:
     intentFilter.addAction(“随意的值”);

    // 3. 动态注册:调用Context的registerReceiver()方法 参数(继承类BroadcastReceiver,intentFilter )
     registerReceiver(mBroadcastReceiver, intentFilter);
 }
//销毁注册
 @Override
 protected void onPause() {
     super.onPause();
      //销毁在onResume()方法中的广播
     unregisterReceiver(mBroadcastReceiver);
     }
}

广播的五大类型:
普通广播(Normal Broadcast)

//发送方法
sendBroadcast(intent);

系统广播(System Broadcast)

监听网络变化
android.net.conn.CONNECTIVITY_CHANGE
关闭或打开飞行模式
Intent.ACTION_AIRPLANE_MODE_CHANGED
充电时或电量发生变化
Intent.ACTION_BATTERY_CHANGED
电池电量低
Intent.ACTION_BATTERY_LOW
电池电量充足(即从电量低变化到饱满时会发出广播
Intent.ACTION_BATTERY_OKAY
系统启动完成后(仅广播一次)
Intent.ACTION_BOOT_COMPLETED
按下照相时的拍照按键(硬件按键)时
Intent.ACTION_CAMERA_BUTTON
屏幕锁屏
Intent.ACTION_CLOSE_SYSTEM_DIALOGS
设备当前设置被改变时(界面语言、设备方向等)
Intent.ACTION_CONFIGURATION_CHANGED
插入耳机时
Intent.ACTION_HEADSET_PLUG
未正确移除SD卡但已取出来时(正确移除方法:设置--SD卡和设备内存--卸载SD卡)
Intent.ACTION_MEDIA_BAD_REMOVAL
插入外部储存装置(如SD卡)
Intent.ACTION_MEDIA_CHECKING
成功安装APK
Intent.ACTION_PACKAGE_ADDED
成功删除APK
Intent.ACTION_PACKAGE_REMOVED
重启设备
Intent.ACTION_REBOOT
屏幕被关闭
Intent.ACTION_SCREEN_OFF
屏幕被打开
Intent.ACTION_SCREEN_ON
关闭系统时
Intent.ACTION_SHUTDOWN
重启设备
Intent.ACTION_REBOOT

有序广播(Ordered Broadcast)

//按照Priority属性值从大-小排序;
//Priority属性相同者,动态注册的广播优先;
//设置优先级(-1000到1000之间)
setPriority(1);
//有序广播的发送方法
sendOrderedBroadcast(intent);
(有序和无序的区别:
                 1.有序是一个个的接,中间传输的时候可以改变,按优先级来;
                 2.无序是一个发,无数个去接,不管什么时候接到或者是接不到)

粘性广播(Sticky Broadcast)
由于在Android5.0 & API 21中已经失效,所以不建议使用。
App应用内广播(Local Broadcast)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值