调度周期性任务的Andr​​oid [英] Scheduling recurring task in Android

查看:222
本文介绍了调度周期性任务的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计一个应用程序,能够发送presence到一个专用的服务器,只要应用程序是在前台的重复任务。

I'm designing an app that has a recurring task of sending presence to a dedicated server as long as the app is in foreground.

在我的整个网络搜索,我看到了一些不同的方法,想知道什么是这样做的最好方法。

in my searches across the web I saw a few different approaches and wanted to know what is the best way of doing this.

什么是安排一个服务器呼叫的最佳方法是什么?

我看到的是选项:

  1. 定时器

<一个href="http://developer.android.com/reference/java/util/concurrent/ScheduledThreadPoolExecutor.html">ScheduledThreadPoolExecutor.

服务

BroadcastReciever与<一个href="http://developer.android.com/reference/android/app/AlarmManager.html">AlarmManager.

BroadcastReciever with AlarmManager.

你对此有何看法?

编辑:
我需要的,这是一个基于聊天应用程序,将所有的用户操作到远程服务器的原因。
即用户键入消息,用户在阅读邮件时,用户在线时,用户下线等。


the reason I need this is for a chat based app that sends all the user actions to a remote server.
i.e. user is typing a message, user is reading a message, user is online, user is offline etc.

这意味着,每间隔,我要送什么我做的服务器,因斯我打开一个聊天室与其他人,他们需要知道我在做什么。

this means that once every interval, I need to send the server what I'm doing, ince I open a chat-room with other people, they need to know what I'm doing.

类似WhatsApp的信息反馈机制:

similar to the whatsapp message feedback mechanism:

推荐答案

我不知道,但按我的知​​识,我分享我的看法。我总是接受最好的答案,如果我错了。

I am not sure but as per my knowledge I share my views. I always accept best answer if I am wrong .

报警管理

报警管理器拥有CPU唤醒锁,只要报警接收机的的onReceive()的方法执行。这保证了手机不会睡觉,直到你完成处理广播。一旦的onReceive()返回时,报警管理器释放该唤醒锁。这意味着,在某些情况下,手机会尽快为您的onReceive()方法完成睡觉。如果您的报警接收器叫做 Context.startService(),有可能手机会睡的请求的服务启动之前。为prevent这一点,你的的BroadcastReceiver 服务将需要实现一个单独的唤醒锁的政策,以保证手机继续运行,直到服务变得可用。

The Alarm Manager holds a CPU wake lock as long as the alarm receiver's onReceive() method is executing. This guarantees that the phone will not sleep until you have finished handling the broadcast. Once onReceive() returns, the Alarm Manager releases this wake lock. This means that the phone will in some cases sleep as soon as your onReceive() method completes. If your alarm receiver called Context.startService(), it is possible that the phone will sleep before the requested service is launched. To prevent this, your BroadcastReceiver and Service will need to implement a separate wake lock policy to ensure that the phone continues running until the service becomes available.

注:报警管理的目的是为您希望让自己的应用code运行在特定的时间的情况下,即使你的应用程序当前未运行。对于正常的定时操作(蜱,超时,等等)更容易和更有效地使用处理程序

Note: 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. For normal timing operations (ticks, timeouts, etc) it is easier and much more efficient to use Handler.

定时器

timer = new Timer();

    timer.scheduleAtFixedRate(new TimerTask() {

        synchronized public void run() {

            \\ here your todo;
            }

        }}, 60000, 60000);

定时器有一些缺陷是由的ScheduledThreadPoolExecutor 解决。因此,这不是最好的选择。

Timer has some drawbacks that are solved by ScheduledThreadPoolExecutor. So it's not the best choice

的ScheduledThreadPoolExecutor

您可以使用 java.util.Timer中的的ScheduledThreadPoolExecutor (preferred)安排一个动作发生在后台线程定期。

You can use java.util.Timer or ScheduledThreadPoolExecutor (preferred) to schedule an action to occur at regular intervals on a background thread.

下面是一个使用后者的一个例子:

Here is a sample using the latter:

ScheduledExecutorService scheduler =
    Executors.newSingleThreadScheduledExecutor();

scheduler.scheduleAtFixedRate
      (new Runnable() {
         public void run() {
            // call service
         }
      }, 0, 10, TimeUnit.MINUTES);

所以我preferred ScheduledExecutorService的

不过想想也是,如果当你的应用程序运行将发生更新,您可以使用定时器,正如在其他的答案,或较新的的ScheduledThreadPoolExecutor 。 如果您的应用程序将更新它不运行,即使,你应该去 AlarmManager

But Also think about that 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.

这篇关于调度周期性任务的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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