广播接收者
一.什么是广播
生活中的广播大家都知道吧,就像村里的大喇叭,只要喇叭一放音乐全村都可以听到。
二.什么是BroadcastReceiver
Android四大组件之一,BroadcastReceiver就相当于村里的大喇叭,用来在APP之间传数据的,是一种通信手段。系统自己在很多时候都会发送广播,比如电量低或者充足,刚启动完,插入耳机,输入法改变等, 发生这些时间,系统都会发送广播,这个叫系统广播。
三.项目中使用广播
广播三要素:
(1)广播发送者 : 发送广播
(2)广播接收者(调频): 用于接收广播
(3)要处理的事情 :处理广播的相关信息, Intent有图对象
广播的使用场景:
(1)同一APP下多个组件之间传递数据(Activity/Fragment/Service之间传递数据)
(2)2个APP之间传递数据
技能get点:
(1)自定义广播接受者
(2)使用广播接受者进行电话拦截和短信拦截和系统电量的变化
四.广播生命周期
静态注册和动态注册的区别:假如说Activity是接受者:
动态注册:
(1)广播会跟Activity的生命周期的结束而结束;
(2)自由的控制注册和取消,有很大的灵活性
静态注册:
(1)广播不会跟随Activity的生命周期的结束而结束,一直存在,即使应用程序关闭,也会被唤醒接受广播
(2)全局的广播
五.广播分类
六.使用BroadcastReceiver
1.自定义类继承BroadcastReceiver,重写onReceive
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.1804.receiver")){
Bundle bundle = intent.getBundleExtra("mybundle");
String message = bundle.getString("msg");
Log.d("amy",message); //端午节
}
}
}
2.注册receiver
静态注册
<receiver android:name=".MyBroadcastReceiver">
<!--广播优先级0-1000,默认500-->
<intent-filter android:priority="1000">
<action android:name="android.1804.receiver" />
</intent-filter>
</receiver>
动态注册
public class ReceiverActivity extends AppCompatActivity {
MyBroadcastReceiver registerReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receiver);
registerReceiver = new MyBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.setPriority(1000);
intentFilter.addAction("android.1804.receiver");
registerReceiver(registerReceiver,intentFilter);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(registerReceiver);
}
}
3.发送广播
public void sendBroadcast(View view) {
Intent intent=new Intent();
intent.setAction("android.1804.receiver");
Bundle bundle=new Bundle();
bundle.putString("msg","端午节快乐");
intent.putExtra("mybundle",bundle);
//以下这行代码静态注册时必须添加,第二个参数为自定义广播接收者类名
// intent.setComponent(new ComponentName(getPackageName(),"com.example.myapplication.MyBroadcastReceiver"));
sendBroadcast(intent);
}
七.发送有序广播
清单文件
<receiver android:name=".MyBroadcastReceiver">
<!--广播优先级0-1000,默认500-->
<intent-filter android:priority="100">
<action android:name="android.1804.receiver" />
</intent-filter>
</receiver>
<receiver android:name=".MyBroadcastReceiver2">
<intent-filter android:priority="1000">
<action android:name="android.1804.receiver"/>
</intent-filter>
</receiver>
Activity
public void sendBroadcast(View view) {
Intent intent=new Intent();
intent.setAction("android.1804.receiver");
intent.putExtra("msg2","端午节快乐");
sendOrderedBroadcast(intent,null);
}
效果:
八.中断广播
效果:
九粘性广播
为什么要使用粘性广播及如何使用
粘性广播无需注册直接发送广播,广播发送后立即被保存。
权限
<uses-permission android:name="android.permission.BROADCAST_STICKY"/>
activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receiver);
sendStickyBroadcast(new Intent("android.1804.receiver.sticky"));
sendBroadcast(new Intent("android.1804.receiver.normal"));
}
public void sendBroadcast(View view) {
MyBroadcastReceiver2 myBroadcastReceiver2 = new MyBroadcastReceiver2();
IntentFilter intentFilter = new IntentFilter("android.1804.receiver.normal");
registerReceiver(myBroadcastReceiver2,intentFilter);
}
public void sendStickyBroadcast(View view) {
MyBroadcastReceiver myBroadcastReceiver = new MyBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter("android.1804.receiver.sticky");
registerReceiver(myBroadcastReceiver,intentFilter);
}
点击第一个按钮有打印
点击第二个按钮没反应
十.本地广播
本地广播特点
- 发送的广播只会在自己App内传播,不会泄露给其他App,确保隐私数据不会泄露
- 其他App也无法向你的App发送该广播,不用担心其他App会来搞破坏
- 比系统全局广播更加高效
用法
//获得manager对象
LocalBroadcastManager manager = LocalBroadcastManager.getInstance(this);
manager.registerReceiver(registerReceiver,intentFilter);
//发送消息
manager.sendBroadcast(intent);
十一.监听手机状态
屏幕锁屏状态
ACTION_SCREEN_ON:亮屏
ACTION_SCREEN_OFF:锁屏
//动态注册
MyBroadcastReceiver3 receiver3 = new MyBroadcastReceiver3();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.intent.action.SCREEN_OFF");
intentFilter.addAction("android.intent.action.SCREEN_ON");
intentFilter.setPriority(1000);
registerReceiver(receiver3,intentFilter);