警报未取消

时间:2014-05-03 14:29:34

标签: android alarmmanager

我在活动中创建了重复闹钟,并且我尝试在其他活动中删除此闹钟。它只是不会发生。

A.activity中的创建代码

AlarmManager am = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    Log.i("location", String.valueOf(id));

    Intent notifyCycle = new Intent(this,NotifyCycleChange.class);
    notifyCycle.setData(Uri.parse("custom://" + id));
    notifyCycle.setAction(String.valueOf(id));
    notifyCycle.putExtra(DatabaseHelper.COLUMN_ROWID, id);
    notifyCycle.putExtra(DatabaseHelper.COLUMN_TITLE, titleText.getText().toString());
    notifyCycle.putExtra(DatabaseHelper.COLUMN_DESCRIPTION, "       Enter Description");
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),(int)id, notifyCycle, PendingIntent.FLAG_UPDATE_CURRENT);


    am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()
            + 60*1000,timestore, pendingIntent);
    Log.i("At here", "successfully");

和B.activity中的取消代码

 AlarmManager alarmManager = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);

            Intent notifyCycle = new Intent(context, NotifyCycleChange.class);
            notifyCycle.setData(Uri.parse("custom://" + id));
            notifyCycle.setAction(String.valueOf(id));
            PendingIntent pendingUpdateIntent = PendingIntent.getService(getApplicationContext(), (int) id, notifyCycle, PendingIntent.FLAG_UPDATE_CURRENT);



            Log.i("location", String.valueOf(id));
            // Cancel alarms
            try {
                alarmManager.cancel(pendingUpdateIntent);
            } catch (Exception e) {
                Log.e("TAG", "AlarmManager update was not canceled. " + e.toString());
            }

没有错误,我已经检查了id的匹配

2 个答案:

答案 0 :(得分:0)

当您创建待处理意图启动和取消警报时,上下文必须相同。 请考虑创建静态方法以获取上下文,以便您可以从其他活动中调用它。

答案 1 :(得分:0)

您应该在安排PendingIntentAlarm

时创建一个类似的cancel the repeat Alarm

设置闹钟时,您已将PendingIntent设为PendingIntent.getBroadcast,但在取消PendingIntent.getService时。这是错误的

创建类似意图取消警报

 Intent notifyCycle = new Intent(context, NotifyCycleChange.class);
 PendingIntent pendingUpdateIntent = PendingIntent.getBroadcast(getApplicationContext(), (int) id, notifyCycle, PendingIntent.FLAG_UPDATE_CURRENT);

然后,取消闹钟

 AlarmManager alarmManager = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
 alarmManager.cancel(pendingUpdateIntent);