2017-07-27 101 views
0

我必須在上午5:30觸發本地通知,並且正在工作。但問題是,每當我打開我的應用程序時,通知也會被觸發。這是我在我的MainActivity代碼:本地通知觸發器每次打開應用程序時

MainActivityonCreate()

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

爲什麼每次觸發我打開我的應用程序?

回答

2

要設置報警電流日上午05時05分,如果是已經通過警報將被立即觸發。停止它這樣做。

AlarmManager 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); 
     if (System.currentTimeMillis() > calendar.getTimeInMillis()) { 
      calendar.add(Calendar.DAY_OF_MONTH, 1); 
     } 
     alarmManager.cancel(pendingIntent); 
     alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent); 

我們在這裏做的是,如果時間已經過去了,我們正在把+1日期,以便它會從明年開始的日期,我們取消以前設置的報警。

2

因爲你在上午9點創建

Calendar.getInstance() 

例如,但設定的時間5:30 AM過去。所以當你使用

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

它已經發生。

您可以使用此

if(calendar.getTimeInMillis() < System.currentTimeMillis()) { 
    calendar.add(Calendar.DAY_OF_MONTH, 1); 
} 
+0

那麼可能的解決方案是什麼?每次打開我的應用程序時,如何避免觸發通知? – XoXo

+0

會在第二天上午5:30觸發通知嗎? – XoXo

+0

或今天如果你使用這個條件 'if(calendar.getTimeInMillis() Aleksandr

0

試試這個

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, 30); 
    calendar.set(Calendar.SECOND, 0); 
    if(calendar.getTime().after(new Date())){ 
    calendar.set(Calendar.Date,1); 
    } 
    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent); 
相關問題