2017-03-09 1 views
0

알림 서비스가 제대로 작동하지만 문제가 생겼습니다.Android 시작 알림 응용 프로그램 시작시 트리거

난 단지 미래에 하루 의 특정 시간에 알림을받을 그러나 나는 잘못

if (time_that_i_set_for_notification < current_time_of_the_day) notification triggers at the boot of my app 

그 상태로, 통지는 다음 날를 실행해야하기 때문에 것으로 나타났습니다 , 내가 앱을 실행하는 순간이 아닙니다.

여기 것들이 작동하게 내 시도이다 :

내가 내 MainActivity에서 이것을 호출에서 onCreate() 메소드 :

private void scheduleNotification(int hour, int minute){ 
    Intent notificationIntent = new Intent(this, NotificationReceiver.class); 
    notificationIntent.putExtra("notifId", notifId); 

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, notifId, notificationIntent, 0); 

// Set the alarm to start at approximately at a time 
     DatePicker datePicker = new DatePicker(this); 
     Calendar calendar = Calendar.getInstance(); 
     calendar.setTimeInMillis(System.currentTimeMillis()); 
     calendar.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth()); 
     if(calendar.getTime().compareTo(new Date()) < 0) calendar.add(Calendar.DAY_OF_MONTH, 1); 
     calendar.set(Calendar.HOUR_OF_DAY, 12); 
     calendar.set(Calendar.MINUTE, 12); 

     AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); 
// With setInexactRepeating(), you have to use one of the AlarmManager interval 
// constants--in this case, AlarmManager.INTERVAL_DAY. 
     alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 
       AlarmManager.INTERVAL_DAY, pendingIntent); 
    } 

나는 if(calendar.getTime().compareTo(new Date()) < 0) calendar.add(Calendar.DAY_OF_MONTH, 1); 추가 나는 결과를 달성 한 것이라고 생각 내가 필요.

감사합니다. 좋은 하루 되세요. 이 도움이

if (calendar.getTimeInMillis() < System.currentTimeMillis()) 
    calendar.setTimeInMillis(calendar.getTimeInMillis() + AlarmManager.INTERVAL_DAY); 

희망 :

답변

0

setInexactRepeating를 호출하기 전에,이 검사를 추가 할 수 있습니다.

0

오케이 거기에 잘못된 체크가 있습니다. 변경하면 문제를 해결할 수 있습니다.

여기에 우리가 간다 : 불필요한해야하기 때문에
Intent notificationIntent = new Intent(this, NotificationReceiver.class); 
    notificationIntent.putExtra("notifId", notifId); 

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, notifId, notificationIntent, 0); 

// Set the alarm to start at approximately at a time 
     DatePicker datePicker = new DatePicker(this); 
     Calendar calendar = Calendar.getInstance(); 
     calendar.setTimeInMillis(System.currentTimeMillis()); 
     calendar.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth()); 
     //if(calendar.getTime().compareTo(new Date()) < 0) calendar.add(Calendar.DAY_OF_MONTH, 1); 
     calendar.set(Calendar.HOUR_OF_DAY, 14); 
     calendar.set(Calendar.MINUTE, 50); 
     if (calendar.getTimeInMillis() < System.currentTimeMillis()) calendar.setTimeInMillis(calendar.getTimeInMillis() + AlarmManager.INTERVAL_DAY); 
     AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); 
// With setInexactRepeating(), you have to use one of the AlarmManager interval 
// constants--in this case, AlarmManager.INTERVAL_DAY. 

    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 
      AlarmManager.INTERVAL_DAY, pendingIntent); 

내가 //if(calendar.getTime().compareTo(new Date()) < 0) calendar.add(Calendar.DAY_OF_MONTH, 1); 댓글을 달았습니다.

작동하는지 알려주세요.

업데이트 : 매일 알림을 처리하는 서비스가 많이 사용되는지 여부를 완전히 확인해야합니다. 고갈 배터리 응용 프로그램은 사용자를 귀찮게합니다.

+1

Perfect! 나는 그 문제를 해결했다! 아주 좋은 대답은 당신에게 감사합니다! – Claff

관련 문제