接受系统广播
动态注册
在Java代码中指定IntentFilter,然后想监听什么广播就添加对应的Action。动态注册的广播,一定要调用unregisterReceiver()方法取消注册。
下面用动态注册监听网络变化
在AndroidManifest.xml中添加权限:
Activity的Java代码:
publicclassMainActivityextendsAppCompatActivity{privateIntentFilter intentFilter;privateNetworkChangeReceiver networkChangeReceiver;@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);//动态接受网络变化的广播接收器intentFilter =newIntentFilter(); intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE"); networkChangeReceiver =newNetworkChangeReceiver(); registerReceiver(networkChangeReceiver, intentFilter); }@OverrideprotectedvoidonDestroy() {super.onDestroy();//取消动态网络变化广播接收器的注册unregisterReceiver(networkChangeReceiver); }//自定义接受网络变化的广播接收器class NetworkChangeReceiver extends BroadcastReceiver {@OverridepublicvoidonReceive(Context context, Intent intent) { ConnectivityManager connectionManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connectionManager.getActiveNetworkInfo();if(networkInfo !=null&& networkInfo.isAvailable()) { Toast.makeText(context,"network is available", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(context,"network is unavailable", Toast.LENGTH_SHORT).show(); } } }}
静态注册
动态注册需要程序启动之后才能接收广播,静态广播就弥补了这个短板。在AndroidManifest中指定IntentReceiver就可以在程序未启动的情况下接收到广播
下面用静态注册接受开机广播
新建一个广播接收器
publicclassMyBroadcastReceiverextendsBroadcastReceiver{@OverridepublicvoidonReceive(Context context, Intent intent) { Toast.makeText(context,"received in MyBroadcastReceiver", Toast.LENGTH_SHORT).show(); }}
在AndroidManifest.xml中添加权限、注册广播接收器和添加Action
<?xml version="1.0" encoding="utf-8"?>
发送广播
发送标准广播
接收器的Java代码:
publicclassMyBroadcastReceiverextendsBroadcastReceiver{@OverridepublicvoidonReceive(Context context, Intent intent) { Toast.makeText(context,"received in MyBroadcastReceiver", Toast.LENGTH_SHORT).show(); }}
接收器的注册代码:
Activity的Java代码
publicclassMainActivityextendsAppCompatActivity{privateButton button;@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);//发送自定义广播button = (Button) findViewById(R.id.button); button.setOnClickListener(newView.OnClickListener() {@OverridepublicvoidonClick(View v) { Intent intent =newIntent("com.example.broadcasttest.MY_BROADCAST"); sendBroadcast(intent); } }); }@OverrideprotectedvoidonDestroy() {super.onDestroy();//取消动态网络变化广播接收器的注册unregisterReceiver(networkChangeReceiver); } }
发送有序广播
发送有序广播的方法:
sendOrderedBroadcast(intent,null);
设置广播接收器的先后顺序,在AndroidManifest.xml中receiver的intent-filter的priority属性,优先级可选值:-1000~1000
android:priority="100"
使用本地广播
主要是用LocalBroadcastManager来对广播进行管理,提供了发送广播和注册广播接收器的方法如下:
LocalBroadcastManager.getInstance()获得实例
~.registerReceiver()注册广播
~.sendBroadcast()发送广播
~.unregisterReceiver()取消广播
注意事项:
本地广播无法通过静态注册来接收,相比系统全局广播更加高效
在广播中启动活动需要添加FLAG_ACTIVITY_NEW_TASK的标记
在广播中弹出AlertDialog需要添加TYPE_SYSTEM_ALERT
Java代码:
publicclassMainActivityextendsAppCompatActivity{privateIntentFilter intentFilter;privateLocalReceiver localReceiver;privateLocalBroadcastManager localBroadcastManager;@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);//得到本地广播管理器的实例localBroadcastManager = LocalBroadcastManager.getInstance(this);//动态注册本地广播接收器intentFilter =newIntentFilter(); intentFilter.addAction("com.example.broadcasttest.LOCAL_BROADCAST"); localReceiver =newLocalReceiver(); localBroadcastManager.registerReceiver(localReceiver, intentFilter);//发送本地广播Button button = (Button) findViewById(R.id.button); button.setOnClickListener(newView.OnClickListener() {@OverridepublicvoidonClick(View v) { Intent intent =newIntent("com.example.broadcasttest.LOCAL_BROADCAST"); localBroadcastManager.sendBroadcast(intent); } }); }@OverrideprotectedvoidonDestroy() {super.onDestroy();//取消动态网络变化广播接收器的注册localBroadcastManager.unregisterReceiver(localReceiver); } class LocalReceiver extends BroadcastReceiver {@OverridepublicvoidonReceive(Context context, Intent intent) { Toast.makeText(context,"received local broadcast", Toast.LENGTH_SHORT).show(); } }}