安卓:警报通知发行警报、通知

2023-09-07 11:23:15 作者:国家级睡觉型选手

在我的应用程序已经设置了许多报警通知。都是在不同的日期,但具有相同的时间。而我与切换按钮的帮助下将它设置开/关。

现在的问题是,如果我把它放在那么在那个时候它给我的previous一个通知,从当前日期。它只出现一次,然后所有的报警通知工作正常。

假设,如果我已经设置了闹钟,以在12,14,16日期,然后我设定的日期为15我的设备和切换按钮,然后它给了我14通知在那个时候。然后,一切都很好。

因此​​,它是逻辑上是正确还是有我的code什么问题?

如果它可以发生或不?

360安全卫士发布橙色警报 15万台手机沦为僵尸

请看到我已经设置了报警通知下面code:

 公共无效startTwoMonthAlarmNotification(){        // ===================== GST报警两个月===================== =====        //为GST 20 2011年6月        AM_2M_GST_1 =(AlarmManager)getApplicationContext()getSystemService(Context.ALARM_SERVICE)。        意图IN1 =新意图(这一点,AlarmReceiverNotificationForTwoMonth.class);        in1.putExtra(MyMessage,你的2每月消费税的回报是2011年6月20日DUE);        PI_2M_GST_1 = PendingIntent.getBroadcast(这一点,0,IN1,PendingIntent.FLAG_UPDATE_CURRENT);        日历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);        //为GST 19 2011年8月        AM_2M_GST_2 =(AlarmManager)getApplicationContext()getSystemService(Context.ALARM_SERVICE)。        意图IN2 =新意图(这一点,AlarmReceiverNotificationForTwoMonth.class);        in2.putExtra(MyMessage,你的2每月GST回报是于2011年8月19日DUE);        PI_2M_GST_2 = PendingIntent.getBroadcast(这一点,1,IN2,PendingIntent.FLAG_UPDATE_CURRENT);        日历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);        //为GST 21 2011年10月        AM_2M_GST_3 =(AlarmManager)getApplicationContext()getSystemService(Context.ALARM_SERVICE)。        意图IN3 =新意图(这一点,AlarmReceiverNotificationForTwoMonth.class);        in3.putExtra(MyMessage,你的2每月GST回报是于2011年10月21日DUE);        PI_2M_GST_3 = PendingIntent.getBroadcast(这一点,2,IN3,PendingIntent.FLAG_UPDATE_CURRENT);        日历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);        //为GST 17 2011年12月        AM_2M_GST_4 =(AlarmManager)getApplicationContext()getSystemService(Context.ALARM_SERVICE)。        意图IN4 =新意图(这一点,AlarmReceiverNotificationForTwoMonth.class);        in4.putExtra(MyMessage,你的2每月GST回报是于2011年12月19日DUE);        PI_2M_GST_4 = PendingIntent.getBroadcast(这一点,3,IN4,PendingIntent.FLAG_UPDATE_CURRENT);        日历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);        /////////////////////////////////////        //为GST 2012年2月26日        AM_2M_GST_5 =(AlarmManager)getApplicationContext()getSystemService(Context.ALARM_SERVICE)。        意图IN5 =新意图(这一点,AlarmReceiverNotificationForTwoMonth.class);        in5.putExtra(MyMessage,你的2每月GST回报是2012年2月28日DUE);        PI_2M_GST_5 = PendingIntent.getBroadcast(这一点,4,IN5,PendingIntent.FLAG_UPDATE_CURRENT);        日历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);        /////////////////////    //为GST 27 2012年4月        AM_2M_GST_6 =(AlarmManager)getApplicationContext()getSystemService(Context.ALARM_SERVICE)。        意图IN6 =新意图(这一点,AlarmReceiverNotificationForTwoMonth.class);        in6.putExtra(MyMessage,你的2每月GST回报是于2012年4月29日DUE);        PI_2M_GST_6 = PendingIntent.getBroadcast(这一点,5,IN6,PendingIntent.FLAG_UPDATE_CURRENT);        日历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);    //为GST 16 2012年1月        AM_2M_GST_7 =(AlarmManager)getApplicationContext()getSystemService(Context.ALARM_SERVICE)。        意图IN7 =新意图(这一点,AlarmReceiverNotificationForTwoMonth.class);        in7.putExtra(MyMessage,你的2每月GST回报是2012年1月到期16);        PI_2M_GST_7 = PendingIntent.getBroadcast(这一点,6,IN7,PendingIntent.FLAG_UPDATE_CURRENT);        日历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);        } 

下面code是在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); 

解决方案

首先,您必须取消旧AlarmManager和待定的意图。在这里,你没有取消挂起的意图。只是取消使用THIC code -

  alaramManagerObj.cancel(的PendingIntent);pendingIntent.cancel(); 

这将是清楚您不需要的通知,只为提供报警工程。如果你想与指定的日期检查您的报警,该日期即通过与否。就在这个检查 -

 日历C = Calendar.getInstance();c.set(2011,1,20);长钢厂= c.getTimeInMillis();长currentTime的= System.currentTimeMillis的();INT X =(钢厂< = currentTime的)0:1; 

在整数 X 将返回0,如果日期不通过,否则返回1。使用此code您的需求。

但愿这可以帮助你。

In My application i have set the many Alarm Notification. All are at different date but with the same time. And i am setting it on/off with the help of the toggle Button.

Now the Problem is, If i am setting it on then at that time it gives me notification of the previous one from current date. it only appear at once and then all alarm notification works fine.

Suppose if i have set the alarm at 12,14,16 date then i set the date as 15 in my device and on the toggle button then it gives me notification of 14 at that time. And then all goes fine.

So is it logically correct or is there any problem in my code ?

Should it be happened or not ?

Please see the Below code that i have set for the alarm notification:

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

Below code is for the Canceling all the notification alarm on the 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);

解决方案

First you must cancel the old AlarmManager and Pending Intent. Here you didn't cancel your Pending Intent. Just cancel that use thic code -

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

This will be clear your unWanted Notification and works only for provided Alarm. If you want to Check your alarm with Specified date for that date is passed or not. Just check with this -

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

In the integer x will return 0 if the date is not passed, else returns 1. Use this code to your needs.

Hope this may helps you.