gpt4 book ai didi

android - 警报没有停止

转载 作者:行者123 更新时间:2023-11-29 21:09:50 25 4
gpt4 key购买 nike

我正在尝试停止闹钟并检查它是否停止,但它总是返回 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;
    }

最佳答案

你必须传递设置闹钟的不同id

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

对于取消闹钟,您已经传递了与设置时相同的id

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

关于android - 警报没有停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23316413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com