android:使用 AlarmManager 运行后台任务 [英] android: running a background task using AlarmManager

查看:27
本文介绍了android:使用 AlarmManager 运行后台任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个应用程序,它需要定期检查服务器是否有新消息并通知用户.我已经看到一些使用 AlarmManager 命中 BroadcastReciever 的示例,这似乎是正确的做法,但我似乎无法让它工作.

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

推荐答案

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

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

这个例子使用的模式,我发现它似乎工作得很好,是使用一个引导接收器来设置AlarmManager(当然也检查从您的主要 Activity 也是如此,例如您的应用程序已安装但系统未启动),并让 AlarmManager 发送另一个 Intent接收器:http://android-in-practice.googlecode.com/svn/trunk/ch02/DealDroidWithService/src/com/manning/aip/dealdroid/DealBootReceiver.java

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

然后从 AlarmReceiver 启动一个 IntentService:http://android-in-practice.googlecode.com/svn/trunk/ch02/DealDroidWithService/src/com/manning/aip/dealdroid/DealAlarmReceiver.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

从您的 IntentService 然后进行网络调用以轮询数据或任何您需要做的事情.IntentService 自动将你的工作放到后台线程中,非常方便:http://android-in-practice.googlecode.com/svn/trunk/ch02/DealDroidWithService/src/com/manning/aip/dealdroid/DealService.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.

这个例子的警告是它处理唤醒锁间隙(如果你需要,优秀的 CommonsWare 代码会这样做),但它可能会给你更多关于如何处理的想法可能会解决使用 AlarmManager 和服务进行轮询"的问题.

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.

更新:代码现在在这里:https://github.com/charlieCollins/android实践中

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

这篇关于android:使用 AlarmManager 运行后台任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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