简体   繁体   English

android:使用AlarmManager运行后台任务

[英]android: running a background task using AlarmManager

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. 我已经看到一些使用AlarmManager来点击BroadcastReciever的例子,这似乎是正确的事情,但我似乎无法让它工作。

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 TIA

Here is one complete example: http://android-in-practice.googlecode.com/svn/trunk/ch02/DealDroidWithService/ 这是一个完整的例子: 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 这个例子使用的模式,以及我发现似乎运行良好的模式,是使用启动接收器来设置AlarmManager (当然也检查从主Activity开始轮询,对于你的情况应用程序已安装且系统未启动)并让AlarmManager向另一个接收器发送Intenthttp//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 然后从AlarmReceiver启动一个IntentServicehttp//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然后进行网络调用以轮询数据,或者您需要做什么。 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 IntentService自动将您的工作放在后台线程中,非常方便: 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. 这个例子的警告是它没有处理唤醒锁定间隙(如果你需要它,优秀的CommonsWare代码可以做到这一点),但是它可能会给你一些关于如何使用AlarmManager和服务进行“轮询”的更多想法。东西。

UPDATE: the code is now here: https://github.com/charlieCollins/android-in-practice 更新:代码现在在这里: https//github.com/charlieCollins/android-in-practice

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM