标准广播 | 有序广播 |
---|---|
异步执行,同步接收无法被截断 | 同步执行,中途可以被优先级高的广播截断 |
接收系统广播
1.动态注册
public class MainActivity extends AppCompatActivity {
private IntentFilter intentFilter;
private NetworkChangeReceiver networkChangeReceiver;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intentFilter=new IntentFilter();
intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
//intentFilter.addAction,想要监听什么广播,就在这里添加相应的action。
networkChangeReceiver=new NetworkChangeReceiver();
registerReceiver(networkChangeReceiver,intentFilter);
//创建一个NetworkChangeReceiver并注册,会收到所有值为android.net.conn.CONNECTIVITY_CHANGE的广播
}
class LocalReceiver extends BroadcastReceiver{
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"received local broadcast",Toast.LENGTH_SHORT).show();
}
}
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(networkChangeReceiver);//取消广播接收器的注册
}
class NetworkChangeReceiver extends BroadcastReceiver{
public void onReceive(Context context, Intent intent) {
ConnectivityManager connectivityManager=(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
//ConnectivityManager用于管理网络连接
//getSystemService(Context.CONNECTIVITY_SERVICE)获得实例
NetworkInfo networkInfo=connectivityManager.getActiveNetworkInfo();
//获取网络信息
if(networkInfo!=null&&networkInfo.isAvailable()) {
Toast.makeText(context, "network is availiable", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(context, "network is unavailable", Toast.LENGTH_SHORT).show()}
}
}
然后在AndroidMainfest.xml中获取权限
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcasttest">
....
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
...
2.静态注册实现开机启动
新建一个Broadcast Receiver工具,会自动进行权限管理注册
<receiver
android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.broadcasttest.MY_BROADCAST"/>
</intent-filter>
</receiver>
android:name标签指定接收器名称。
action android:name=“com.example.broadcasttest.MY_BROADCAST”,系统开机后要发出一条广播,指定action名称,让接收器可以接收到。
3.使用intent发送标准广播
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent("com.example.broadcasttest.MY_BROADCAST");
sendBroadcast(intent);
4.发送有序广播
public class AnotherBroadcastReceiver extends BroadcastReceiver{
public voie onReceive(Context context,Intent intent){
Toast.makeText(...).show();
然后在AndroidMainfest.xml中添加接收器
<receiver
android:name=".AnotherBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.broadcasttest.MY_BROADCAST"/>
</intent-filter>
</receiver>
在第一个Broadcast的oncreate中添加:
sendOrderBroadcast(intent,null);
可以使用android:priority属性给广播添加优先级.