警报没有被停止[英] Alarm not getting stopped

本文是小编为大家收集整理的关于警报没有被停止的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试停止警报并检查它是否停止,但它总是返回 true 表示警报正在工作.我试图根据链接 https://stackoverflow.com/a/17616299/1226882 中的答案停止警报但它对我不起作用.

请参考以下代码

  • 开始闹钟

    public static void startSchedulerAlaram(Context ctx) {
    
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 0); 
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    
    if (calendar.compareTo(Calendar.getInstance()) <= 0)
        calendar.add(Calendar.DATE, 1);
    
    AlarmManager alarmManager = (AlarmManager) ctx
            .getSystemService(Context.ALARM_SERVICE);
    
    Intent intent = new Intent(ctx, Alaram_Receiver.class);
    intent.setAction(Utility.SCHEDULE_ACTION);
    
    PendingIntent pi = PendingIntent.getBroadcast(ctx, 1, intent,0);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
    calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pi);
    
    log("Scheduler Alarm", "Started");
    
    }
    
  • 停止闹钟

    public static void stopSchedulerAlaram(Context ctx) {
    
    Intent intent = new Intent(ctx, Alaram_Receiver.class);
    intent.setAction(Utility.SCHEDULE_ACTION);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, 1,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) ctx
            .getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(pendingIntent);
    
    log("Scheduler Alarm", "Stopped");
    }
    
  • 检查警报

    public static boolean checkSchedulerAlaram(Context ctx) {
    
    boolean alarmUp = (PendingIntent.getBroadcast(ctx, 1, new Intent(ctx,Alaram_Receiver.class).setAction(Utility.SCHEDULE_ACTION),
            PendingIntent.FLAG_NO_CREATE) != null);
    
    return alarmUp;
    }
    

推荐答案

你必须通过different id for set alarm

PendingIntent.getBroadcast(ctx, id, intent,PendingIntent.FLAG_UPDATE_CURRENT);

对于 cancel alarm you have pass sameidthat you used for set.

PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, id,
        intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) ctx
        .getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);

其他推荐答案

您正在使用此代码来确定是否设置了闹钟:

boolean alarmUp = (PendingIntent.getBroadcast(ctx, 1, new Intent(ctx,Alaram_Receiver.class).setAction(Utility.SCHEDULE_ACTION),
    PendingIntent.FLAG_NO_CREATE) != null);

此代码依赖于 PendingIntent 的存在来确定是否安排了警报.

当您取消闹钟时,您会这样做:

PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, 1,
    intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) ctx
    .getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);

这会取消警报,但不会从系统中删除 PendingIntent.由于您依赖 PendingIntent 的存在/不存在来了解警报是否已安排,因此您还需要取消 PendingIntent 以便将其从系统中删除.这样做(在您取消闹钟后):

pendingIntent.cancel();

其他推荐答案

这是我用来取消报警服务的代码

Intent intent = new Intent(this, MyReceiver.class);
         PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
         AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
         alarmManager.cancel(sender);

本文地址:https://www.itbaoku.cn/post/102529.html

问题描述

I am trying to stop the alarm and checking it whether it stopped or not, but it always returns true means alarm is working. I tried to stop the alarm based on the answer in the link https://stackoverflow.com/a/17616299/1226882 but it doesnt work for me.

Please refer the below code

  • Start Alarm

    public static void startSchedulerAlaram(Context ctx) {
    
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 0); 
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    
    if (calendar.compareTo(Calendar.getInstance()) <= 0)
        calendar.add(Calendar.DATE, 1);
    
    AlarmManager alarmManager = (AlarmManager) ctx
            .getSystemService(Context.ALARM_SERVICE);
    
    Intent intent = new Intent(ctx, Alaram_Receiver.class);
    intent.setAction(Utility.SCHEDULE_ACTION);
    
    PendingIntent pi = PendingIntent.getBroadcast(ctx, 1, intent,0);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
    calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pi);
    
    log("Scheduler Alarm", "Started");
    
    }
    
  • Stop Alarm

    public static void stopSchedulerAlaram(Context ctx) {
    
    Intent intent = new Intent(ctx, Alaram_Receiver.class);
    intent.setAction(Utility.SCHEDULE_ACTION);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, 1,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) ctx
            .getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(pendingIntent);
    
    log("Scheduler Alarm", "Stopped");
    }
    
  • Check Alarm

    public static boolean checkSchedulerAlaram(Context ctx) {
    
    boolean alarmUp = (PendingIntent.getBroadcast(ctx, 1, new Intent(ctx,Alaram_Receiver.class).setAction(Utility.SCHEDULE_ACTION),
            PendingIntent.FLAG_NO_CREATE) != null);
    
    return alarmUp;
    }
    

推荐答案

You have to pass different id for set alarm

PendingIntent.getBroadcast(ctx, id, intent,PendingIntent.FLAG_UPDATE_CURRENT);

For cancel alarm you have pass sameidthat you used for set.

PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, id,
        intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) ctx
        .getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);

其他推荐答案

You are using this code to determine if the alarm is set or not:

boolean alarmUp = (PendingIntent.getBroadcast(ctx, 1, new Intent(ctx,Alaram_Receiver.class).setAction(Utility.SCHEDULE_ACTION),
    PendingIntent.FLAG_NO_CREATE) != null);

This code relies on the presence of the PendingIntent to determine if the alarm is scheduled or not.

When you cancel the alarm you do this:

PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, 1,
    intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) ctx
    .getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);

This cancels the alarm, but does not remove the PendingIntent from the system. Since you rely on the presence/absence of the PendingIntent to know if the alarm has been scheduled or not you also need to cancel the PendingIntent so that it will be removed from the system. Do that like this (after you cancel the alarm):

pendingIntent.cancel();

其他推荐答案

Here is my code that i am using to cancel the alarm service

Intent intent = new Intent(this, MyReceiver.class);
         PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
         AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
         alarmManager.cancel(sender);