BroadCast 广播
- 广播的介绍
- 广播作用以及机制
- 项目中广播使用
- 广播生命周期
- 广播的分类
- 无序广播发送 (也叫标准广播)
- 有序广播发送
- 如何实现广播
- 静态广播注册
- 动态广播注册
- 系统广播
- 静态接收系统锁屏广播
- 使用注意事项
一.BroadCastReceiver介绍:
BroadCastReceiver广播接受者,安卓四大组件之一
广播三要素:
(1)广播发送者 : 发送广播
(2)广播接收者(调频): 用于接收广播
(3)要处理的事情 :处理广播的相关信息, Intent有图对象
广播的使用场景:
(1)同一APP下多个组件之间传递数据(Activity/Fragment/Service之间传递数据)
(2)2个APP之间传递数据
技能get点:
(1)自定义广播接受者
(2)使用广播接受者进行电话拦截和短信拦截和系统电量的变化
二.如何实现广播
步骤1:广播接受者
(1)自定义类继承BroadcastReceiver,重写onReceive方法
(2)注册广播(安卓的四大组件都需要注册)
静态注册:在清单文件中
动态注册:在代码中注册(注册和解除注册)
步骤2:广播发送方:sendBroadcast(Intent意图对象)
静态注册和动态注册的区别:假如说Activity是接受者:
动态注册:
(1)广播会跟Activity的生命周期的结束而结束;
(2)自由的控制注册和取消,有很大的灵活性
静态注册:
(1)广播不会跟随Activity的生命周期的结束而结束,一直存在,即使应用程序关闭,也会被唤醒接受广播
(2)全局的广播
三.代码案例:
(1)自定义广播接受者类继承BroadCastReceiver,重写onReceiver方法
public class MyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if("android.bawei.action.customer".equals(action)){
Bundle bundle = intent.getExtras();
int msg=bundle.getInt("msg");
Log.i("TAG", "收到了一个广播: "+msg);
}
}
}
(2)注册广播方式一:清单文件静态注册
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="com.hahaha" />
</intent-filter>
</receiver>
(3)注册广播方式二:动态注册
onCreate():注册广播调用Context的registerReceiver()方法
onDestory():解除注册调用Context的unregisterReceiver()方法
public class MainActivity extends AppCompatActivity {
private MyReceiver receiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
receiver=new MyReceiver();
regeister();//注册
}
private void regeister() {
//创建过滤器
IntentFilter intentFilter=new IntentFilter();
//调频
intentFilter.addAction("android.bawei.action.customer");
//注册
registerReceiver(receiver,intentFilter);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);//解除注册
}
}
(4)发送方:sendBroad(Intent意图对象
public void customer(View view) {
Intent intent=new Intent();
intent.setAction("com.hahaha");
Bundle bundle=new Bundle();
bundle.putString("msg","发送广播");
intent.putExtras(bundle);
sendBroadcast(intent);
}
四.广播的分类:
1. 无序广播:sendBroadcast()
2.有序广播:sendOrderBroadcast()
当发送的是有序广播的时候,优先级越高的接受者越先接收到广播,可以调用abortBroadCast()中断广播,不让其他人接受广播。
3.粘性广播:sendStickyBroadcast()
将之前广播发送方发送的消息存储起来,普通广播就不能接受之前发过的消息
五.获取系统广播:动态注册
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button button;
private MyReceiver myReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button1);
button.setOnClickListener(this);
//创建一个
myReceiver = new MyReceiver();
//添加广播过滤器
IntentFilter intentFilter = new IntentFilter();
//添加action
intentFilter.addAction(BroadcastConst.ACTION);
//注册
registerReceiver(myReceiver,intentFilter);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(myReceiver);//注销广播
}
}
发送一个无序广播
Intent intent = new Intent();
intent.setAction("comhahaha);
Bundle bundle = new Bundle();
bundle.putInt("msg",123);
intent.putExtras(bundle);
sendBroadcast(intent);
发送一个有序广播
Intent intent1 = new Intent();
intent1.setAction("com.hahaha");
sendOrderedBroadcast(intent1,null);
java代码
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private MyReceiver myReceiver;
private Button button1;
private Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button2 = findViewById(R.id.button2);
button2.setOnClickListener(this);
button1 = findViewById(R.id.button1);
button1.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int id = view.getId();
switch (id) {
case R.id.button1:
Intent intent = new Intent();
intent.setAction("com.hahaha");
Bundle bundle = new Bundle();
bundle.putInt("msg",123);
intent.putExtras(bundle);
sendBroadcast(intent);
break;
case R.id.button2:
Intent intent1 = new Intent();
intent1.setAction("com.hahaha");
sendOrderedBroadcast(intent1,null);
break;
default:
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(myReceiver);
}
}