为什么我的闹钟没有被取消

时间:2013-11-11 19:02:10

标签: android alarmmanager

我有一个服务(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是相同的应该是!!

2 个答案:

答案 0 :(得分:0)

intentAlarm.putExtra("Notif_body", Notif_Body); 

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

答案 1 :(得分: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);

它运作得很好..