8

特定の時間に通知があります。コードを参照してください。

//Create alarm manager
 AlarmManager alarmMgr0 = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

 //Create pending intent & register it to your alarm notifier class
 Intent intent0 = new Intent(this, AlarmReceiver_maandag_1e.class);
 intent0.putExtra("uur", "1e"); 
 PendingIntent pendingIntent0 = PendingIntent.getBroadcast(this, 0, intent0, 0);

 //set timer you want alarm to work (here I have set it to 8.30)
 Calendar timeOff9 = Calendar.getInstance();
 timeOff9.set(Calendar.HOUR_OF_DAY, 8);
 timeOff9.set(Calendar.MINUTE, 30);
 timeOff9.set(Calendar.SECOND, 0);

 //set that timer as a RTC Wakeup to alarm manager object
 alarmMgr0.set(AlarmManager.RTC_WAKEUP, timeOff9.getTimeInMillis(), pendingIntent0);

これは、1つのことを除いて完璧に機能します。

通知は、アラーム マネージャーで 8:30 に設定されています。たとえば、8:30、9:00 の後にアプリを起動すると、通知は引き続き表示されます..

8:30 にアラームが鳴るが、それ以降ではない可能性はありますか?

編集

同じ問題を抱えている人には、次の解決策があります。

これをブロードキャストレシーバーに入れます:

long current_time = System.currentTimeMillis();

Calendar timeOff9 = Calendar.getInstance();
timeOff9.set(Calendar.HOUR_OF_DAY, 8);
timeOff9.set(Calendar.MINUTE, 35);
timeOff9.set(Calendar.SECOND, 0);

long limit_time = timeOff9.getTimeInMillis();

if (current_time > limit_time) {
    //nothing
} else {
    //show notification
}
4

1 に答える 1

2

また、Alarm Manager の次の方法を試すこともできます。

public void setExact (int type, long triggerAtMillis, PendingIntent operation)

ただし、API レベル 19 が必要です。

于 2014-09-21T06:10:44.517 に答える