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

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

问题描述

我在那之后开始接收器的手机开机是这样的:

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,.

去解决这个问题,我需要从我的活动,以检查是否AlarmManager设置,如果不是我需要从活动设置。

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.

推荐答案

不幸的是,你不能查询AlarmManager当前警报。

Sadly, you cant query AlarmManager for current Alarms.

为您解决问题最好的办法是为<一个href="http://developer.android.com/reference/android/app/AlarmManager.html#cancel%28android.app.PendingIntent%29"相对=nofollow>取消当前的报警并设置一个新的。

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

您只需要创建一个相匹配的一个在reciever意图。 <一href="http://stackoverflow.com/questions/4315611/android-get-all-pendingintents-set-with-alarmmanager">More这里资讯

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

所以添加到您的活动

   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);

您可能需要改变意图的上下文,以便其相同的一个在Reciever

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?

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

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