如何检查是否报警设置 [英] How to check if alarm is set

查看:193
本文介绍了如何检查是否报警设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查我的报警激活。该alarmIsSet方法将返回false报警设置之前,真当报警设置。到目前为止好,但报警后,我取消alarmIsSet将继续返回true,直到我重新启动设备。 我该如何解决这个问题?

 公共类报警延伸活动{
    私人意向意图=新的意图(PROPOSE_A_TOAST);

    私人无效报警(布尔激活){
        AlarmManager AM =(AlarmManager)getSystemService(Context.ALARM_SERVICE);
        PendingIntent PI = PendingIntent.getBroadcast(此,0,意向,0);

        如果(激活==真){
            整型= AlarmManager.ELAPSED_REALTIME_WAKEUP;
            长间隔= 3000;
            长triggerTime = SystemClock.elapsedRealtime();
            am.setRepeating(类型,triggerTime,间隔,PI);
        } 其他 {
            am.cancel(PI);
        }
    }

    私人布尔alarmIsSet(){
        返回PendingIntent.getBroadcast(此,0,意向,PendingIntent.FLAG_NO_CREATE)!= NULL;
    }
}
 

解决方案

您只需要添加

  pi.cancel();
 

  am.cancel(PI);
 

I'm trying to check if my alarm is active or not. The alarmIsSet method will return false before the alarm is set, true when the alarm is set. So far so good, however, after the alarm i canceled alarmIsSet will continue to return true until I reboot the device. How do I fix this?

public class Alarm extends Activity {
    private Intent intent = new Intent("PROPOSE_A_TOAST");

    private void alarm (boolean activate) {
        AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, 0);

        if(activate == true) {
            int type = AlarmManager.ELAPSED_REALTIME_WAKEUP;
            long interval = 3000;
            long triggerTime = SystemClock.elapsedRealtime();       
            am.setRepeating(type, triggerTime, interval, pi);       
        } else {
            am.cancel(pi);
        }
    }

    private boolean alarmIsSet() {
        return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_NO_CREATE) != null;
    }
}

解决方案

You just have to add

pi.cancel();

after

am.cancel(pi);

这篇关于如何检查是否报警设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆