1.简介
Android程序可以对自己感兴趣的广播注册,程序只接受注册的广播,而且可以接收来自系统和其他程序的广播。
不要在广播中添加过多逻辑或进行任何的耗时操作,因为在广播接收器中不允许开启线程。当onReceive()方法运行了较长时间而没有结束,程序就会报错。广播接收器一般扮演打开程序其他组件的角色,比如创建一条通知、启动一个服务等。
发送广播使用Intent,接收广播使用广播接收器(Broadcast Receiver)。
- 标准广播:异步执行的广播,所有广播接收器同时接收广播,无先后顺序之分。
- 有序广播:同步执行的广播,按先后顺序一个一个接收广播,一个广播接收执行完后再传给后一个广播接收器,广播接收器可以截断广播。
2. 广播注册方式
2.1 静态注册 在AndroidManifest.xml中注册
可以接收开机启动广播:
BootCompleteReceiver广播接收器:
package com.example.broadcasttest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class BootCompleteReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Boot Complete", Toast.LENGTH_LONG).show();
}
}
注册广播:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.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:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--注册的广播-->
<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>
2.2. 动态注册 在代码中注册
动态注册虽然灵活,但是必须启动程序才能接收广播。
创建一个广播接收器,只需新建一个类,继承BroadcastReceiver类,并重写onReceive()方法就行了。有广播来时,就会执行onReceive()中的方法。
package com.example.broadcasttest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private IntentFilter intentFilter;
private LocalReceiver localReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intentFilter = new IntentFilter();//创建IntentFilter实例
intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");//确定接收的广播种类,这里是网络发生变化的广播
localReceiver = new LocalReceiver();//创建广播接收器实例
registerReceiver(localReceiver, intentFilter); // 注册本地广播监听器,这样LocalReceiver就会收到所有值为android.net.conn.CONNECTIVITY_CHANGE的广播,也就实现了监听网络变化的功能
}
/**
*动态注册的广播必须取消注册
*/
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(localReceiver);//取消注册
}
/**
* 这个是广播类继承了BroadcastReceiver类
*/
class LocalReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//getSystemService()方法得到ConnectivityManager的实例,这是一个系统服务,专门用于管理网络连接的。
ConnectivityManager connectionManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
//getActiveNetworkInfo()方法得到NetworkInfo实例
NetworkInfo networkInfo = connectionManager.getActiveNetworkInfo();
//NetworkInfo的isAvailable()方法用以判断当前是否网络
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中打开权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
3. 自定义广播
3.1 发送标准广播
//定义发送的广播action,这里的com.example.broadcasttest.LOCAL_BROADCAST是自定义的值,只需新建一个广播接收器接收这个值得广播,就能接收到这里发送的广播
Intent intent = new Intent("com.example.broadcasttest.LOCAL_BROADCAST");
sendBroadcast(intent); // 发送本地广播
3.2 发送有序广播
//定义发送的广播action
Intent intent = new Intent("com.example.broadcasttest.LOCAL_BROADCAST");
sendOrderedBroadcast(intent,null); // 发送有序广播广播.第一个参数是intent,第二个参数是与权限有关的字符串,这里传入null
注册广播时需要定义顺序值android:priority:
<receiver
android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="100"><!--这里确定该广播接收器接收广播的优先级,值越大越先接收-->
<action android:name="com.example.broadcasttest.MY_BROADCAST"/>
</intent-filter>
</receiver>
在广播接收器的onReceive()中调用abortBroadcast()发放截断有序广播。:
package com.example.broadcasttest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "received in MyBroadcastReceiver", Toast.LENGTH_SHORT).show();
abortBroadcast();//截断有序广播,这之后的广播接收器就收不到广播
}
}
4 本地广播
只在程序内部传播的广播,避免安全性问题,而且本地广播无法通过静态注册的方式接收。本地广播使用LocalBroadcastManager类来对广播进行管理,并提供了光送广播和注册广播接收器的方法:
package com.example.broadcasttest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
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);//注销本地广播
}
/**
* 这个是广播类继承了BroadcastReceiver类
*/
class LocalReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "received local broadcast", Toast.LENGTH_SHORT).show();
}
}
}