0

我有一个保存在 TimeInterval 对象中的每周计划,它们工作正常。我构建了一个循环,这样我就可以为每个人设置一个警报。我的代码不起作用,我错过了什么?

此外,如何从另一个线程的 BroadcastReceiver 取消这些警报?

for(ArrayList<TimeInterval> day : days){
        for(TimeInterval ti : day){
            numberofintents++;
            Intent i = new Intent(getApplicationContext(),ChangeMode.class);
            i.putExtra("ringermode", ti.getRingerMode());

            PendingIntent pending = PendingIntent.getService(getApplicationContext(), id, i,PendingIntent.FLAG_CANCEL_CURRENT);
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.MINUTE,ti.getMinute()%60);
            cal.set(Calendar.HOUR_OF_DAY,ti.getMinute()/60);
            cal.set(Calendar.DAY_OF_WEEK,dayofweek);
            Log.d(TAG, cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE));
            service.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 1000*60*60*24*7, pending);
            id++;
        }
        dayofweek++;
    }
4

1 回答 1

0

首先制作一个待处理意图的数组列表,

public static ArrayList <PendingIntent> intentArray= new ArrayList <PendingIntent>();

然后将待处理的意图添加到数组列表中,

for(ArrayList<TimeInterval> day : days){
    for(TimeInterval ti : day){
        numberofintents++;
        Intent i = new Intent(getApplicationContext(),ChangeMode.class);
        i.putExtra("ringermode", ti.getRingerMode());

        PendingIntent pending = PendingIntent.getService(getApplicationContext(), id, i,PendingIntent.FLAG_CANCEL_CURRENT);

        intentarray.add(pending);

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MINUTE,ti.getMinute()%60);
        cal.set(Calendar.HOUR_OF_DAY,ti.getMinute()/60);
        cal.set(Calendar.DAY_OF_WEEK,dayofweek);


    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
            pendingIntent);


         id++;
    }
    dayofweek++;
}

现在您的待处理意图存储在数组列表中。

如果您想取消闹钟,

alarmManager.cancel(intentArray.get(i));
于 2013-03-04T09:33:14.797 回答