应用程序退出,以保持它在后台一段时间 [英] Application exit to keep it in background for some times

查看:244
本文介绍了应用程序退出,以保持它在后台一段时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Andr​​oid应用程序,其中i使用服务来使用定时器的时间间隔获取地点工作。但是,如果我把我在回地面应用了一段时间,然后应用程序退出其的onCreate()方式被重新叫我定时器停止,请告诉我,我怎么能继续运行我的应用程序为长时间。

 定时器=新的Timer();
TimerTask的=新的TimerTask(){
    @覆盖
    公共无效的run(){
    }
};
timer.schedule(TimerTask的,1000 * 60 * 4,1000 * 60 * 4);


解决方案

这对于较长时间运行的服务,是唯一值得它的执行(和能源消耗)的时间,如果它提供价值给用户不断。这不是一个位置的轮询,这仅适用于很短的时间,这取决于你的位置受访做什么创造价值的情况下。在这种情况下,你应该执行,将执行一个简短的任务(我指的是一般意义上的,而不是一个工作目标任务),然后你必须安排你的服务运行服务时。您可以使用Android的调度机制,名为<一个href=\"http://www.techrepublic.com/blog/app-builder/use-androids-alarmmanager-to-schedule-an-event/2651\"相对=nofollow> AlarmManager ,安排您的服务。

有一个问题inerent这种方法,虽然:当系统处于省电休眠状态,您的服务已完成版本的系统唤醒锁时获得唤醒锁,以便正确地执行,那么回去睡觉状态。实现这个唤醒锁定获取/释放机制并不是一个简单的任务。

我建议你使用,而不是自己实现一个Commonsware的位置轮询实施。这是很好的测试,并解决收购/释放为您服务,在后台执行唤醒锁的问题。

如果你在自己做轮询坚持(如把已经写好code使用的),我建议使用Commonsware的的,以避免编写自己的唤醒锁定获取/释放机制为您服务WakefulIntentService 。这是非常容易使用。

I am working on Android app where i using a service to fetch location on a time interval using timer. But if I keep my app in back ground for some time then application exit and its onCreate() method is re Called and my timer stops please tell me how I can keep running my application for long times.

timer  = new Timer();
timerTask = new TimerTask() {           
    @Override
    public void run() {                 
    }
};
timer.schedule(timerTask, 1000*60*4, 1000*60*4);

解决方案

A service that runs for a longer period of time is only worth its execution (and energy consumption) time if it delivers value to the user continually. It's not the case of a location poller, which only delivers value for short periods of time, depending on what you do with the polled location. In this case you should implement a service that will perform a short task (I mean task in the general sense, not a Task object) and then you must schedule your service to run from time to time. You can use Android's scheduling mechanism, called AlarmManager, to schedule your services.

There is a problem inerent to this approach, though: when the system is in battery-saving sleep state, your service has to acquire a wake lock in order to execute properly, then when finished release the wake lock for the system to go back to sleep state. Implementing this wake lock acquisition/release mechanism is not a trivial task.

I suggest you to use Commonsware's Location Poller implementation instead of implementing one yourself. It is well tested and solves the issue of acquiring/releasing a wake lock for your service to execute in the background.

If you insist in doing the polling yourself (e.g. to put already written code to use), I recommend using Commonsware's WakefulIntentService in order to avoid writing your own wake lock acquisition/release mechanism for your service. It's very easy to use.

这篇关于应用程序退出,以保持它在后台一段时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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