Can i use AlarmManager with LocalBroadcastManager on android?

我是研究僧i 提交于 2019-12-19 05:33:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!