0

Dans Mon application, j'ai défini les nombreuses notifications d'alarme. Tous sont à des dates différentes mais avec la même heure. Et je le règle on/off avec l'aide du bouton à bascule.Android: Notification d'alarme Problème

Maintenant, le problème est, si je le mets en marche puis à ce moment-là, il me donne notification de la précédente à partir de la date actuelle. il apparaît seulement à la fois et alors toute la notification d'alarme fonctionne bien. Supposons que si j'ai réglé l'alarme à la date 12,14,16, j'ai réglé la date sur 15 dans mon appareil et sur le bouton à bascule, cela me donne une notification de 14 à ce moment-là. Et puis tout va bien.

Est-ce que c'est logiquement correct ou y a-t-il un problème dans mon code?

Devrait-il se produire ou non?

S'il vous plaît voir le code ci-dessous que je l'ai défini pour la notification d'alarme:

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

Ci-dessous le code est pour l'annulation de toutes les alarmes de notification sur la toogleButton off:

 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

peut-être que c'est un problème d'indexation. Vous devriez poster du code n. – dokkaebi

+0

@dokkaebi: veuillez voir la question mise à jour. Que j'utilise pour régler l'alarme. Qu'est-ce qui ne va pas avec ça ? Et ma logique est-elle réglée ou non? –

+0

@dokkaebi: Avez-vous une idée de comment le résoudre ou pourquoi c'est comme ça? –

Répondre

3

vous devez d'abord annuler l'ancien AlarmManager et l'intention en attente. Ici, vous n'avez pas annulé votre intention en attente. Annulez simplement que l'utilisation du code Thic -

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

Ce sera clairement votre notification non sollicitées et ne fonctionne que pour l'alarme fourni. Si vous voulez vérifier votre alarme avec la date spécifiée pour cette date est passée ou pas. Il suffit de vérifier ce point -

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

Dans l'entier x retourne 0 si la date ne sont pas transmises, sinon retourne 1. Utilisez ce code à vos besoins.

Espérons que cela puisse vous aider.

+0

O Merci. Ça marche pour moi. –

+1

Merci Cela fonctionne aussi pour moi ... –