问题
I have a BroadcastReceiver
inside a service:
public class NotificationClickService extends Service {
private static final String DEBUG_TAG = "NotificationClickService";
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
registerReceiver(NotificationClickReceiver, new IntentFilter(DownloadManager.ACTION_NOTIFICATION_CLICKED));
}
@Override
public void onDestroy() {
unregisterReceiver(NotificationClickReceiver);
}
BroadcastReceiver NotificationClickReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(DEBUG_TAG, "NotificationClickReceiver: onReceive CALLED");
Intent i = new Intent(android.app.DownloadManager.ACTION_VIEW_DOWNLOADS);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_SINGLE_TOP |
Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
context.startActivity(i);
}
};
}
This brings the system Download Manager to the front.
On my phone I'm running CyanogenMod 10.1 based on JellyBean.
But...
As soon as the system app CMupdater starts:
- if CMupdater is currently running, it is called from my service's BroadcastReceiver, instead of the
DownloadManager
; - if CMupdater is not running, but has ran at least once, my receiver is not called at all.
It works again if I reboot and don't run the updater. All tested also on my tablet with the corresponding CyanogenMod 10.1 version.
This is from the CM's receiver:
package com.cyanogenmod.updater.receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import com.cyanogenmod.updater.UpdatesSettings;
public class NotificationClickReceiver extends BroadcastReceiver{
private static String TAG = "NotificationClickReceiver";
@Override
public void onReceive(Context context, Intent intent) {
// Bring the main app to the foreground
Intent i = new Intent(context, UpdatesSettings.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP |
Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
context.startActivity(i);
}
}
and from its manifest:
<receiver android:name="com.cyanogenmod.updater.receiver.NotificationClickReceiver">
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED"/>
<category android:name="android.intent.category.HOME"/>
</intent-filter>
</receiver>
android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED
is the constant value for the DownloadManager.ACTION_NOTIFICATION_CLICKED
intent I used.
回答1:
While I'm still having trouble deciphering your question, I can't see how your receiver would have ever been called. You are missing the category registration in your intent filter.
Try:
IntentFilter filter = new IntentFilter(DownloadManager.ACTION_NOTIFICATION_CLICKED);
filter.addCategory(Intent.CATEGORY_HOME);
registerReceiver(NotificationClickReceiver, filter);
That should ensure that you always receive the same broadcasts as the manifest registered receiver.
回答2:
All matching BroadcastRecievers
will be notified, with their appropriate Context
. Note that this can happen in two ways:
If a Receiver is locally created in code and registered, inside some object, it will be called only if that object is around.
If an Reciever is declared in manifest, that object will be created and
onReceive()
method will be called.
So, you can have Service class implement BroadCastReciever
, declare it in Manifest file, and start your service form its onRecieve()
;
来源:https://stackoverflow.com/questions/15068353/is-it-possible-that-two-broadcastreceiver-from-two-apps-based-on-the-same-broad