-1

PendingIntent.getService()は、IntentService onHandleIntent()を指定された時間前に実行します。PendingIntent.getService()アラーム予定時刻前にIntentService onHandleIntentを呼び出す

これはコードです。

class MakeAlarm { 

public static void scheduleAlarm(Context context) { 

    AlarmManager manager = ....; 

    boolean enabled = // Some determination logic ; 

    //Intent to trigger 
    Intent intent = new Intent(context, ReminderService.class); 

    PendingIntent operation = PendingIntent 
      .getService(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); 

    if (enabled) { 
     //Gather the time preference 
     Calendar startTime = // Calendar instance with time set 

     //Start at the preferred time 
     //If that time has passed today, set for tomorrow 
     if (Calendar.getInstance().after(startTime)) { 
      startTime.add(Calendar.DATE, 1); 
     } 

     Log.d(TAG, "Scheduling reminder alarm"); 
     manager.setInexactRepeating(
       AlarmManager.RTC, 
       startTime.getTimeInMillis(), 
       AlarmManager.INTERVAL_DAY, 
       operation 
     ); 
    } else { 
     Log.d(TAG, "Disabling reminder alarm"); 
     manager.cancel(operation); 
    } 
    } 
} 

これが呼び出されているIntentServiceさ:アラームスケジュールは完璧

public class ReminderService extends IntentService { 

    private static final String TAG = ReminderService.class.getSimpleName(); 

    public ReminderService() { 
     super(TAG); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     Log.d(TAG, "reminder event triggered"); 

     //Present a notification to the user 
     NotificationManager manager = 
      (NotificationManager) 
getSystemService(Context.NOTIFICATION_SERVICE); 

     Notification note = new NotificationCompat.Builder(this) 
       .setContentTitle(getString(R.string.notification_title)) 
       ... 
       .build(); 

     manager.notify(NOTIFICATION_ID, note); 
    } 
} 

取り組んでいるが、私はアラームをスケジュールする際に瞬時に通知を取得しています。ログから、IntentService onHandleIntentが即座に起動されることが示されます。

+0

あなたのstartTimeは過去のようですね。それが正しいことを確認してください –

+0

私のstartTimeはいつも将来です。 – Ehbraheem

+0

あなたの開始時刻がどういうことが分かりますか?いくつかの例を挙げてください。ロギング? –

答えて

0

時間を(今日の日付+ 1)に設定する必要があります。これを試して。

startTime.set(Calendar.getInstance().get(Calendar.YEAR), 
       Calendar.getInstance().get(Calendar.MONTH), 
       Calendar.getInstance().get(Calendar.DATE)); 

startTime.add(Calendar.DATE, 1); 
+0

実際に 'SimpleDateFormat'を使用しています – Ehbraheem

+0

SimpleDateFormatは日付表示を書式設定するためのものですが、いくつかのDateオブジェクトで作業する必要がありますか? – Awesome

関連する問題