0

次のコードを使用して、20秒ごとにアラームを鳴らしています

public class SchedulerSetupReceiver extends BroadcastReceiver {
private static final String APP_TAG = "LOG: ";


private static final int EXEC_INTERVAL = 20 * 1000;

@Override
public void onReceive(final Context ctx, final Intent intent) {
    Log.d(APP_TAG, "SchedulerSetupReceiver.onReceive() called");
    AlarmManager alarmManager = (AlarmManager) ctx
            .getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(ctx, SchedulerEventReceiver.class); 
    PendingIntent intentExecuted = PendingIntent.getBroadcast(ctx, 0, i,
            PendingIntent.FLAG_CANCEL_CURRENT);
    Calendar now = Calendar.getInstance();
    now.add(Calendar.SECOND, 20);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
            now.getTimeInMillis(), EXEC_INTERVAL, intentExecuted);
}

}

コードは期待どおりに機能し、LogCat で 20 秒ごとにログ メッセージを受け取ります。

毎日午前 10 時にアラームが鳴るように設定したいと思います。ただし、アラームは発生しません。エミュレータの時刻を午前 10 時 (例: 午前 9 時 59 分) より前に変更し、コードを実行しました。ただし、アラームは午前 10:00 に発生しません。アラームのIDも設定しました。エミュレータの日付も明日に変更しました。アラームはまったく発生しません。

これが事実である理由について何か理由はありますか?

public class SchedulerSetupReceiver extends BroadcastReceiver {
private static final String APP_TAG = "LOG: ";
private static final int ALARM_ID_2000 = 2000;

//private static final int EXEC_INTERVAL = 20 * 1000;

@Override
public void onReceive(final Context ctx, final Intent intent) {
    Log.d(APP_TAG, "SchedulerSetupReceiver.onReceive() called");
    AlarmManager alarmManager = (AlarmManager) ctx
            .getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(ctx, SchedulerEventReceiver.class);       
    PendingIntent intentExecuted = PendingIntent.getBroadcast(ctx, ALARM_ID_2000, i,
            PendingIntent.FLAG_CANCEL_CURRENT);

    Calendar cal = new GregorianCalendar();
    cal.setTimeInMillis(System.currentTimeMillis());

    Calendar updateTime = new GregorianCalendar();
    //we want to set a daily alarm at 10:00AM
    updateTime.add(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR));
    updateTime.set(Calendar.HOUR_OF_DAY,10);
    updateTime.set(Calendar.MINUTE,0);
    updateTime.set(Calendar.SECOND,0);
    updateTime.set(Calendar.MILLISECOND,0);
    updateTime.set(Calendar.DATE,cal.get(Calendar.DATE));
    updateTime.set(Calendar.MONTH,cal.get(Calendar.MONTH));

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
            updateTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, intentExecuted);
}

古いコードに切り替えましたが、起動時にまだアラームが発生しません。私は次のことを試しました:

  1. ADBを殺す
  2. 日食を再開する
  3. 新しいエミュレータ

これは昨日の 20 秒のアラームで機能していましたが、現在はそのコードでさえ機能しません。

4

2 に答える 2

0

問題はコードではなく、エミュレーターにありました。デバイス ビューでアプリのプロセスが表示されませんでした。エミュレーターの最初の起動時にアラームが発生することを期待していましたが、エミュレーターを再起動する必要がありました。つまり、再起動を模倣するために、エミュレーター ウィンドウからデバイスのオンとオフを切り替える必要がありました。

エミュレータが「再起動」すると、期待どおりにアラームが発生します。

于 2013-03-13T14:39:09.543 に答える