本地广播与全局广播的区别:

 
由于之前的广播都是全局的,所有应用程序都可以接收到,这样就很 容易会引起安全性 的问题,比如说我们发送一些携带关键性数据的广播有可能 被其他的应用程序截获 ,或者其他的程序不停地 向我们的广播接收器里发送各种垃圾广播
为了能够简单地解决广播的安全性问题,Android引入了一套 本地广播机制 ,使用这个机制发出的广播 只能够在应用程序的内部进行传递 ,并且 广播接收器也只能接收来自应用程序发出的广播 ,这样所有的安全性问题就都不存在了。
1注册方式不同
2安全性不同(一个内,一个外)
 
(1)本地广播无法通过静态注册来接收,相比起系统全局广播更加高效
(2)在广播中启动activity的话,需要为intent加入FLAG_ACTIVITY_NEW_TASK的标记,不然会报错,因为需要一个栈来存放新打开的activity。
(3)广播中弹出AlertDialog的话,需要设置对话框的类型为:TYPE_SYSTEM_ALERT不然是无法弹出的。
 
 
本地广播:
 
    源码LocalBroadcastManager实质解析 原理就是通过 Handler 操作 实现收发消息
 
1 调用 localBroadcastManager=LocalBroadcastManager.getInstance()来获得实例
2 调用 localBroadcastManager.registerReceiver()来注册广播
3 调用 localBroadcastManager.sendBroadcast()发送广播
4 调用 localBroadcastManager.unregisterReceiver()取消注册
 
public class LocalBroadcastActivity extends AppCompatActivity {
private LocalBroadcastManager localBroadcastManager ;
private Button sendButton ;
private LocalBroadReceiver localBroadReceiver ;
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout. activity_local_broadcast );
sendButton =findViewById(R.id. sendLocalBroadcast );
initLocalBroadcast();
 
sendButton .setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
send();
}
});
}
 
private void initLocalBroadcast(){
//LocalBroadcastManager实质解析 原理就是通过 Handler 操作 实现收发消息
localBroadcastManager =LocalBroadcastManager. getInstance ( this );
localBroadReceiver = new LocalBroadReceiver();
IntentFilter intentFilter= new IntentFilter();
intentFilter.addAction( "com.blue.cameraxdemo.LocalBroadcastActivity" );
localBroadcastManager .registerReceiver( localBroadReceiver ,intentFilter);
}
 
private void send(){
Intent intent = new Intent( "com.blue.cameraxdemo.LocalBroadcastActivity" );
//发送本地广播
localBroadcastManager .sendBroadcast(intent);
}
 
public class LocalBroadReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast. makeText (LocalBroadcastActivity. this , "收到本地广播" ,Toast. LENGTH_LONG ).show();
}
}
 
@Override
protected void onDestroy() {
super .onDestroy();
if ( null != localBroadcastManager ){
localBroadcastManager .unregisterReceiver( localBroadReceiver );
}
}
}
 
 
全局广播:
 
/**
* 全局广播 demo
* 补充 全局广播全部都能收到 只要注册了相应的广播接收
*/
public class AllBroadcastActivity extends AppCompatActivity {
private AllBroadcastReceiver allBroadcastReceiver ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout. activity_all_broadcast );
 
allBroadcastReceiver = new AllBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction( "com.blue.cameraxdemo.allbroadcast.CONNECTIVITY_CHANGE" );
registerReceiver( allBroadcastReceiver , intentFilter);
 
findViewById(R.id. sendAll ).setOnClickListener(v -> {
sen();
});
}
/**
* 以网络检测广播 做列子
*/
private void sen() {
Intent intent = new Intent();
intent.setAction( "com.blue.cameraxdemo.allbroadcast.CONNECTIVITY_CHANGE" );
sendBroadcast(intent);
}
 
public class AllBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals( "com.blue.cameraxdemo.allbroadcast.CONNECTIVITY_CHANGE" )){
Toast. makeText (context, "网络检测广播" ,Toast. LENGTH_LONG ).show();
}
}
}
}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

你可能感兴趣的:(Android,常用)