【问题标题】:Unable to cancel multiple alarms at once无法一次取消多个警报
【发布时间】:2017-06-22 23:31:03
【问题描述】:

我在设置中有一个开关,当打开时将通过警报管理器设置多个警报,如果关闭则将取消所有正在设置的警报。两者都采用不同的方法,但它根本没有取消任何警报。我检查了所有链接并了解如果我没有指定所有请求代码而只是调用 .cancel 它将取消每个警报(我也尝试跟踪 ID,但在我的情况下它无法工作)。下面是代码。

 // in on create it is getting the value from settings

if(test==true){
        Toast.makeText(getBaseContext(), "in MainAct true", Toast.LENGTH_SHORT).show();
        setAlarm1();
    }
    else {
        Toast.makeText(getBaseContext(), "in MainAct False", Toast.LENGTH_SHORT).show();
        unsetAlarm();

    }

用于设置闹钟

   public void setAlarm1() { // on click of notification , checking oka? don't be so..

    int columnIndex = 0; // Whichever column we want to fetchh, 0 coz i fetch only time's col so yeahhhh.....
    int secondColumnIndex = 1;
    int keyidColumnIndex = 2;

    handler = new SQLiteDB(getBaseContext());
    handler.open();
    Cursor C = handler.returnData();
    String[] DealTimes = new String[C.getCount()];
    String[] NotificationText = new String[C.getCount()];
    String[] keyIds = new String[C.getCount()];
  //  intent_ids = new int[C.getCount()];


    if (C.moveToFirst()) {
        for (int i = 0; i < C.getCount(); i++) {

            //   DealTimes[i] = C.getBlob(columnIndex);
            DealTimes[i] = C.getString(columnIndex);
            NotificationText[i] = C.getString(secondColumnIndex);
            keyIds[i] = C.getString(keyidColumnIndex);

            C.moveToNext();
        }
    }
    C.close();
    handler.close();


    // Converting that string array into an array of time
    /////////---------- WORKED ALHUMDULLIALHHHH <3 <3
    ////           Above wokring coool, next is experiment, and it worked :D <3

    int[] h = new int[C.getCount()];
    int[] m = new int[C.getCount()];
    long[] AllTime = new long[C.getCount()];


    Intent alertIntent = new Intent(this, AlertReceiver.class);
    alertIntent.putExtra("strings", DealTimes);  // sending deal times thoruh string to AlertReceverrr !!!

    for (int i = 0; i < C.getCount(); i++) {


        h[i] = Integer.valueOf(DealTimes[i].split(":")[0]);
        m[i] = Integer.valueOf(DealTimes[i].split(":")[1]);
        Calendar calendar = Calendar.getInstance();

        int curHr = calendar.get(Calendar.HOUR_OF_DAY);

        calendar.set(Calendar.HOUR_OF_DAY, h[i]);

        calendar.set(Calendar.MINUTE, m[i]);
        calendar.set(calendar.SECOND, 0);
        calendar.set(calendar.MILLISECOND, 0);


        if(calendar.before(Calendar.getInstance())) {
            calendar.add(Calendar.DATE, 1);
        }

        alertIntent.putExtra("text", NotificationText[i]);


        AllTime[i] = calendar.getTimeInMillis();


        final int _id = (int) System.currentTimeMillis();

        intent_ids[i] = _id; // for keeping track of id, but it won't work coz every time I run the application it will generate new ids and at the time of cancel it will cancel the recent ones not all.

        Log.i("setAlarm", "intent idsss" + intent_ids[i] + "original id = " + _id);  
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        pendingIntent =  PendingIntent.getBroadcast(this, _id, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,AllTime[i],AlarmManager.INTERVAL_DAY,

                pendingIntent);
                  }

}

取消

public void unsetAlarm() {

        handler = new SQLiteDB(getBaseContext());
        handler.open();
        Cursor C = handler.returnData();

        Intent intent = new Intent(this, AlertReceiver.class);
        final int _id = (int) System.currentTimeMillis();

        Log.i("unsetAlarm", "intent idsss outside loop" + intent_ids );

        for (int i = 0; i < C.getCount(); i++) {
            Log.i("untAlarm", "intent ids seprate loop" + intent_ids[i]);
        }
         pendingIntent = PendingIntent.getBroadcast(this,0, intent, PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA);
        alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    try{
        alarmManager.cancel(pendingIntent);
    } catch (Exception e) {
        Log.e("FAILED TO CANCEL", "cancel failed " + e.getMessage());
    }



    Toast.makeText(getBaseContext(), "Cancelling notifications", Toast.LENGTH_SHORT).show();


}

【问题讨论】:

    标签: android notifications alarmmanager repeat cancellation


    【解决方案1】:

    请勿使用 final int _id = (int) System.currentTimeMillis(); 作为唯一 ID 来设置或取消设置警报。因为System.currentTimeMillis() 每毫秒都会有所不同。您应该根据项目的要求使用一些唯一值作为int _id 这个值。否则你做得很好。

    【讨论】:

    • 我已经更新了我的代码,请重新检查。如果我删除 final int _id = (int) System.currentTimeMillis();对于请求代码,那么我将不得不提供我自己的唯一 ID,这是不可能的,因为我必须从数据库中获得如此多的警报时间,这些时间不断变化,所以我无法对其进行硬编码。
    • @shahtajkhalid 我不是要你硬编码。您只需要为您设置的每个警报提供唯一的 ID。可能您可以从您的数据库本身检索唯一 id。
    • 哦,我明白了。它有效:D 非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多