哪一个更好ScheduledExecutorService的或AlarmManager android系统中? [英] Which is Better ScheduledExecutorService or AlarmManager in android?

查看:231
本文介绍了哪一个更好ScheduledExecutorService的或AlarmManager android系统中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个初学者,我开发一个Android应用程序,这将继续有一定的延迟(这是天)后,发送短信给用户。我想,一旦注册的用户应不考虑接受的事实短信他登录或不。该短信内容和手机号码是从database.So提取研究后,我发现两种方法

I am a beginner and I am developing an android application which will keep on sending SMS to the user after a certain delay (which is in days).I want that the user once registered should receive the SMS irrespective of the fact that he is logged in or not. The SMS content and mobile number are fetched from the database.So after researching I found two ways

  1. ScheduledExecutorService的

  1. ScheduledExecutorService

AlarmManager

AlarmManager

现在的问题是,该alarmManager将关闭时,手机关机或重新启动。这是用ScheduledExecutorService的太多了呢?有多少线程的线程池,我应该用在使用执行器服务?

The problem is that the alarmManager will shut down when the phone is switched off or rebooted. Is this the case with ScheduledExecutorService too? And how many threads should I use in the ThreadPool while using the Executor Service?

推荐答案

报警管理

报警管理器拥有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.

的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的

So I preferred ScheduledExecutorService

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.
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.

还有清洁香港出<一href="http://stackoverflow.com/questions/6558694/difference-between-alarmmanager-and-scheduledexecutorservice">this帖子。

这篇关于哪一个更好ScheduledExecutorService的或AlarmManager android系统中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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