android:使用AlarmManager运行后台任务

发布于 2024-09-26 05:54:43 字数 165 浏览 0 评论 0原文

我正在编写一个应用程序,需要定期检查服务器是否有新消息并通知用户。我见过一些使用 AlarmManager 来点击 BroadcastReciever 的示例,这似乎是正确的做法,但我似乎无法让它工作。

谁能给我展示此类事情的分步教程(重复警报触发某种触发通知的后台代码)?

TIA

I am writing an app which needs to periodically check the server for new messages and notify the user. I have seen some examples using AlarmManager to hit a BroadcastReciever which seems like the right thing to do, but i cant seem to get it to work.

Can anyone show me a step by step tutorial for this sort of thing (repeating alarm which triggers some kind of background code that fires a Notification)?

TIA

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

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

发布评论

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

评论(1

ゝ杯具 2024-10-03 05:54:43

这是一个完整的示例:http://android-in-practice .googlecode.com/svn/trunk/ch02/DealDroidWithService/

此示例使用的模式(我发现似乎效果很好)是使用启动接收器来设置 AlarmManager /em> (当然,也检查是否从您的主 Activity 开始轮询,以防您的应用程序已安装且系统未启动)并拥有 AlarmManager em> 为另一个接收者发送Intenthttp://android-in-practice.googlecode.com/svn/trunk/ch02/DealDroidWithService/src/com/manning/aip/dealdroid/DealBootReceiver.java< /a>

然后从 AlarmReceiver 启动 IntentService
http: //android-in-practice.googlecode.com/svn/trunk/ch02/DealDroidWithService/src/com/manning/aip/dealdroid/DealAlarmReceiver.java

从你的 IntentService 然后让你的网络调用来轮询数据,或者任何您需要做的事情。 IntentService 自动将您的工作放入后台线程中,这非常方便:
http: //android-in-practice.googlecode.com/svn/trunk/ch02/DealDroidWithService/src/com/manning/aip/dealdroid/DealService.java

也检查这些类的文档,其中有很多内容那里。

此示例需要注意的是,它处理唤醒锁间隙(如果您需要的话,优秀的 CommonsWare 代码可以做到这一点),但它可能会为您提供更多有关如何潜在解决该问题的想法。 “使用 AlarmManager 和服务进行轮询”的东西。

更新:代码现在在这里: https://github.com/charlieCollins/android-in-练习

Here is one complete example: http://android-in-practice.googlecode.com/svn/trunk/ch02/DealDroidWithService/

The pattern this example uses, and one that I've found that seems to work well, is to use a boot receiver to setup the AlarmManager (and of course also check to start the polling from your main Activity too, for the case when your app is installed and the system is not booted) and have the AlarmManager send an Intent for another receiver: http://android-in-practice.googlecode.com/svn/trunk/ch02/DealDroidWithService/src/com/manning/aip/dealdroid/DealBootReceiver.java

And then from the AlarmReceiver start an IntentService:
http://android-in-practice.googlecode.com/svn/trunk/ch02/DealDroidWithService/src/com/manning/aip/dealdroid/DealAlarmReceiver.java

From your IntentService then make your network call to poll for data, or whatever you need to do. IntentService automatically puts your work in a background thread, it's very handy:
http://android-in-practice.googlecode.com/svn/trunk/ch02/DealDroidWithService/src/com/manning/aip/dealdroid/DealService.java

Check the docs for these classes too, a lot of into in there.

The caveat with this example is that it does not deal with the wake lock gap (the excellent CommonsWare code does that if you need it), but it may give you some more ideas about how to potentially address the "poll using AlarmManager and Service" stuff.

UPDATE: the code is now here: https://github.com/charlieCollins/android-in-practice

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