BroadcastReceiver:广播接收者,接收发送过来的广播,本质是全局监听器。广播作为Android一种通信方式,用途十分广泛。例如:开机启动后会接收到启动服务的广播,电量变化时会接收电量低及时保存的广播提示,网络变化会接收及时提示和保存的提示,检测SD状态广播等等。
BroadcastReceiver的生命周期:
实现BroadcastReceiver的实例非常简单,继承BroadcastReceiver会调用onReceive(),所有的操作直接在onReceive()中运行。执行完之后,BroadcastReceiver直接销毁。该方法直接运行在UI线程中,大概只有10s左右,所以不可做耗时操作。
如何注册:
首先自定义一个BroadcastReceiver对象,继承BroadcastReceiver
public class MyReceiver extends BroadcastReceiver {
private static final String Tag = "Myreceive";
@Override
public void onReceive(Context context, Intent intent) {
String msg = intent.getStringExtra("msg");
Log.i(Tag,"Myreceive:"+ msg);
}
}
注册:
1.静态注册:在AndroidManifest.xml中实现
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="MY_BROADCAST"/><!-- 隐式发送,用来匹配Intent-->
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
补充(隐式跳转):
1.一个Intent可以有多个category目录,但至少会有一个默认的;
2.只有Intent的所有category都匹配上了,Activity才能接受这个Intent。
所以为什么要加category 这个默认方式了。
MianActivity代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt_send = (Button) findViewById(R.id.bt_send);
bt_send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("android.intent.action.MY_BROADCAST");//在配置文件中的action目录做的配置
intent.putExtra("msg","hello World"); sendBroadcast(intent);
}
}
);
}
添加了一个发送按钮,用来发送广播
结果:
2.动态注册
MyReceiver myReceiver = new MyReceiver();
IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("android.intent.action.MY_BROADCAST");
registerReceiver(myReceiver,intentFilter);//动态注册
结果:
动态注册在程序关闭时是收不到广播的,但是静态可以。
根据广播的发送方式可以分为:
1.Normal Broadcast普通广播
2.System Broadcast系统广播
3.Ordered Broadcast有序广播
4.Local BroadcastApp内部广播
5.Sticky Broadcast粘性广播(5.0不在推荐)
1.普通广播:
建立了三个自定义广播类
MyReceiver
public class MyReceiver extends BroadcastReceiver {
private static final String Tag = "Myreceive";
@Override
public void onReceive(Context context, Intent intent) {
String msg = intent.getStringExtra("msg");
Log.i(Tag, "Myreceive:" + msg);
}
}
FirstReceiver
public class FirstReceiver extends BroadcastReceiver {
private static final String Tag = "FirstReceiver";
@Override
public void onReceive(Context context, Intent intent) {
String msg = intent.getStringExtra("msg");
Log.i(Tag, "FirstReceiver:" + msg);
}
}
SecondReceiver
public class SecondReceiver extends BroadcastReceiver {
private static final String Tag = "SecondReceiver";
@Override
public void onReceive(Context context, Intent intent) {
String msg = intent.getStringExtra("msg");
Log.i(Tag, "SecondReceiver:" + msg);
}
}
MainActivity:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt_send = (Button) findViewById(R.id.bt_send);
bt_send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("android.intent.action.MY_BROADCAST");
intent.putExtra("msg","hello World");
sendBroadcast(intent);
}
}
);
}
}
在配置文件中添加相应的配置
结果:
04-06 08:01:34.308 30288-30288/com.appress.gerber.broadcastreceiver I/Myreceive: **Myreceive:hello World**
04-06 08:01:34.308 30288-30288/com.appress.gerber.broadcastreceiver I/FirstReceiver: **FirstReceiver:hello World**
04-06 08:01:34.308 30288-30288/com.appress.gerber.broadcastreceiver I/SecondReceiver: **SecondReceiver:hello World**
都接收到了消息。
有序广播:
使用sendOrderedBroadcast()方法,优先级-1000到1000
在配置文件中配置优先级
<receiver android:name=".MyReceiver">
<intent-filter android:priority="10"><!--优先级10-->
<action android:name="android.intent.action.MY_BROADCAST"/><!-- 隐式发送,用来匹配Intent-->
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
<receiver android:name=".FirstReceiver">
<intent-filter android:priority="80"><!--优先级80-->
<action android:name="android.intent.action.MY_BROADCAST"/><!-- 隐式发送,用来匹配Intent-->
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
<receiver android:name=".SecondReceiver">
<intent-filter android:priority="-20"><!--优先级-20-->
<action android:name="android.intent.action.MY_BROADCAST"/><!-- 隐式发送,用来匹配Intent-->
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
MainActivity:没有做设置权限,直接null,如果设置权限只能接收相同权限的广播。
sendOrderedBroadcast(intent,null);
结果:优先级FirstReceiver,Myreceive,SecondReceiver
04-06 08:18:36.716 14590-14590/com.appress.gerber.broadcastreceiver I/FirstReceiver: **FirstReceiver:hello World**
04-06 08:18:36.716 14590-14590/com.appress.gerber.broadcastreceiver I/Myreceive: **Myreceive:hello World**
04-06 08:18:36.716 14590-14590/com.appress.gerber.broadcastreceiver I/SecondReceiver: **SecondReceiver:hello World**
Local Broadcast:App内部广播,说明广播可以跨进程之间通信。(自己不怎么了解)
System Broadcast:Android系统内置了多个系统广播,只要涉及到基本操作的基本上都带有广播