我可以在Android上使用AlarmManager和LocalBroadcastManager吗?

时间:2012-08-24 16:19:01

标签: android alarmmanager localbroadcastmanager

我有这段代码:

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);

但是我想为LocalBroadcastManager改变它。这可能吗?

2 个答案:

答案 0 :(得分:10)

不,这是不可能的,因为LocalBroadcastManager仅适用于您自己的流程,AlarmManager的后端运行在不同的流程中。这就是为什么无法创建适用于PendingIntent的{​​{1}}。

答案 1 :(得分:4)

但是您可以注册一个BroadcastReceiver,它基本上将“全局”广播转换为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);
  }
}