如何检查报警器是否已经设置并运行[英] How to check if Alarm have been set and running

本文是小编为大家收集整理的关于如何检查报警器是否已经设置并运行的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个接收器,它在手机启动之后开始如下:

<receiver android:name=".OnBootReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>

</receiver>

在接收器中,我运行设置这样的警报:

AlarmManager  mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

        Intent i=new Intent(context, LocationPoller.class);

        i.putExtra(LocationPoller.EXTRA_INTENT,
                  new Intent(context, LocationReceiver.class));
        i.putExtra(LocationPoller.EXTRA_PROVIDER,
                 LocationManager.GPS_PROVIDER);



         PendingIntent pi=PendingIntent.getBroadcast(context, 0, i, 0);
          mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                        SystemClock.elapsedRealtime(),
                        PERIOD,
                        pi);

工作正常,除了当然,当用户安装应用程序时,将不会设置警报,直到用户重新启动手机,.

要解决此问题,我需要从我的活动中检查如果设置为Alarmmanager,如果不是我需要从活动中设置.

因此,如何检查警报管理器是否已设置.

推荐答案

可悲的是,您无法查询AlarmManager用于当前警报.

解决问题的最佳选择将是取消当前警报并设置一个新的警报.

您只需创建与再次匹配的意图. 更多信息在这里

所以将此添加到您的活动中

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

     Intent i=new Intent(context, LocationPoller.class);

        i.putExtra(LocationPoller.EXTRA_INTENT,
                  new Intent(context, LocationReceiver.class));
        i.putExtra(LocationPoller.EXTRA_PROVIDER,
                 LocationManager.GPS_PROVIDER);

        PendingIntent pi=PendingIntent.getBroadcast(context, 0, i, 0);
    // Cancel alarms
    try {
        AlarmManager.cancel(pi);
    } catch (Exception e) {
        Log.e(TAG, "AlarmManager update was not canceled. " + e.toString());
    }
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                        SystemClock.elapsedRealtime(),
                        PERIOD,
                        pi);

您可能需要更改意图的上下文,使其与再现中的内容相同.

另一个解决方法是检测它是否是应用的第一个开始,并且只开始它.但是,如果用户在第一个开始之前重新启动手机时会发生什么?

其他推荐答案

您唯一可以做的是当用户安装应用程序并第一次打开它时,您可以在SharedPreference中设置一个标志,以便设置警报是否已设置,如果它未设置为集阿拉兰.

在你的主要Activity中 检查onCreate方法

SharedPreferences spref = getSharedPreferences("TAG", MODE_PRIVATE);

 Boolean   alaram_set = spref.getBoolean("alarm_set_flag", false);  //default is false

if(!alaram_set){
//create your alarm here then set the flag to true

 SharedPreferences.Editor editor = spref.edit();
        editor.putBoolean("alarm_set_flag", true); // value to store
        editor.commit();
}

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

问题描述

I have a receiver that start after phone boot like this:

<receiver android:name=".OnBootReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>

</receiver>

in the receiver I run set an alarm like this:

AlarmManager  mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

        Intent i=new Intent(context, LocationPoller.class);

        i.putExtra(LocationPoller.EXTRA_INTENT,
                  new Intent(context, LocationReceiver.class));
        i.putExtra(LocationPoller.EXTRA_PROVIDER,
                 LocationManager.GPS_PROVIDER);



         PendingIntent pi=PendingIntent.getBroadcast(context, 0, i, 0);
          mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                        SystemClock.elapsedRealtime(),
                        PERIOD,
                        pi);

It works fine, except, of course, when user install app, the alarm will not be set until user reboot phone,.

to go around this, I need to check from my Activity if AlarmManager is set, if not I need to set from Activity.

Hence, how do I check if Alarm manager is already set.

推荐答案

Sadly, you cant query AlarmManager for current Alarms.

Best option to solve your problem would be to cancel your current Alarm and set a new one.

You just have to create an intent that matches the one in the reciever. More info here

so add this to your activity

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

     Intent i=new Intent(context, LocationPoller.class);

        i.putExtra(LocationPoller.EXTRA_INTENT,
                  new Intent(context, LocationReceiver.class));
        i.putExtra(LocationPoller.EXTRA_PROVIDER,
                 LocationManager.GPS_PROVIDER);

        PendingIntent pi=PendingIntent.getBroadcast(context, 0, i, 0);
    // Cancel alarms
    try {
        AlarmManager.cancel(pi);
    } catch (Exception e) {
        Log.e(TAG, "AlarmManager update was not canceled. " + e.toString());
    }
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                        SystemClock.elapsedRealtime(),
                        PERIOD,
                        pi);

You probably need to change the context of the intent so its the same as the one in the Reciever.

Another Workaround would be to detect if its the first start of the App, and only start it then. But then what happens if user reboots his phone before the first start?

其他推荐答案

The only thing you can do is when the user install the app and open it the first time you can set a flag in the SharedPreference that tells you if the Alarm is set or not if its not set set the Alaram .

in your main Activity check in the onCreate method

SharedPreferences spref = getSharedPreferences("TAG", MODE_PRIVATE);

 Boolean   alaram_set = spref.getBoolean("alarm_set_flag", false);  //default is false

if(!alaram_set){
//create your alarm here then set the flag to true

 SharedPreferences.Editor editor = spref.edit();
        editor.putBoolean("alarm_set_flag", true); // value to store
        editor.commit();
}