2014-01-10 2 views
2

PendingIntentcancel() API док говорит:Отмена AlarmManager событие с отменить

Отменить текущий активный PendingIntent. Только оригинальное приложение, имеющее PendingIntent, может отменить его.

Я не уверен в значении этого. Если установить событие AlarmManager от активности x вроде этого:

PendingIntent pendingIntent; 
Intent myIntent = new Intent(x.this, AlarmReciever.class); 

myIntent.putExtra("task_uuid", task_uuid); 
pendingIntent = PendingIntent.getBroadcast(x.this, 0, myIntent,0); 
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
alarmManager.set(AlarmManager.RTC, dateTime.getTimeInMillis(), pendingIntent); 

Мой вопрос: могу ли я отменить ожидающий умысел деятельности y с помощью:

PendingIntent pendingIntent; 
Intent myIntent = new Intent(y.this, AlarmReciever.class); 

myIntent.putExtra("task_uuid", task_uuid); 
pendingIntent = PendingIntent.getBroadcast(y.this, 0, myIntent,0); 
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
alarmManager.cancel(pendingIntent); 
+0

Это будет отменено ... Идентификатор должен быть таким же, и вы это сделали. Подробнее читайте на странице http://stackoverflow.com/questions/9823408/cancelling-a-pendingintent –

ответ

1

При установке сигнализации вы должны пройти один ключевое значение в PendingIntent будет дифференцироваться не сигнализаций, должно быть, как это

pendingIntent = PendingIntent.getBroadcast(x.this, key_value, myIntent,0); 
SharedPreferences settings = context.getSharedPreferences("alarm", 0); 
SharedPreferences.Editor editor = settings.edit();  
editor.putBoolean(key, false); 
editor.commit(); 

отменить тот же сигнал, который необходимо сохранить, что key_values некоторые, где вы можете использовать общие настройки. получить тот же ключ, а затем отменить будильник, как это

SharedPreferences settings = context.getSharedPreferences("alarm", 0);    
Map<String,?> allNotifyIdsMap = settings.getAll();  
if(allNotifyIdsMap!=null&&allNotifyIdsMap.isEmpty()==false) 
{  
    for(String notifyId: allNotifyIdsMap.keySet()) 
    { 
     boolean isCleared = settings.getBoolean(notifyId, false); 
     if(isCleared==false) 
     { 
      pendingIntent = PendingIntent.getBroadcast(y.this, key_value, myIntent,0); 
      AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
      alarmManager.cancel(pendingIntent); 
     } 
    } 
} 
Смежные вопросы