2017-03-09 9 views
0

私は私を取得しようとしています毎日通知サービスが正常に動作していますが、問題が残っています。Androidの通知アプリケーションの起動時にトリガする

私は将来で一日の特定の時間に通知を受け取る必要がありますが、私は間違っている

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

は、その状態で、通知が唯一の翌日をトリガーする必要があるためということに気づきました、私はアプリを起動する瞬間ではありません。ここで

は、作業の事を得るために私の試みです:

私はのonCreate()メソッド、私のMainActivityでこれを呼び出す:私はif(calendar.getTime().compareTo(new Date()) < 0) calendar.add(Calendar.DAY_OF_MONTH, 1);を追加すると、私は結果を達成しただろうと思っ

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); 
    } 

必要だった。

ありがとうございました。

答えて

0

setInexactRepeatingを呼び出す前に、このチェックを追加します。

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

は、この情報がお役に立てば幸いです。

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

完璧!私はこの問題を解決しました!非常に良い答えはありがとう! – Claff

関連する問題