如何使用LocalBroadcastManager?LocalBroadcastManager如谷歌文檔和服務(wù)廣播文檔中所述如何使用/定位?我試圖谷歌它,但沒有可用的代碼開始?文件說如果我想在我的應(yīng)用程序進(jìn)程內(nèi)部進(jìn)行廣播,我應(yīng)該使用它,但我不知道在哪里尋找這個(gè)。任何幫助/評論?更新:我知道如何使用廣播,但不知道如何LocalBroadcastManager在我的項(xiàng)目中使用。
4 回答

慕少森
TA貢獻(xiàn)2019條經(jīng)驗(yàn) 獲得超9個(gè)贊
在接收結(jié)束時(shí):
首先注冊LocalBroadcast Receiver
然后處理onReceive中的傳入意圖數(shù)據(jù)。
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this); lbm.registerReceiver(receiver, new IntentFilter("filter_string")); } public BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (intent != null) { String str = intent.getStringExtra("key"); // get all your data from intent and do what you want } } };
發(fā)送結(jié)束時(shí):
Intent intent = new Intent("filter_string"); intent.putExtra("key", "My Data"); // put your all data using put extra LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
添加回答
舉報(bào)
0/150
提交
取消