跑在单独的线程服务和觉醒它每隔10分钟? [英] Running a service in separate thread and waking it every 10 minutes?

查看:139
本文介绍了跑在单独的线程服务和觉醒它每隔10分钟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序将访问一个Web服务来访问数据(即使该应用程序是不是在前台),每次10分钟。什么是做到这一点的最好办法?

My app will access a webservice to access data (even if the application is not in foreground), every 10 minutes. What is the best way to do that?

首先,我需要开始我的服务seaparate线程?
而如何使其从服务器更新每隔10分钟?有人说handler.postdelayed有的使用报警管理器。哪一个更好?我们有一些例子。

First do I need to start my service in seaparate thread? And how to make it get update from server every 10 minutes? Some people said handler.postdelayed and some using Alarm Manager. which one is better and do we have some examples.

推荐答案

如果在其他的答案建议你的应用程序运行时会发生的更新,你可以使用一个定时器,或较新的<一个href=\"http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ScheduledThreadPoolExecutor.html\"相对=nofollow> 的ScheduledThreadPoolExecutor 。结果
如果你的应用程序会更新,即使它没有运行,你应该与 <$走C $ C> AlarmManager

If the updates will occur while your application is running, you can use a Timer, as suggested in other answers, or the newer ScheduledThreadPoolExecutor.
If your application will update even when it is not running, you should go with the AlarmManager:

报警管理器是为您希望有你的情况下,
  在特定的时间应用code运行,即使您的应用程序
  目前没有运行。

The Alarm Manager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running.

就拿注意,如果你打算当应用程序被关闭,每十分钟更新一次非常频繁,因此可能有点太耗电。

Take note that if you plan on updating when your application is turned off, once every ten minutes is quite frequent, and thus possibly a bit too power consuming.

这篇关于跑在单独的线程服务和觉醒它每隔10分钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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