[英]Dynamically registered broadcast receiver's onReceive() hook method is not called
This is part of an optional assignment.这是可选作业的一部分。 I have an Activity dynamically registering a broadcast receiver and also starting an IntentService.
我有一个动态注册广播接收器并启动 IntentService 的活动。 The broadcast receiver is supposed to receive the intent from the IntentService after it performs its work.
广播接收器应该在执行其工作后从 IntentService 接收意图。 However, after the IntentService sent the broadcast, the onReceive is not called.
但是,在 IntentService 发送广播后,并没有调用 onReceive。
I was logging the output, and the last log I receive is from the IntentService after it sends the broadcast.我正在记录输出,我收到的最后一个日志来自 IntentService 发送广播后。 After that, I have no idea how to continue debugging.
之后,我不知道如何继续调试。
Please advice.请指教。
What is wrong with my code such that the onReceive() method is not called?我的代码有什么问题以至于没有调用 onReceive() 方法?
EDIT: ANSWER (from HeyAlex)编辑:答案(来自 HeyAlex)
The IntentService was sending broadcast via the LocalBroadcastManager, while the Broadcast receiver started by the Activity was not registered to LocalBroadcastManager instance but the normal dynamic register. IntentService 通过 LocalBroadcastManager 发送广播,而由 Activity 启动的广播接收器没有注册到 LocalBroadcastManager 实例,而是正常的动态注册。 So the broadcast receiver did not pick up on the sent broadcast.
所以广播接收器没有接收到发送的广播。
DownloadAtomFeedService (extending IntentService)下载AtomFeedService(扩展IntentService)
@Override
public void onHandleIntent(Intent intent) {
List<Entry> entries = downloadAtomFeed(uri.toString());
int requestCode = intent.getExtras().getInt(REQUEST_CODE);
sendEntries((ArrayList<Entry>) entries,uri,requestCode);
Log.d(TAG, "onHandleIntent: returning to MainActivity");
}
private void sendEntries(ArrayList<Entry> entries,
Uri url,
int requestCode) {
Bundle bundle = makeReplyBundle(entries,url,requestCode);
Intent intent = new MainActivity().makeBroadcastReceiverIntent();
intent.putExtras(bundle);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
Log.d(TAG, "sendEntries: SENT BROADCAST");
}
private Bundle makeReplyBundle(ArrayList<Entry> entries,
Uri url,
int requestCode) {
Bundle data = new Bundle();
data.putParcelableArrayList(ENTRY_ARRAY_KEY,entries);
data.putInt(REQUEST_CODE,requestCode);
data.putString(FEED_URL,url.toString());
if(entries == null){
data.putInt(DOWNLOAD_RESULTS,Activity.RESULT_CANCELED);
}else data.putInt(DOWNLOAD_RESULTS,Activity.RESULT_OK);
return data;
}
MainActivity主要活动
@Override
protected void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter(DownloadStateReceiver.BROADCAST_ACTION);
DownloadStateReceiver mDownloadStateReceiver = new DownloadStateReceiver();
registerReceiver(mDownloadStateReceiver,intentFilter);
android.util.Log.d(TAG, "onResume: downloadstatereceiver registered");
}
@Override
protected void onPause() {
LocalBroadcastManager.getInstance(getApplicationContext()).unregisterReceiver(mDownloadStateReceiver);
super.onPause();
}
public static Intent makeBroadcastReceiverIntent() {
Intent intent = new Intent().setAction(DownloadStateReceiver.BROADCAST_ACTION);
return intent;
}
public class DownloadStateReceiver extends BroadcastReceiver {
public static final String BROADCAST_ACTION =
"myassignment.activities.BROADCAST";
public DownloadStateReceiver() {
android.util.Log.d(TAG, "DownloadStateReceiver: ");
}
public void onReceive(Context context, Intent intent) {
MainActivity.this.serviceIntentReceived(intent.getExtras());
android.util.Log.d(TAG, "onReceive: BroadcastReceived");
}
}
Problem is that you're registering BroadcastReceiver not by LocalBroadcastManager.问题是您不是通过 LocalBroadcastManager 注册 BroadcastReceiver。 Just make your onResume like:
只需让你的 onResume 像:
@Override
protected void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter(DownloadStateReceiver.BROADCAST_ACTION);
mDownloadStateReceiver = new DownloadStateReceiver();
LocalBroadcastManager.getInstance(this).registerReceiver(mDownloadStateReceiver,intentFilter);
android.util.Log.d(TAG, "onResume: downloadstatereceiver registered");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.