广播的两种类型:标准广播,有序广播。
①标准广播:几乎所有的广播都会在同一时刻接受到这条广播信息,无先后顺序。
②有序广播:同一时刻只会有一个广播接受器能够收到这条广播信息,执行完传递(可截断),优先级越高,可以越先收到信息。
注册广播接收器的方式一般有两种:
①动态注册在代码中注册,可以调用registerReceiver(广播接受器,intentFilter)方法注册。
而且得调用unregisterRceiver(广播接收器)取消注册。
动态注册必须要在程序启动之后才能接受到广播。
②静态注册在AndroidManifest.xml中注册,
举个例子,动态注册监听网络变化:
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.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private IntentFilter intentFilter;
private NetworkChangeReceiver networkChangeReceiver;
@Override
//每次动态注册后必须要取消注册,重写onDestroy,调用unregisterReceiver进行取消注册
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(networkChangeReceiver);
}
//声明广播接收器
class NetworkChangeReceiver extends BroadcastReceiver{
@Override
// 每当网络状态发生变化,onReceive()方法会自动执行
public void onReceive(Context context, Intent intent) {
//获取网络状态,需要去AndroidManifest.xml声明权限
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();
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intentFilter = new IntentFilter();
//动态注册
intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
//网络状态发生变化时,系统发出android.net.conn.CONNECTIVITY_CHANGE这条广播
//这里的addAction括号里面正是你所想要监听的广播
networkChangeReceiver = new NetworkChangeReceiver();
//实例化对象(就是你监听后的行为,即将做的事)
registerReceiver(networkChangeReceiver,intentFilter);
//调用registerReceiver()对广播接收器进行注册
}
}
注意:访问网络需要权限,在AndroidManifest.xml中声明权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> //声明权限
举个例子,静态注册实现开机启动:
让程序接受一条开机广播,当收到这条广播时就可以在onReceive()方法里执行相应的逻辑,从而实现开机启动的功能。
一.右击com.example.broadcasttest包-new-Other-Broadcast Receiver。
@Override public void onReceive(Context context, Intent intent) { // TODO: This method is called when the BroadcastReceiver is receiving // an Intent broadcast. Toast.makeText(context,"Boot Complete",Toast.LENGTH_SHORT).show(); }
二.在AndroidManifest.xml中声明权限和注册并选择监听的广播
//声明权限 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name=".BootCompleteReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> // 监听开机广播 </intent-filter> </receiver>
发送自定义广播
一.发送标准广播
1.新建一个广播接收器
Override public void onReceive(Context context, Intent intent) { // TODO: This method is called when the BroadcastReceiver is receiving // an Intent broadcast. Toast.makeText(context,"received in MyBroadReceiver", Toast.LENGTH_SHORT).show(); }2.接收指定的广播
<receiver android:name=".MyBroadcastReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="com.example.broadcasttest.MY_BROADCAST"/>//接受此广播 </intent-filter> </receiver>
3.发送广播(这里用按钮来触发)
Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent ("com.example.broadcasttest.MY_BROADCAST"); //Intent 里面是要你发出来的广播 sendBroadcast(intent); } });
二.发送有序广播
1.使用sendOrderedBroadcast进行发送
Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent ("com.example.MyBroadcastReceiver.MY_BROADCAST"); //Intent 里面是要你发出来的广播 sendOrderedBroadcast(intent,null);//发送有序广播 } });
2.设置优先级
<receiver android:name=".MyBroadcastReceiver" android:enabled="true" android:exported="true"> <intent-filter android:priority="100"> <action android:name="com.example.MyBroadcastReceiver.MY_BROADCAST"/>//接受此广播 </intent-filter> </receiver>3.终止广播
public void onReceive(Context context, Intent intent) { // TODO: This method is called when the BroadcastReceiver is receiving // an Intent broadcast. Toast.makeText(context,"received in MyBroadReceiver",Toast.LENGTH_SHORT).show(); abortBroadcast(); //终止广播 }
本地广播
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.util.Log; 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 view) { Intent intent = new Intent ("com.example.MyBroadcastReceiver.MY_BROADCAST"); //Intent 里面是要你发出来的广播 localBroadcastManager.sendBroadcast(intent); // 发送本地广播 } }); intentFilter = new IntentFilter(); intentFilter.addAction("com.example.MyBroadcastReceiver.MY_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 local broadcast",Toast.LENGTH_SHORT).show(); } } }