广播机制
广播接收器(Broadcast Receiver)
- 标准广播:是一种完全异步执行的广播,广播一旦发送,所以广播接收器都会立刻同时接收到这条广播
- 有序广播:是一种同步执行的广播,广播发出后,同一时间只有一个广播接收器能够接收到这条广播信息,然后执行,然后继续传播
- 同步执行
- 效率不高
- 有先后顺序,可以截断正在传递的广播
接收系统广播
动态注册监听网络变化
- 定义一个内部类NetworkChangeReceiver,这个类继承自BroadcastReceiver的,并重写onReceive()方法
class NatworkChangeReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "network changes", Toast.LENGTH_SHORT).show();
}
}
- 在onCreate()方法,首先先创建一个IntentFilter的实例,添加action,再创建NatworkChangeReceiver实例,最后调用registerReceiver()方法进行注册
private IntentFilter intentFilter;
private NatworkChangeReceiver natworkChangeReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intentFilter = new IntentFilter();
intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
natworkChangeReceiver = new NatworkChangeReceiver();
registerReceiver(natworkChangeReceiver,intentFilter);
}
- 记得,动态注册的广播接收器一定都要取消注册才行,这里我们是在onDestroy()方法调用unregisterReceiver()方法
静态注册实现开机启动
public class BootCompleteReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Boot Complete", Toast.LENGTH_SHORT).show();
}
}
- 需要在AndroidManifest.xml将广播接收器的类名注册进去。
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
.....
<receiver android:name=".BootCompleteReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
发送标准广播
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "received in MyBroadcastReceiver", Toast.LENGTH_SHORT).show();
}
}
- 在AndroidManifest.xml中对这个广播接收器进行注册
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="com.example.broadcasttest.MY_BROADCAST"/>
</intent-filter>
</receiver>
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);
}
});
发送有序广播
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");
sendOrderedBroadcast(intent,null);
}
});
<receiver android:name=".MyBroadcastReceiver">
<intent-filter android:priority="100">
<action android:name="com.example.broadcasttest.MY_BROADCAST"/>
</intent-filter>
</receiver>
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "received in MyBroadcastReceiver", Toast.LENGTH_SHORT).show();
abortBroadcast();
}
}
使用本地广播
- 获取LocalBroadcastManager实例
localBroadcastManager = LocalBroadcastManager.getInstance(this);
......
class LocalReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "received local broadcast", Toast.LENGTH_SHORT).show();
}
}
intentFilter = new IntentFilter();
intentFilter.addAction("com.example,broadcasttest.LOCAL_BROADCAST");
localReceiver = new LocalReceiver();
localBroadcastManager.registerReceiver(localReceiver,intentFilter);
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.LOCAL_BROADCAST");
localBroadcastManager.sendBroadcast(intent);
}
});
localBroadcastManager.unregisterReceiver(localReceiver);