3

次のコードを使用しています

AlarmManager service = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(this, AlarmReceiver.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pending = PendingIntent.getBroadcast(this, 0, i,PendingIntent.FLAG_CANCEL_CURRENT);
Calendar cal = Calendar.getInstance();
// Start 30 seconds after boot completed
cal.add(Calendar.SECOND, 30);
//
// Fetch every 30 seconds
// InexactRepeating allows Android to optimize the energy consumption
service.setInexactRepeating(AlarmManager.RTC_WAKEUP ,cal.getTimeInMillis(),AlarmManager.INTERVAL_DAY , pending);

// service.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
// REPEAT_TIME, pending);

私のコードでは、1 日間隔で動作し、AlarmReceiver サービスのブロードキャストを行うことを願っています。

ただし、これはちょうど1か月後に発生することを望んでいます。たとえば、ある人が 1 月 3 日にアプリケーションをインストールすると、次のアラームは 2 月 3 日に発生します。どうすれば1か月の間隔を置くことができますか。

4

1 に答える 1

2

次のコードを使用してください。現在のコードと大きな違いはありません。

AlarmManager service = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(this, AlarmReceiver.class);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pending = PendingIntent.getBroadcast(this, 0, i,PendingIntent.FLAG_CANCEL_CURRENT);
    Calendar cal = Calendar.getInstance();
    // Start 1 month after boot completed
    cal.add(Calendar.MONTH, 1);
    //
    // Fetch every 1 month
    // InexactRepeating allows Android to optimize the energy consumption
    service.setInexactRepeating(AlarmManager.RTC_WAKEUP ,cal.getTimeInMillis(),AlarmManager.INTERVAL_DAY , pending);
于 2013-01-08T09:40:11.007 に答える