2013-05-27 41 views
1

首先,本網站很棒,每個人都非常有幫助。這是我的第一篇文章,所以原諒我,如果我有任何ommited。Android AlarmService重複警報在取消呼叫後重復重複一次

我創建警報像這樣:

private void startLocation() { 

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

    Intent i = new Intent(context, MyWeatherUpdateService.class); 
    PendingIntent service = PendingIntent.getService(context, 0, i, 
       PendingIntent.FLAG_CANCEL_CURRENT); 

    alarm.setRepeating(AlarmManager.RTC, SystemClock.elapsedRealtime() + (60 * 1000), 
      Long.parseLong(prefs.getString("listpref", "60000")), service); 

} 

在該方法中其被稱爲片段內,上下文是從getApplication(),listpref是以毫秒爲單位的字符串更新間隔。

我通過取消:

public void endLocation() { 

    Intent i = new Intent(context, MyWeatherUpdateService.class); 
    PendingIntent service = PendingIntent.getService(context, 0, i, 
       PendingIntent.FLAG_CANCEL_CURRENT); 
    alarm.cancel(service); 

} 

確保意圖/未決意圖是相同的。

現在我有2個問題:

1)火警幾乎imediately創建後,即使我告訴它1分鐘後開始。 2)當我呼叫取消時,警報在取消之前再次發生。

問題1)爲什麼鬧鐘會很快起火?和2)這是否按預期工作,或者如果報警立即取消,就像我想要的那樣。

如果我沒有提供足夠的信息,如果需要添加更多的代碼。

在此先感謝。

回答

1

的火警創建之後幾乎imediately,即使我告訴它1分鐘

那是因爲你正在使用RTCelapsedRealtime()開始時間後啓動。那些需要匹配。最簡單的解決方案是切換到ELAPSED_REALTIME

當我呼叫取消時,報警被取消之前報警再次發生。

嘗試0更換PendingIntent.FLAG_CANCEL_CURRENT,至少在PendingIntentcancel()電話。

+0

謝謝您的及時答覆,但我自己排序。我實際上調用了兩次startlocation方法。抱歉。 – yrag69

+0

我應該接受你的答案還是離開? – yrag69

+0

@CommonsWare感謝!!,defintely upvote這個好答案 –