-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