LocalBroadcastManager使用簡析

LocalBroadcastManager屬於v4包裏面的類。

android.support.v4.content.LocalBroadcastManager

developer裏面OverView是這麼寫的:
Helper to register for and send broadcasts of intents to local objects within your peocess.this is has a number of advantages over sending global broadcasts with sendBroadcast(Intent):
.You know that the data you are braodcasting 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 effcient than sending a global broadcast through the system.
通過粗體部分能看出來這是一個在你app內部通信的方法,在app內部通信上和全局的boradcast相比有優勢。體現爲:
1.因爲是內部通信不會離開該app,所以不用擔心數據泄露出去。
2.比全局boradcast更有效率.
3.內部方法不用擔心安全問題.
使用也很簡單:

LocalBroadCastManager.getInstance(context).registerReceiver(receiver,intentfilter);
LocalBroadCastManager.getInstance(context).sendBroadcast(Intent);
LocalBroadCastManager.getInstance(context).unregisterReiver(receiver);
發佈了55 篇原創文章 · 獲贊 4 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章