广播机制:标准广播,有序广播
标准广播:发一条广播,所有广播接收器都会收到这条广播
顺序广播:发一条广播,广播接收器按照优先权,从大到小先后接收这条广播,当优先权大的广播接收器拦截正在传播的该广播,后面的接收器将接收不到信息
广播接收器:可以自由对自己感兴趣的广播进行注册(当有相应的广播发出时,广播接收器就可以接收到该广播,并在内部做处理)
注册广播的方式:
1. 动态注册(在代码中注册)
public class MainActivity extends AppCompatActivity{ private IntentFilter intentFilter; private NetworkChangeReceiver receiver; public void onCreate(Bundle saveInstanceState){ super.onCreate(saveInstanceState); setContentView(R.layout.main); //意图过滤器,用来添加我们要监听的广播 intentFilter = new IntentFilter(); intentFilter.addAction("这里添加我们一条广播"); receiver = new NetworkChangeReceiver(); registerReceiver(receiver,intentFilter);//将广播注册到我们定义的广播接收器 } /** **定义一个广播接收器,实现onReceive(Context context,Intent intent)方法 */ class NetworkReceiver extends BroadcastReceiver{ public void onReceive(Context context,Intent intent){ //接收到系统发来的广播后进行一系列内部操作 } } }
2. 静态注册(在AndroidManifest.xml文件中注册)
通过Android studio创建一个BroadcastReceiver,实现onReceive()方法
在AndroidManifiest.xml文件中,需要注册我们定义的广播接收器
<reveiver android:name=".MyBroadCastReceiver" android:enabled="true" android:exported="true"> <!--添加intent-filter--> <intent-filter> <action android:name="my.example.broadcast" </intent-filter> </receiver>
******************注意:不要在onReceive()方法中添加过多逻辑或耗时操作************************
在做了上述注册之后,当接收器收到一条my.example.broadcast的广播,系统将会执行onReceive()中的方法
比如:我们通过点击button来发送一条广播
public class MainActivity extends BaseActivity{ .... ...... public void onCreate(Bundle saveInstance){ .... Button button = (Button)findViewById(R.id.bt); button.setOnclickListener(new OnClickListener){ public void onClick(View v){ Intent intent = new Intent("my.example.broadcast");
sendBroadcast(intent);///当改为sendOrderedBroadcast(intent,null),会发送有序广播
} } } }
上述代码:当点击button时,会触发广播接收器
有序广播,无序广播,都具有全局性,尽管有序广播可以通过在每个应用中对intent-filter的android:priority=""对我们要监听的广播设置权重,从而在onReceive()中通过abortBroadcast()方法中断广播传送,但是仍然存在安全性问题,可以通过本地广播,来避免问题。
本地广播:主要使用LocalBroadcastManager对广播进行管理
public class LocalBroacastActivity extends AppCompatActivity { private Button bt; private IntentFilter intentFilter; private LocalReceiver localReceiver; private LocalBroadcastManager localBroadcastManager; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_broad); localBroadcastManager = LocalBroadcastManager.getInstance(this); bt = (Button)findViewById(R.id.button); bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent("com.example.broadcasttest.LOCAL"); localBroadcastManager.sendBroadcast(intent); } }); intentFilter = new IntentFilter(); intentFilter.addAction("com.example.broadcasttest.LOCAL"); 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,"receiver",Toast.LENGTH_LONG).show(); } } }
实现强制下线功能