Android广播机制
2017-12-10 本文已影响0人
Mason_Xu
广播机制
简介
- 标准广播
- 完全异步执行的广播
- 同一时刻多个接收器收到消息
- 不能阻断
- 有序广播
- 同步执行的广播
- 同一个时刻只有一个接收器接收消息,广播接收器逻辑执行完毕后,继续传递.
- 可以中间阻断
接收系统广播
开机,电量,时间等系统广播
动态祖册监听网络变换
- 动态注册 ----- 在代码中注册
- 静态注册 ----- 在AndroidManifest.xml中注册
-
如何创建一个广播接收器?
- 新建一个类,继承自BroadcastReceive
- 重写父类onReceive()方法
-
新建Broadcast项目.修改MainActivity代码
- 定义内部类 NetworkChangeReceiver ,继承自BroadcastReceiver
- 重写onReceive()方法. ------ 每当网路状态发生变换时,Toast就会提醒
- 观察onCreate()方法,首先创建一个IntentFilter的实例,添加android.net.conn.CONNECTIVITY_CHANGE的action ---- 当网络发生变化时,系统发出的一条值为android.net.conn.CONNECTIVITY_CHANGE的广播,也就睡我们的广播接收器想要监听的广播.
- 创建一个NetworkChangeReceiver的实例,然后调用registerReceiver()方法进行注册,将NetworkChangeReceiver的实例和IntentFilter的实例都传进去.这样NetworkChangeReceiver就会收到所有值为android.net.conn.CONNECTIVITY_CHANGE的广播,也就实现了监听网络变化的功能
- 最后要注意,动态注册的广播接收器一定要取消注册才行,这里我们在onDestroy()方法中调用unregisterReceiver()方法来实现.
public class MainActivity extends AppCompatActivity {
private IntentFilter intentFilter;
private NetworkChangeReceiver networkChangeReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intentFilter = new IntentFilter();
intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
networkChangeReceiver = new NetworkChangeReceiver();
registerReceiver(networkChangeReceiver, intentFilter);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(networkChangeReceiver);
}
class NetworkChangeReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "network changes" , Toast.LENGTH_SHORT).show();
}
}
}
- 修改代码,使能准确的告诉用户网络状态
- 在onReceive()方法中,首先通过getSystemService()方法得到ConnectivityManager的实例,这是一个系统服务类,专门用于管理网络连接的.
- 然后调用他的getActiveNetworkInfo()方法可以得到NetworkInfo的实例,接着调用NetworkInfo的isAvailable()方法,就可以判断出是否有网络了.
- 最后Toast提示
- 添加敏感操作权限
public class MainActivity extends AppCompatActivity {
...
class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.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.xml配置文件添加权限 --- ide也许自动添加,需要 alt-enter
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mason.broadcasttest">
// 访问网络状态权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
...
</manifest>
静态注册实现开机启动
- Android studio 有快捷方式可以快速创建一个广播接收器
- 在broadcasttest包下邮件new->other->Broadcast Receiver
- 新建BootCompleteReceiver
- 属性 exported表示是否允许这个广播接收器接收本程序以外的广播 enabled表示是否启用这个广播接收器
- Toast提示
- 新建button,用于点击事件
- 静态的广播接收器一定要在AndroidManifest.xml文件中注册才可以使用 --- 由于我们使用快捷方式创建的,IDE已经自动完成注册 -- 属性和name
- 对AndroidManifest.xml进行修改,使它能够收到开机广播
- 在
<intent-filter>
标签中添加相应的action,开机的action是android.intent.action.BOOT_COMPLETED,同时声明监听开机广播权限<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
public class BootCompleteReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Boot Complete", Toast.LENGTH_SHORT).show();
}
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mason.broadcasttest">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
...
// 注册静态广播接收器
<receiver
android:name=".BootCompleteReceiver"
android:enabled="true"
android:exported="true"></receiver>
</application>
</manifest>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mason.broadcasttest">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
// 注册监听系统开机广播
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
...
// 注册静态广播接收器
<receiver
android:name=".BootCompleteReceiver"
android:enabled="true"
android:exported="true">
// 注册监听系统开机广播
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
</manifest>
发送自定义广播
发送标准广播
- 新建 MyBroadReceiver,收到则弹出Toast
- 对AndroidManifest进行修改,修改name
.MyBroadReceiver
和action 为com.example.broadcasttest.MY_BROADCAST
- 添加鼠标监听
- 首先构建出一个Intent对象,并把要发送的广播的值传入
- 然后调用Context的sendBroadcast()方法将广播发送出去.这样监听
com.example.broadcasttest.MY_BROADCAST
这条广播的广播接收器就会收到消息.
- 对AndroidManifest进行修改,修改name
- 此时就是一条标准广播
public class MyBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"received in MyBroadReceiver",Toast.LENGTH_SHORT).show();
}
}
<receiver
// 注意name
android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
// 注册监听
<intent-filter>
<action android:name="com.example.broadcasttest.MY_BROADCAST"/>
</intent-filter>
</receiver>
添加button
public class MainActivity extends AppCompatActivity {
...
@Override
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);
}
});
...
}
...
}
请注意,在Android8.0上,静态注册已经很多不能使用!
发送有序广播
其他程序接收到
public class AnoBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"received in AnoBroadcastReceiver",Toast.LENGTH_SHORT).show();
}
}
<receiver
android:name=".AnoBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.broadcasttest.MY_BROADCAST"/>
</intent-filter>
</receiver>
有序广播
sendOrderedBroadcast(intent,null);
- 设置优先级
- 设置截断
<receiver
android:name=".BroadcastReceiver"
android:enabled="true"
android:exported="true">
// 设置优先级
<intent-filter android:priority="100">
<action android:name="com.example.broadcasttest.MY_BROADCAST"/>
</intent-filter>
</receiver>
public class BroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"received in AnoBroadcastReceiver",Toast.LENGTH_SHORT).show();
//设置截断
abortBroadcast();
}
}
本地广播
- 避免垃圾广播和安全问题,使用本地广播
- 使用LocalBroadcastManager来对广播进行管理
- 通过LocalBroadcastManager的getInstance()方法得到它的一个实例
- 然后在注册的时候调用的是LocalBroadcastManager的registerReceiver()方法
- 在发送广播的时候调用LocalBroadcastManager的sendBroadcast()方法
public class MainActivity extends AppCompatActivity {
private IntentFilter intentFilter;
private LocalReceiver localReceiver;
private LocalBroadcastManager localBroadcastManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
localBroadcastManager = LocalBroadcastManager.getInstance(this); // 获取实例
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); // 发送本地广播
}
});
intentFilter = new IntentFilter();
intentFilter.addAction("com.example.broadcasttest.LOCAL_BROADCAST");
localReceiver = new LocalReceiver();
localBroadcastManager.registerReceiver(localReceiver,intentFilter); // 注册本地广播监听器
}
@Override
protected void onDestroy() {
super.onDestroy();
localBroadcastManager.unregisterReceiver(localReceiver);
}
...
class LocalReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"received LocalBroadcast",Toast.LENGTH_SHORT).show();
}
}
}
这是我学习Android的笔记,参考书<<第一行代码-第二版>>,感谢郭霖大神
如有不足之处,可以一起讨论
附上郭霖的blog http://blog.csdn.net/guolin_blog