我必須在上午5:30觸發本地通知,並且正在工作。但問題是,每當我打開我的應用程序時,通知也會被觸發。這是我在我的MainActivity
代碼:本地通知觸發器每次打開應用程序時
MainActivity
的
onCreate()
:
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent alarmIntent = new Intent(this, MyStartServiceReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 100, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 5);
calendar.set(Calendar.MINUTE, 5);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
這裏是我的MyStartServiceReceiver
:
public class MyStartServiceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("AlarmReceiver", "Started");
Intent intent1 = new Intent(context, AlarmService.class);
intent1.setData(Uri.parse("custom://" + System.currentTimeMillis()));
context.startService(intent1);
}
}
爲什麼每次觸發我打開我的應用程序?
那麼可能的解決方案是什麼?每次打開我的應用程序時,如何避免觸發通知? – XoXo
會在第二天上午5:30觸發通知嗎? – XoXo
或今天如果你使用這個條件 'if(calendar.getTimeInMillis()
Aleksandr