1
我有一個MyBroadcastReceiver類實現,我試圖在運行時註冊一個廣播接收器:我的註冊廣播接收機有什麼問題?
String MY_ACTION = "DUMMY";
MyBroadcastReceiver receiver = new MyBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter(MY_ACTION);
Log.i(TAG, "intentFilter: " + intentFilter.getAction(0));
Intent intent = context.registerReceiver(receiver, intentFilter);
Log.i(TAG, "intent returned after registering:" + intent);
Intent queryIntent = new Intent(MY_ACTION);
PackageManager pm = context.getPackageManager();
List<ResolveInfo> resolveInfos = pm.queryBroadcastReceivers(queryIntent, 0);
Log.i(TAG, "list of receivers:" + resolveInfos.toString());
我跑了廣播從亞行外殼:
am broadcast \
-n com.foo.foo.foo/.MyBroadcastReceiver \
-a DUMMY
我也有一些日誌。我在我的onReceive()(在MyBroadcast Receiver裏面實現)。
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "in Broadcasting Receiver anyway");
if (intent != null) {
...
}
}
}
結果是:
- intentFilter.getAction的日誌(0)給出正確的行動
- 意圖的登記後的日誌使空
- 我從日誌中獲得空的接收者列表。
- 而的onReceive從未進入(從日誌中我可以看到 那)
我想有我寄存器部分的一些錯誤? 任何提示/建議將高度讚賞!
謝謝。用MyBroadcastReceiver的代碼更新了問題。 –
在我的回答結尾添加了另一個想法。另外,你的代碼'context.registerReceiver()'中的'context'是什麼?你是從「活動」還是「服務」調用這個或者什麼? –
通過使用context.getApplicationContext()獲取上下文並遍歷它。 –