Android Alarmmanager 不太可靠

发布于 2024-10-14 21:57:05 字数 1857 浏览 4 评论 0原文

我在使用 Android 的 Alarmmanager 功能时遇到问题。

问题是等待一个多小时左右的警报无法响起。

我的应用程序最初创建一个闹钟,如下所示:-

PendingIntent sender = PendingIntent.getBroadcast(this, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
             am.set(AlarmManager.RTC_WAKEUP, mCal.getTimeInMillis(), sender);

当闹钟响起时,它会触发我的 RecieverHandler 类,特别是这个函数:-

public void onReceive(Context context, Intent intent) 
    {
        try {
             Bundle bundle = intent.getExtras();


             Intent newIntent = new Intent(context, MessageDispatcher.class);
             newIntent.putExtras(bundle);
            // newIntent.addFlags(Intent.FLAG);
             context.startService(newIntent);



            } catch (Exception e) {
             Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
             e.printStackTrace();

            }


    }

然后启动一个名为 MessageDispatcher 的服务,并调用该函数:-

public int onStartCommand(Intent intent, int flags, int startId)

该函数获取下一个闹钟时间从我的数据库中,我确信这工作正常,然后它根据数据库中的日期设置一个新警报,如下所示:-

PendingIntent sender = PendingIntent.getBroadcast(this, 192837, newIntent, PendingIntent.FLAG_UPDATE_CURRENT);


    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, mCal.getSendAt().getTimeInMillis(), sender);

这将为下一条消息创建警报。

我已经在很短的时间内对此进行了测试,它似乎有效,并且通过更改手机内的日期和时间进行了长时间的测试。看来发射成功了。

然后,当该警报响起时,它会发出下一个警报并安排该警报。我几乎 100% 确定这些部件工作正常。

所以我只坚持一些为什么它不起作用的理论。

我认为这可能与我断开手机与调试器的连接有关,但在这种情况下,警报似乎会在很短的时间内起作用。

所以我的主要理论是我正在创建的警报管理器在一段时间后会被删除?如果这是真的,这就是一个大问题,因为无论过去了多少时间,我都需要它正常工作。

非常感谢任何确保我的警报保持正常的帮助,谢谢。

I am having a problem with the Alarmmanager functions for the Android.

The problem is alarms that have over an hour or so to wait fail to go off.

My application initially creates an alarm like so:-

PendingIntent sender = PendingIntent.getBroadcast(this, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
             am.set(AlarmManager.RTC_WAKEUP, mCal.getTimeInMillis(), sender);

When the alarm goes off it triggers my RecieverHandler class, specifically this function:-

public void onReceive(Context context, Intent intent) 
    {
        try {
             Bundle bundle = intent.getExtras();


             Intent newIntent = new Intent(context, MessageDispatcher.class);
             newIntent.putExtras(bundle);
            // newIntent.addFlags(Intent.FLAG);
             context.startService(newIntent);



            } catch (Exception e) {
             Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
             e.printStackTrace();

            }


    }

This then launches a service by the name of MessageDispatcher and this function is called:-

public int onStartCommand(Intent intent, int flags, int startId)

This function gets the next alarm time from my Database, this I am sure is working correctly, it then sets a new alarm based on the date from the database like so:-

PendingIntent sender = PendingIntent.getBroadcast(this, 192837, newIntent, PendingIntent.FLAG_UPDATE_CURRENT);


    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, mCal.getSendAt().getTimeInMillis(), sender);

This creates the alarm for the next message.

I have tested this over a short amount of time and it appears to work and have tested it over large amounts of time by changing my date and time within the phone. It appears to fire off successfully.

Then when this alarm goes off it gets the next alarm to go off and schedules this. I am nearly 100% sure these parts are working fine.

So I am stuck with only some theories of why it’s not working.

I thought it might be related to me disconnecting the phone from the debugger but the alarm seems to work over short amounts of time in that case.

So my main theory is that the alarmmanager I am creating is being deleted after a certain amount of time? If this is true this a big problem since I need this to be working no matter how much time has passed.

Any help into ensuring my alarm remains is greatly appreciated, thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里的微风 2024-10-21 21:57:05

所以我的主要理论是我正在创建的警报管理器在一段时间后会被删除?

已注册的警报将保持注册状态,直到您取消它们,或者直到下次重新启动,或者直到用户在 Android 2.1 及更早版本上使用“任务杀手”终止您的应用程序。

您没有指出:

  • 您如何确定警报是否“响起”
  • BroadcastReceiver 正在做什么

如果没有这些信息,就不可能说出您哪里出了问题。

确保您在 BroadcastReceiver 中完成所有工作(如果工作非常快),或者在将控制权传递给 BroadcastReceiver 时维护自己的 WakeLock code>IntentService 正在完成其余的工作。查看 WakefulIntentService 了解更多信息。

此外,您可能会尝试为每个警报创建唯一的Intents,而不是就地更新当前的意图。我不知道这里有什么特别的问题,但这让我感到紧张。

So my main theory is that the alarmmanager I am creating is being deleted after a certain amount of time?

Alarms that are registered remain registered until you cancel them, or until the next reboot, or until the user kills your application with a "task killer" on Android 2.1 and earlier.

You have not indicated:

  • how you are determining whether the alarm is "going off"
  • what the BroadcastReceiver is doing

Without that information, it is impossible to say where you are going wrong.

Make sure that either you are doing all your work in the BroadcastReceiver (if the work is very quick) or that you are maintaining your own WakeLock as you pass control to the IntentService that is doing the rest of the work. Check out WakefulIntentService for more.

Also, you might try to create unique Intents per alarm, rather than updating the current one in place. I am not aware that there is a particular problem here, but it makes me nervous.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文