如何检查是否报警设置

2023-09-04 13:25:59 作者:唐宫梦i

我想检查我的报警激活。该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);

相关推荐