0

我有一个正在声明警报的服务(AlarmService.java):

           Intent intentAlarm = new Intent(this, TimeAlarm.class);             
           intentAlarm.putExtra("Notif_body", Notif_Body);             
            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            PendingIntent displayIntent = PendingIntent.getBroadcast(getApplication(),1,intentAlarm, 0);
           alarmManager.set(AlarmManager.RTC_WAKEUP,new GregorianCalendar().getTimeInMillis()+5*1000, displayIntent);

和一个活动(user_settings.java),一旦取消选中复选框,我想从中删除该警报:

            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            Intent intentAlarm = new Intent(this,TimeAlarm.class);
            PendingIntent displayIntent = PendingIntent.getBroadcast(getApplication(),1,intentAlarm, 0);               
            alarmManager.cancel(displayIntent);

但它不起作用我的警报没有被删除为什么虽然pendingintent与它应该是一样的!!

4

2 回答 2

0
intentAlarm.putExtra("Notif_body", Notif_Body); 

在取消警报期间创建 intentAlarm 时放置上述行,或在设置警报时将其删除。我想这会奏效。

于 2013-11-11T19:21:47.580 回答
0

我的问题是:

而不是添加:

if(bool_activate_reminder==true){
//call the alarm method;
}

我在做:

 if(bool_activate_reminder=true){//with one equal 
    //call the alarm method;
    }

所以它总是被访问......当我修复它并在服务中使用以下代码时:

 Intent intentAlarm = new Intent(getApplication(), TimeAlarm.class);    
               //add the notification body to the intent:
               intentAlarm.putExtra("Notif_body", Notif_Body);             
               // create the object
                AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
               //set the alarm for particular time
                PendingIntent displayIntent = PendingIntent.getBroadcast(getApplication(),1,intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
               alarmManager.set(AlarmManager.RTC_WAKEUP,new GregorianCalendar().getTimeInMillis()+20*1000, displayIntent);

并在活动中:

 AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            Intent intentAlarm = new Intent(getApplication(),TimeAlarm.class);
            PendingIntent displayIntent = PendingIntent.getBroadcast(getApplication(),1,intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);               
            alarmManager.cancel(displayIntent);

它工作得很好..

于 2013-11-24T13:36:09.197 回答