Android:BroadcastReceiver
Broadcast分类
注册方式:
- 动态广播
- 在代码中注册receiver
- 一定要手动在onDestroy()时调用unregisterReveiver()来取消注册
- 静态广播
- 在AndroidManifest.xml中注册receiver(主要是设定action)
动态广播:
1. 在代码中设置receiver、监听的广播类型并进行注册:
1 public class MainActivity extends Activity { 2 private NetworkChangeReceiver receiver; 3 4 @Override 5 protected void onCreate(Bundle savedInstanceState) { 6 super.onCreate(savedInstanceState); 7 setContentView(R.layout.activity_main); 8 9 IntentFilter intentFilter = new IntentFilter(); 10 intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE"); 11 12 receiver = new NetworkChangeReceiver(); 13 registerReceiver(receiver, intentFilter); 14 } 15 16 @Override 17 protected void onDestroy() { 18 unregisterReceiver(receiver);//remember to unregister manually 19 super.onDestroy(); 20 } 21 }
2. 定义receiver的行为:继承BroadcastReceiver并在覆写onReceiver()时定义好接收到广播时处理的逻辑:
1 public class NetworkChangeReceiver extends BroadcastReceiver { 2 @Override 3 public void onReceive(Context context, Intent intent) { 4 ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 5 NetworkInfo networkInfo = manager.getActiveNetworkInfo(); 6 if(networkInfo != null && networkInfo.isAvailable()) 7 Toast.makeText(context, "Network is available.", Toast.LENGTH_SHORT).show(); 8 else 9 Toast.makeText(context, "Network is unavailable.", Toast.LENGTH_SHORT).show(); 10 } 11 }
3. 在AndroidManifest.xml中定义必要的权限,主要<uses-permission>标签位于<manifest>的下一级,即与<application>同级:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
静态广播(以开机启动的广播为例)
1. 定义receiver的行为:
1 public class BootCompleteReceiver extends BroadcastReceiver { 2 @Override 3 public void onReceive(Context context, Intent intent) { 4 Toast.makeText(context, "Boot complete.", Toast.LENGTH_LONG).show(); 5 } 6 }
2. 在AndroidManifest.xml中定义receiver(与<activity>同级,<application>的子元素):
1 <receiver android:name=".MyBroadcast"> 2 <intent-filter android:priority="100"> 3 <action android:name="com.scv.lawrence.MY_BROADCAST"/> 4 </intent-filter> 5 </receiver>
3. 在AndroidManifest.xml中添加相应权限(<application>同级,<manifest>的子元素):
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
接收顺序:
- 标准(Normal broadcast)
- 默认的广播方式,即broadcast发出后,所有receiver几乎在同时接收到。
- 有序(Ordered broadcast)
- receiver根据设定的优先级(priority)来决定broadcast的传递顺序,较高优先级的receiver可以选择拦截该broadcast阻止其向后传播
- 二者区别
- 发送广播时,标准广播调用的是sendBroadcast(),有序广播调用的是sendOrderedBroadcast()
- 接收广播时(在AndroidManifest.xml中注册receiver),标准广播不用在<intent-filter>中设置android:priority属性,有序广播则需要设定一个整数,数值越大priority越高
在代码中发送自定义的标准/无序广播:
1 Button sendBroadcast = (Button) findViewById(R.id.broadcast); 2 sendBroadcast.setOnClickListener(new View.OnClickListener() { 3 @Override 4 public void onClick(View v) { 5 Intent intent = new Intent("com.scv.lawrence.MY_BROADCAST"); 6 sendBroadcast(intent); 7 // sendOrderedBroadcast(intent, null); 8 } 9 });
在AndroidManifest.xml中定义(父元素为<application>,即与<activity>同级):
1 <receiver android:name=".MyBroadcast"> 2 <intent-filter android:priority="100"> 3 <action android:name="com.scv.lawrence.MY_BROADCAST"/> 4 </intent-filter> 5 </receiver>
广播范围:
- 全局广播
- 非LocalBroadcastManager进行发送或接收的广播即为全局广播
- 本地广播
- 发送/接收的broadcast只限于当前app范围内,安全性比全局高
- 与全局广播的区别在于本地广播依靠LocalBroadcastManager进行注册、发送及注销receiver
本地广播:
1. 定义接收到广播的行为:
1 public class LocalBroadcastReceiver extends BroadcastReceiver { 2 @Override 3 public void onReceive(Context context, Intent intent) { 4 Toast.makeText(context, "Get the local broadcast.", Toast.LENGTH_SHORT).show(); 5 } 6 }
2. 和动态广播差不多,区别主要在使用LocalBroadcastManager的实例来进行操作:
1 public class MainActivity extends Activity { 2 private LocalBroadcastManager localBroadcastManager; 3 private LocalBroadcastReceiver localReceiver; 4 5 @Override 6 protected void onCreate(Bundle savedInstanceState) { 7 super.onCreate(savedInstanceState); 8 setContentView(R.layout.activity_main); 9 10 //Local Broadcast 11 localBroadcastManager = LocalBroadcastManager.getInstance(MainActivity.this); 12 13 final Button localBroadcast = (Button) findViewById(R.id.local_broadcast); 14 localBroadcast.setOnClickListener(new View.OnClickListener() { 15 @Override 16 public void onClick(View v) { 17 Intent intent = new Intent("com.scv.lawrence.LOCAL_BROADCAST"); 18 localBroadcastManager.sendBroadcast(intent); 19 } 20 }); 21 22 IntentFilter localIntentFilter = new IntentFilter(); 23 localIntentFilter.addAction("com.scv.lawrence.LOCAL_BROADCAST"); 24 25 localReceiver = new LocalBroadcastReceiver(); 26 localBroadcastManager.registerReceiver(localReceiver, localIntentFilter); 27 } 28 29 @Override 30 protected void onDestroy() { 31 localBroadcastManager.unregisterReceiver(localReceiver); 32 super.onDestroy(); 33 } 34 }
3. 在AndroidManifest.xml中配置permission. (如有必要)