无法一次取消多个警报

时间:2017-02-06 05:17:38

标签: android notifications alarmmanager repeat cancellation

我在设置中有一个开关,当打开时将通过警报管理器设置多个警报,如果关闭则将取消所有设置的警报。两者都有不同的方法,但它根本没有取消任何警报。我检查了所有链接,并了解如果我没有指定所有请求代码并且只是调用.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();


}

1 个答案:

答案 0 :(得分:1)

请勿使用final int _id = (int) System.currentTimeMillis();作为唯一ID来设置或取消设置闹钟。因为System.currentTimeMillis()在每毫秒后会有所不同。您应该根据项目的要求使用一些唯一值作为int _id此值。否则你的表现很好。