2013-05-14 63 views
1

我已經安裝了alarmManager如下:如何取消AlarmManager

Intent intent = new Intent(TopActivity.this, RecordActivity.class); 
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    intent.putExtra(Utils.KEY_RECORD_TIME, recordLength); 
    intent.putExtra(Utils.KEY_REC_START_TIME, start); 

    saveTimeAndLength(start, recordLength); 

    PendingIntent pintent = PendingIntent.getActivity(TopActivity.this, 0, intent, 0); 
    AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
    alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pintent); 

下應該取消它,但它總是失敗。我錯過了什麼?

AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
Intent intent = new Intent(TopActivity.this, RecordActivity.class); 
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
intent.putExtra(Utils.KEY_RECORD_TIME, start); 
intent.putExtra(Utils.KEY_REC_START_TIME, recordLength); 

PendingIntent pintent = PendingIntent.getActivity(TopActivity.this, 0, intent, 0); 
try { 
    alarmManager.cancel(pintent); 
    Log.e(TAG, "Cancelling all pending intents"); 
} catch (Exception e) { 
    Log.e(TAG, "AlarmManager update was not canceled. " + e.toString()); 
} 

我已經閱讀了StackOverflow的很多答案,但仍然無法弄清楚是什麼問題。
在此先感謝!

+0

有人已經提到,額外的數據密鑰反轉(啓動和RECORDLENGTH)?'intent.putExtra(Utils.KEY_RECORD_TIME,RECORDLENGTH) ; intent.putExtra(Utils.KEY_REC_START_TIME,start);'與'intent.putExtra(Utils.KEY_RECORD_TIME,start); intent.putExtra(Utils.KEY_REC_START_TIME,recordLength);' - 它不應該導致過濾麻煩,雖然... – Johannes 2016-09-09 14:30:44

回答

2

我會張貼在這裏我的回答希望能幫助別人節省一些時間。
不知怎的,在雙方都添加標誌PendingIntent.FLAG_UPDATE_CURRENT解決了這個問題。

PendingIntent pintent = PendingIntent.getActivity(TopActivity.this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT); 

解決了這個問題。
設置報警

Intent intent = new Intent(TopActivity.this, RecordActivity.class); 
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    intent.putExtra(Utils.KEY_RECORD_TIME, recordLength); 
    intent.putExtra(Utils.KEY_REC_START_TIME, start); 

    saveTimeAndLength(start, recordLength); 

    PendingIntent pintent = PendingIntent.getActivity(TopActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
    AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
    alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pintent); 

取消報警

AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
Intent intent = new Intent(TopActivity.this, RecordActivity.class); 

PendingIntent pintent = PendingIntent.getActivity(TopActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
try { 
    alarmManager.cancel(pintent); 
    Log.e(TAG, "Cancelling all pending intents"); 
} catch (Exception e) { 
    Log.e(TAG, "AlarmManager update was not canceled. " + e.toString()); 
} 
0

您需要創建掛起的意圖,然後將其取消

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 

Intent updateServiceIntent = new Intent(context, MyPendingIntentService.class); 
PendingIntent pendingUpdateIntent = PendingIntent.getService(context, 0, updateServiceIntent, 0); 

// Cancel alarms 
try { 
    alarmManager.cancel(pendingUpdateIntent); 
} catch (Exception e) { 
    Log.e(TAG, "AlarmManager update was not canceled. " + e.toString()); 
} 
+0

我想我做同樣的事情! – 2013-05-14 06:39:44

2

對於AlarmManager取消事件,它需要兩個意圖比較。

這是通過filterEquals實現(意向其他)方法,它說

確定是否兩個目的是意圖 分辨率(濾波)的目的是相同的。也就是說,如果他們的行爲,數據,類型,類別, 和類別是相同的。這不會比較意圖中包含的任何額外數據 。

我不知道爲什麼它不匹配,當類是相同的。

我也有同樣的問題,爲了克服,我也設置了一個數據。

看看這是否有幫助你。

EDITED

在設置報警

Intent intent = new Intent(TopActivity.this, RecordActivity.class); 
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    intent.putExtra(Utils.KEY_RECORD_TIME, recordLength); 
    intent.putExtra(Utils.KEY_REC_START_TIME, start); 
    intent.setData(Uri.fromParts("yourappscheme", "alarm", null)); 
    saveTimeAndLength(start, recordLength); 

    PendingIntent pintent = PendingIntent.getActivity(TopActivity.this, 0, intent, 0); 
    AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
    alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pintent); 

當需要取消

AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
Intent intent = new Intent(TopActivity.this, RecordActivity.class); 
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
intent.putExtra(Utils.KEY_RECORD_TIME, start); 
intent.putExtra(Utils.KEY_REC_START_TIME, recordLength); 
intent.setData(Uri.fromParts("yourappscheme", "alarm", null)); 

PendingIntent pintent = PendingIntent.getActivity(TopActivity.this, 0, intent, 0); 
try { 
    alarmManager.cancel(pintent); 
    Log.e(TAG, "Cancelling all pending intents"); 
} catch (Exception e) { 
    Log.e(TAG, "AlarmManager update was not canceled. " + e.toString()); 
} 
+0

你能否告訴我你是如何克服你的問題的。我所做的一切似乎都行不通。@ Vivek Khandelwal我甚至保存了我在鬧鐘中設置的意圖並取消它,但它不起作用。 – 2013-05-14 08:10:37

+0

我還沒有嘗試過你的方法,不知何故添加標誌PendingIntent.FLAG_UPDATE_CURRENT解決了我的問題。很奇怪! – 2013-05-14 10:35:20