1.广播接收者是什么:就是用来接收Android系统和自定义的广播的一个Android组建,是四大组件之一,按注册方式分为动态广播和静态广播,按接收的顺序分为有序广播和无序广播。
注意不要在onReceiver不能进行过多的逻辑和任何的耗时操作,在广播接收者中是不允许开启线程的,当onReceiver运行过长时间而没有结束,程序机会报错。所以广播接收者一般用来打开其他的组件。
2.动态注册广播:和隐式启动activity非常的类似
public class BroadcastReceiverDynamicActivity extends AppCompatActivity {
private IntentFilter filter;
private NetworkChangeReceiver networkChangeReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_broadcast_receiver_dynamic);
filter = new IntentFilter();
filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
networkChangeReceiver = new NetworkChangeReceiver();
registerReceiver(networkChangeReceiver, filter);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(networkChangeReceiver);
}
class NetworkChangeReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connectivityManager= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info=connectivityManager.getActiveNetworkInfo();
if (info!=null&&info.isAvailable()){
Toast.makeText(BroadcastReceiverDynamicActivity.this,"网络连上了",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(BroadcastReceiverDynamicActivity.this,"网络断开了",Toast.LENGTH_SHORT).show();
}
}
}
}
3.静态注册广播:直接在AndroidManifest.xml文件中注册并添加对应的action,以及权限。
public class BootBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"开机了",Toast.LENGTH_SHORT).show();
}
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.firstlinecode">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
...
<receiver
android:name=".fourComponents.broadcastReceiver.BootBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
</manifest>
4.自定义广播:定义一个广播接收者,action自定义,然后在activity中sendBroadcast。
Button button= (Button) findViewById(R.id.triggerBroadcastReceiver);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent("com.firstlinecode.fourComponents.broadcastReceiver.MyBroadcastReceiver");
sendBroadcast(intent);
}
});
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"接收到了自定义的广播",Toast.LENGTH_SHORT).show();
}
}
<receiver
android:name=".fourComponents.broadcastReceiver.MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.firstlinecode.fourComponents.broadcastReceiver.MyBroadcastReceiver" />
</intent-filter>
</receiver>
发送有序广播
Intent intent=new Intent("com.firstlinecode.fourComponents.broadcastReceiver.MyBroadcastReceiver");
// sendBroadcast(intent);
sendOrderedBroadcast(intent,null);
设置广播接收者优先级
<intent-filter android:priority="100">
<action android:name="com.firstlinecode.fourComponents.broadcastReceiver.MyBroadcastReceiver" />
</intent-filter>
高优先级的广播接收者可以截断有序广播
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"接收到了自定义的广播",Toast.LENGTH_SHORT).show();
abortBroadcast();
}
5.本地广播:就是使用了localBroadcastManager来动态注册和发送的广播。
public class BroadcastReceiverLocalActivity extends AppCompatActivity {
private LocalBroadcastManager localBroadcastManager;
private LocalReceiver localReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_broadcast_receiver_local);
localBroadcastManager = LocalBroadcastManager.getInstance(this);
Button button= (Button) findViewById(R.id.localReceiver);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent("com.firstlinecode.fourComponents.broadcastReceiver.LocalBroadcastReceiver");
localBroadcastManager.sendBroadcast(intent);
}
});
IntentFilter filter=new IntentFilter();
filter.addAction("com.firstlinecode.fourComponents.broadcastReceiver.LocalBroadcastReceiver");
localReceiver = new LocalReceiver();
localBroadcastManager.registerReceiver(localReceiver,filter);
}
@Override
protected void onDestroy() {
super.onDestroy();
localBroadcastManager.unregisterReceiver(localReceiver);
}
class LocalReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"接收到了本地广播",Toast.LENGTH_SHORT).show();
}
}