活动和使用处理服务 [英] Activities and services using handlers

查看:80
本文介绍了活动和使用处理服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

喜 我想在一个应用程序中使用多种服务,我想从一个服务获取更改后的值后,重新运行一段时间后,服务。 请指导我如何使用处理器/定时器/广播接收器实现这一点。

Hi I am trying to use several services in an application, i wish to re-run the services after a certain period of time after fetching the changed values from one of the services. Pls guide me on implementing this using handlers/timers/broadcast receivers.

感谢

推荐答案

您应该使用发送未决意图的AlarmManager。您可以使用多个意图和广播recievers。在广播recievers就可以启动服务。这样,您就可以在不同的时间,如果你想启动多个服务或在一起。

You should use an AlarmManager that sends pending intents. You can use multiple intents and broadcast recievers. In the broadcast recievers you can start a service. This way you can start multiple services at different times or together if you want.

您可以使用Alarmmanager发送重复报警,或者你可以在服务之一,使用它,并设置新的时间为您的待处理的意图。

You can use the Alarmmanager to send repeated alarms, or you could use it in one of the services and set a new time for your pending intent.

不要使用定时器!

做这种方式的好处是,这所有的作品时,活动不活跃。

The benefit of doing it this way is that this all works when the Activity is not active.

您可以绑定到一个服务中所示的例子,但如果你想从一个服务传递数据的活动,你可以只使用应用程序对象,或单。另外我会用一个IntentService,它会自动执行主线程的工作。如果你想要做的东西在不同的线程,应该更紧密地与活动互动,我会用一个AsyncTask的或螺纹。

You can bind to a service as shown in the example, but if you want to pass data from a service to an activity you can just use the application object, or a singleton. Also I would use an IntentService, which automatically does its work of the main thread. If you want to do something on a different thread that should more closely interact with the Activity I would use an AsyncTask or a Thread.

这篇关于活动和使用处理服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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