在Services定时VS线程休眠 [英] Timer Vs thread sleep in Services

查看:135
本文介绍了在Services定时VS线程休眠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查每15分钟订阅RSS文件,以更新我的应用程序的结果;我知道我必须实现一个服务,但我想知道哪种方法我应该使用要做到这一点;它是更好地实现一个计时器( http://developerlife.com/tutorials/?p=356)或服务中的一个线程( http://www.youtube.com/watch?v = yfWsp9IHX1Y )?
我尝试的第一个教程,但我得到的错误无法创建内螺纹尚未调用Android中活套prepare处理...
我该怎么办?保持棒与第一种方法或者尝试另一种?

I need to check every 15 minutes a feed RSS file in order to update my application's results; I know I have to implement a service, but I was wondering which approach I should use to do this; is it better to implement a timer (http://developerlife.com/tutorials/?p=356) or a thread inside the service (http://www.youtube.com/watch?v=yfWsp9IHX1Y)? I tried the first tutorial but I get the error "Can’t create handler inside thread that has not called Looper prepare in Android"... What should I do? Stay stick with the first method or try the other one?

推荐答案

您应该使用的的PendingIntent 并与 AlarmManager 注册它是开始在未来15分钟。这样,你的应用程序将不使用任何ressources直到火警。

You should use a PendingIntent and register it with the AlarmManager to be started 15 minutes in the future. This way your app won't use any ressources until the alarm fires.

您需要使用您开始服务创建的PendingIntent的意图。服务完成后,您需要重新注册的PendingIntent为您服务的下一次执行。

You need to use the Intent that starts your Service to create the PendingIntent. After your service finished you need to reregister the PendingIntent for the next execution of your service.

这篇关于在Services定时VS线程休眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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