0

在我的應用程序中,我設置了許多警報通知。 所有在不同的日期,但在同一時間。而且我在切換按鈕的幫助下打開/關閉它。Android:警報通知問題

現在的問題是,如果我設置它然後在那個時候它給我從當前日期的前一個通知。它只會立即出現,然後所有警報通知都可以正常工作。

假設如果我已將鬧鐘設置爲12,14,16日期,那麼我在設備中設置日期爲15,然後在切換按鈕上,那時它會通知我當時有14。然後一切順利。

那麼它在邏輯上是正確的還是在我的代碼中有任何問題?

是否應該發生?

請看到我已經設置了報警通知下面的代碼:

public void startTwoMonthAlarmNotification() { 

     // ===================== GST ALARM FOR THE TWO MONTHS ========================== 
     // for the GST 20 June 2011 
     AM_2M_GST_1 = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE); 
     Intent in1 = new Intent(this, AlarmReceiverNotificationForTwoMonth.class); 
     in1.putExtra("MyMessage","Your 2 Monthly GST return is DUE on 20th June 2011."); 
     PI_2M_GST_1 = PendingIntent.getBroadcast(this, 0, in1, PendingIntent.FLAG_UPDATE_CURRENT); 
     Calendar calendar_GST_18_June_2011 = Calendar.getInstance(); 
     calendar_GST_18_June_2011.setTimeInMillis(System.currentTimeMillis()); 
     calendar_GST_18_June_2011.set(2011, 5, 18, mHour, mMinute, 0); 
     AM_2M_GST_1.set(AlarmManager.RTC_WAKEUP, calendar_GST_18_June_2011.getTimeInMillis(), PI_2M_GST_1); 

     // for the GST 19 August 2011 
     AM_2M_GST_2 = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE); 
     Intent in2 = new Intent(this, AlarmReceiverNotificationForTwoMonth.class); 
     in2.putExtra("MyMessage","Your 2 Monthly GST return is DUE on 19th August 2011."); 
     PI_2M_GST_2 = PendingIntent.getBroadcast(this, 1, in2, PendingIntent.FLAG_UPDATE_CURRENT); 
     Calendar calendar_GST_17_August_2011 = Calendar.getInstance(); 
     calendar_GST_17_August_2011.setTimeInMillis(System.currentTimeMillis()); 
     calendar_GST_17_August_2011.set(2011, 7, 17,mHour, mMinute, 0); 
     AM_2M_GST_2.set(AlarmManager.RTC_WAKEUP, calendar_GST_17_August_2011.getTimeInMillis(),PI_2M_GST_2); 

     // for the GST 21 October 2011 
     AM_2M_GST_3 = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE); 
     Intent in3 = new Intent(this, AlarmReceiverNotificationForTwoMonth.class); 
     in3.putExtra("MyMessage","Your 2 Monthly GST return is DUE on 21st October 2011."); 
     PI_2M_GST_3 = PendingIntent.getBroadcast(this, 2, in3, PendingIntent.FLAG_UPDATE_CURRENT); 
     Calendar calendar_GST_19_October_2011 = Calendar.getInstance(); 
     calendar_GST_19_October_2011.setTimeInMillis(System.currentTimeMillis()); 
     calendar_GST_19_October_2011.set(2011, 9, 19,mHour, mMinute, 0); 
     AM_2M_GST_3.set(AlarmManager.RTC_WAKEUP, calendar_GST_19_October_2011.getTimeInMillis(),PI_2M_GST_3); 

     // for the GST 17 December 2011 
     AM_2M_GST_4 = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE); 
     Intent in4 = new Intent(this, AlarmReceiverNotificationForTwoMonth.class); 
     in4.putExtra("MyMessage","Your 2 Monthly GST return is DUE on 19th December 2011."); 
     PI_2M_GST_4 = PendingIntent.getBroadcast(this, 3, in4, PendingIntent.FLAG_UPDATE_CURRENT); 
     Calendar calendar_GST_17_December_2011 = Calendar.getInstance(); 
     calendar_GST_17_December_2011.setTimeInMillis(System.currentTimeMillis()); 
     calendar_GST_17_December_2011.set(2011, 11, 17,mHour, mMinute, 0); 
     AM_2M_GST_4.set(AlarmManager.RTC_WAKEUP, calendar_GST_17_December_2011.getTimeInMillis(),PI_2M_GST_4); 
     ///////////////////////////////////// 
     // for the GST 26 February 2012 
     AM_2M_GST_5 = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE); 
     Intent in5 = new Intent(this, AlarmReceiverNotificationForTwoMonth.class); 
     in5.putExtra("MyMessage","Your 2 Monthly GST return is DUE on 28th February 2012."); 
     PI_2M_GST_5 = PendingIntent.getBroadcast(this, 4, in5, PendingIntent.FLAG_UPDATE_CURRENT); 
     Calendar calendar_GST_26_February_2012 = Calendar.getInstance(); 
     calendar_GST_26_February_2012.setTimeInMillis(System.currentTimeMillis()); 
     calendar_GST_26_February_2012.set(2012, 1, 26,mHour, mMinute, 0); 
     AM_2M_GST_5.set(AlarmManager.RTC_WAKEUP, calendar_GST_26_February_2012.getTimeInMillis(),PI_2M_GST_5); 
     ///////////////////// 
    // for the GST 27 April 2012  
     AM_2M_GST_6 = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE); 
     Intent in6 = new Intent(this, AlarmReceiverNotificationForTwoMonth.class); 
     in6.putExtra("MyMessage","Your 2 Monthly GST return is DUE on 29th April 2012."); 
     PI_2M_GST_6 = PendingIntent.getBroadcast(this, 5, in6, PendingIntent.FLAG_UPDATE_CURRENT); 
     Calendar calendar_GST_27_April_2012 = Calendar.getInstance(); 
     calendar_GST_27_April_2012.setTimeInMillis(System.currentTimeMillis()); 
     calendar_GST_27_April_2012.set(2012, 3, 27,mHour, mMinute, 0); 
     AM_2M_GST_6.set(AlarmManager.RTC_WAKEUP, calendar_GST_27_April_2012.getTimeInMillis(),PI_2M_GST_6); 

    // for the GST 16 January 2012 
     AM_2M_GST_7 = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE); 
     Intent in7 = new Intent(this, AlarmReceiverNotificationForTwoMonth.class); 
     in7.putExtra("MyMessage","Your 2 Monthly GST return is DUE on 16th January 2012."); 
     PI_2M_GST_7 = PendingIntent.getBroadcast(this, 6, in7, PendingIntent.FLAG_UPDATE_CURRENT); 
     Calendar calendar_GST_16_January_2012 = Calendar.getInstance(); 
     calendar_GST_16_January_2012.setTimeInMillis(System.currentTimeMillis()); 
     calendar_GST_16_January_2012.set(2012, 0, 14,mHour, mMinute, 0); 
     AM_2M_GST_7.set(AlarmManager.RTC_WAKEUP, calendar_GST_16_January_2012.getTimeInMillis(),PI_2M_GST_7); 
     } 

下面的代碼是在toogleButton關閉取消全部通知報警:

 AM_2M_GST_1.cancel(PI_2M_GST_1); 
    AM_2M_GST_2.cancel(PI_2M_GST_2); 
    AM_2M_GST_3.cancel(PI_2M_GST_3); 
    AM_2M_GST_4.cancel(PI_2M_GST_4); 
    AM_2M_GST_5.cancel(PI_2M_GST_5); 
    AM_2M_GST_6.cancel(PI_2M_GST_6); 
    AM_2M_PROVISIONAL_1.cancel(PI_2M_PROVISIONAL_1); 
    AM_2M_PROVISIONAL_2.cancel(PI_2M_PROVISIONAL_2); 
    AM_2M_PROVISIONAL_3.cancel(PI_2M_PROVISIONAL_3); 
    AM_2M_TERMINAL_1.cancel(PI_2M_TERMINAL_1); 
+0

也許這是一個索引問題。你應該發佈一些代碼。 – dokkaebi

+0

@dokkaebi:請參閱最新的問題。我用來設置鬧鐘。那有什麼不對?我的邏輯是否設置了它? –

+0

@dokkaebi:你有什麼想法如何解決它或爲什麼它是這樣的? –

回答

3

首先,你必須取消舊的AlarmManager和Pending Intent。在這裏你並沒有取消你的Pending Intent。只是取消使用THIC碼 -

alaramManagerObj.cancel(pendingIntent); 
pendingIntent.cancel(); 

這將是清楚您不需要的通知,並只爲提供報警的作品。如果您想要檢查具有該日期的指定日期的鬧鐘是否通過。只是這個檢查 -

Calendar c = Calendar.getInstance(); 
c.set(2011, 1, 20); 
long mills = c.getTimeInMillis(); 
long currentTime = System.currentTimeMillis(); 
int x = (mills<=currentTime)?0:1; 

在整數x將返回0,如果日期不通過,否則返回1.使用此代碼,以您的需求。

希望這可以幫助你。

+0

O謝謝。這個對我有用。 –

+1

謝謝它也適用於我... –