本地广播LocalBroadcastManager的使用:1.为什么使用(1)可以明确的知道正在发送的广播不会离开我们的程序,因此不会担心机密数据泄露.(2)其他的程序无法将广播发送到我们程序的内部,因此不需要担心有安全漏洞的隐患(3)发送本地广播比发送全局广播高效2.使用方法LocalBroadcastManager mLocalBroadcastManager = LocalBroadcastManager.getInstance(this); //获取LocalBroadcastManager实例注册本地广播IntentFilter mIntentFilter = new IntentFilter();mIntentFilter.addAction("com.example.broadcast.LOCAL_BROADCAST");LocalReceiver mLocalReceiver = new LocalReceiver();mLocalBroadcastManager.registerReceiver(mLocalReceiver, mIntentFilter);发送本地广播Intent intent = new Intent();mLocalBroadcastManager.sendBroadcast(intent);取消本地广播mLocalBroadcastManager.unregisterReceiver(mLocalReceiver);实现BroadcastReceiverclass LocalReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (null != intent && intent.getAction().equalsIgnoreCase("com.example.broadcast.LOCAL_BROADCAST")) { // Do Something } }}
新闻热点
疑难解答