我应该将 PendingIntent.getService() 或 getBroadcast 与 AlarmManager 一起使用吗?

发布于 2024-12-02 18:17:24 字数 718 浏览 1 评论 0原文

我的应用程序需要在每天的特定时间从网络上获取一些数据。所以我使用 AlarmManager 来安排任务,效果很好。

但是,当查看各种示例时,当涉及待处理意图时,似乎有两种方法可以处理 AlarmManager。

当警报响起时,可以使用 PendingIntent.getBroadcast() 来调用广播接收器,并且在该接收器内启动执行实际工作的服务。

另一种方法是使用 PendingIntent.getService() 并在警报响起时直接调用服务。

有人可以向我解释这两种方法之间的区别,以便我决定依赖哪一种吗?

编辑:还有一个问题是使用 getService() 时在哪里获取唤醒锁?

例如,当使用 BroadcastReceiver 时,我在 onReceive() 中有以下行:

WakeReminderIntentService.acquireStaticLock(context);

如果相反,我直接调用该服务,例如:

PendingIntent pi = PendingIntent.getService(this, 0, new Intent(this, OnAlarmReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);

我应该简单地从服务内部获取它吗?

My app needs to grab some data from the web at a specific time each day. So I use an AlarmManager to schedule the task and that works ok.

But when looking at various examples there seems to be two ways to deal with the AlarmManager when it comes to the pending intent.

One uses PendingIntent.getBroadcast() to call a broadcast receiver when the alarm goes off and inside that receiver the service to do the real work is started.

Another approach is to use PendingIntent.getService() and call the service directly when that alarm goes off.

Can someone explain to me the difference between the two approaches so I can decide on which one to rely?

EDIT: One more question is where to acquire the wake lock when using getService()?

For example, when using a BroadcastReceiver I have the following line in onReceive():

WakeReminderIntentService.acquireStaticLock(context);

How should I acquire the wake lock if I instead call the service directly like:

PendingIntent pi = PendingIntent.getService(this, 0, new Intent(this, OnAlarmReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);

Should I simply acquire it from within the service instead?

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

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

发布评论

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

评论(2

甲如呢乙后呢 2024-12-09 18:17:24

当警报响起时,可以使用 PendingIntent.getBroadcast() 调用广播接收器,并且在该接收器内启动执行实际工作的服务。

它比启动服务多了一个步骤。

另一种方法是使用 PendingIntent.getService() 并在警报响起时直接调用服务。

那么你应该使用第二种方法,因为它减少了你的执行步骤。

One uses PendingIntent.getBroadcast() to call a broadcast receiver when the alarm goes off and inside that receiver the service to do the real work is started.

it has one more step in starting service than

Another approach is to use PendingIntent.getService() and call the service directly when that alarm goes off.

then you should use the second approach as it is reducing your one step in execution..

一杯敬自由 2024-12-09 18:17:24

阅读您的编辑,我想您自己发现了:如果您想确保在使用 AlarmManager 时启动您的服务,您最好绕道首先发送到接收器并获取唤醒锁定那里。
否则,手机可能会在启动请求的服务之前进入睡眠状态。
这就是 AlarmManager 的 javadoc 所说的,我还在 Google 的帖子中阅读了它工程师。

现在进行编辑:何时获取锁?
使用接收器的全部目的是在接收器的 onReceive() 方法中获取锁,因为在该方法执行期间 Android 不会进入睡眠状态。

有关示例,请参阅此 问题

Reading your edit I presume you found out yourself: If you want to make sure that your service is started when using AlarmManager, you better take the detour of first sending to a receiver and acquiring a wake lock there.
Otherwise it is possible that the phone will sleep before the requested service is launched.
That's what the javadoc of AlarmManager says and I also read it in post by Google engineer.

So now for your edit: When to acquire the lock?
The whole point of using the receiver is to acquire the lock within the onReceive() method of the receiver, because Android will not fall asleep during the execution of this method.

For an example see this question.

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