簡體   English   中英

禁用RepeatingAlarms不起作用[Android平台]

[英]Disabling RepeatingAlarms does not work [Android platform]

我在Android上禁用來自AlarmManager的警報時遇到麻煩。

我在這里閱讀了幾篇文章和答案,不知道為什么我的cancel()警報不起作用。

我什至將我的代碼更改為帶有靜態變量的代碼(但仍然無法正常工作),該代碼存儲在Utility.class中,而不是調用該方法的代碼。

public class Utilities {
public static final int ID_FOR_STOP_ALARM = 770;

private static PendingIntent piStopMyApp;
private static Intent aiStopMyApp;
private static Context aContext;

public static boolean toggleAlarmToStopMyApp(Context context) {

    if(aContext == null){
        aContext = context;
    }

    if (aiStopMyApp == null) {
        aiStopMyApp = new Intent(aContext, MyAppServiceStop.class);
    }

    PendingIntent piStopMyAppCheck = PendingIntent.getService(aContext,
            ID_FOR_STOP_ALARM, aiStopMyApp, PendingIntent.FLAG_UPDATE_CURRENT);

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

    if (piStopMyApp == null) {
        piStopMyApp = PendingIntent.getService(aContext, ID_FOR_STOP_ALARM,
                      aiStopMyApp, PendingIntent.FLAG_UPDATE_CURRENT);
    }
    if (piStopMyAppCheck == null) {

        SharedPreferences p = aContext.getSharedPreferences(
                Utilities.APP_SHARED_PREFS, Context.MODE_PRIVATE);

        int h = p.getInt(Utilities.ALARM_STOP_H, 23);
        int m = p.getInt(Utilities.ALARM_STOP_M, 0);

        Calendar cur_cal = new GregorianCalendar();
        cur_cal.setTimeInMillis(System.currentTimeMillis());
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.YEAR, cur_cal.get(Calendar.YEAR));
        cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));
        cal.set(Calendar.DAY_OF_MONTH, cur_cal.get(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, h);
        cal.set(Calendar.MINUTE, m);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        if (isLoggingEnabled) {
            Log.d(TAG, "Enable ALARM for STOP " + cal.toString());
        }

        alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY, piStopMyApp);

    } else {

        alarm.cancel(piStopMyApp);

        if (isLoggingEnabled) {
            Log.d(TAG, "Disable ALARM for STOP = "
                + (PendingIntent.getService(aContext,
                            ID_FOR_STOP_ALARM,aiStopMyApp,
                    PendingIntent.FLAG_NO_CREATE) == null));
        }

    }

    piStopMyAppCheck = null;

    return (PendingIntent.getService(aContext, ID_FOR_STOP_ALARM, aiStopMyApp,

            PendingIntent.FLAG_NO_CREATE) != null);
}

}

而且它根本不起作用,無法啟用警報,也無法將其禁用:-(

其他活動中的按鈕運行以下代碼:

    boolean alarmUpStop  = Utilities.toggleAlarmToStopMyApp(getApplicationContext());

1)第一次點擊上方的按鈕,警報已正確安排,日志顯示已觸發服務

2)第二次點擊按鈕並記錄日志,該警報已嘗試禁用,但未禁用:-(

有什么線索嗎?

由於使用以下命令檢查警報是否正在運行,因此無法正常運行:(PendingIntent.getService(aContext,ID_FOR_STOP_ALARM,aiStopMyApp,PendingIntent.FLAG_NO_CREATE)== null)

我只是簡單地將其更改為:

1)運行警報時,我在Prefs中存儲alarmRunning = true來知道警報處於活動狀態

2)取消警報時,我在Prefs中設置alarmRunning = false來知道已禁用警報

在啟動時,我知道Prefs的最新狀態,如有必要,我可以重新創建警報

此解決方法有缺陷,但可以起作用,它只是假設設置了警報時,無論什么,直到啟動,它都必須由系統保留,但是應該這樣:-)

下個問題見。 V.

您可以使用以下方法檢查警報是否處於活動狀態:

PendingIntent.getService(aContext, ID_FOR_STOP_ALARM,aiStopMyApp,
                       PendingIntent.FLAG_NO_CREATE) != null

您對null比較只是錯誤的。 PendingIntent.FLAG_NO_CREATE的文檔不正確。 如果沒有匹配的PendingIntent ,則返回null ;否則,返回匹配的PendingIntent

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM