问题
I've got this code:
private AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
private PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, new Intent("my action-name"), 0);
alarmManager.setInexactRepeating((int)AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + autoUpdateIntervalInMinutes * 60 * 1000, autoUpdateIntervalInMinutes * 60 * 1000, alarmIntent);
But I would like to change this for LocalBroadcastManager. Is this possible?
回答1:
No, it is not possible, because LocalBroadcastManager
is only for your own process, and AlarmManager
's backend runs in a different process. That is why there is no way to create a PendingIntent
that works with LocalBroadcastManager
.
回答2:
But you could register a BroadcastReceiver which basically converts the "Global" Broadcast into a LocalBroadcast:
public class AutoUpdateBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = AutoUpdateBroadcastReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, ".onReceive");
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
lbm.sendBroadcast(intent);
}
}
来源:https://stackoverflow.com/questions/12113103/can-i-use-alarmmanager-with-localbroadcastmanager-on-android