標籤:android本地廣播 localbroadcastreceiv
- 官方文檔解釋:Helper to register for and send broadcasts of Intents to local objects within your process. This is has a number of advantages over sending global broadcasts with
Context.sendBroadcast(android.content.Intent):
- You know that the data you are broadcasting won‘t leave your app, so don‘t need to worry about leaking private data.
- It is not possible for other applications to send these broadcasts to your app, so you don‘t need to worry about having security holes they can exploit.
- It is more efficient than sending a global broadcast through the system.
意義是:翻譯下,1、你正在發送的資料不會超出你的應用,所以不用擔心泄露應用私人資料。
- 2、其他應用發送的廣播不會發送到你的APP,所以你不用擔心有安全性的攻擊。
- 3、本地廣播比全域廣播更高效
使用方法:非常簡單,該類是採用單例,建立
final LocalBroadcastManager localBroadcastManager=LocalBroadcastManager.getInstance(this);
註冊:
IntentFilter filter=new IntentFilter(); filter.addAction("duanqing.test.localbroadcast.fly"); localBroadcastManager.registerReceiver(new MyBroadCastReceiver(),filter);
發送代碼:
Intent intent=new Intent(); intent.setAction("duanqing.test.localbroadcast.fly"); localBroadcastManager.sendBroadcast(intent);
Android LocalBroadcastManager提高應用安全性