当手机没有充电的应用程序的服务停止 [英] Service of an app stops when phone is not being charged

查看:171
本文介绍了当手机没有充电的应用程序的服务停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的动态调用 startservice启动的服务()。为了简化我的问题,可以说,该服务将是一个计数器,计数器将在每10秒增加。

My Activity starts a service by calling startservice(). To simplify my problem lets say the service will be a counter, and the counter will be increased in every 10 sec.

Timer t_counter;
int counter = 0; 

@Override   
public int onStartCommand(Intent intent, int flags, int startId) {

    t_counter = new Timer();
    t_counter.schedule(new TimerTask() { 
        @Override
        public void run() {
            counter++;
            Log.d("counter: ",Integer.toString(counter));
        }}, 0, 10000);

    return Service.START_STICKY;
}   

当手机正在充电,(或在调试模式 - 因为我能看到的的logcat )的服务工作正常。在周围的每一个10秒的logcat 显示调试信息,当应用程序在后台与否。但是,当我已经拔掉了电话,服务一段时间后,停止运行。事件在应用程序(活动赖以起家的服务)是活跃。需要注意的是服务不被破坏,只是暂时搁置,或者这样的事情。

When the phone is being charged, (or in debug mode - since I can see the the Logcat) the service works as expected. In around every 10 sec Logcat shows the debug info, whenever the app is in background or not. But when I have unplugged the phone, the service stops running after a while. Event when the app (Activity which started the service) is active. Note that service not destroyed, just put on hold, or something like this.

由于当我插上移动再次,计时器将继续和计数器的值是从那里我刚刚拔掉电话的价值正在增加。因此,如果该服务已被破坏则值将会再次为零。 (我也调试服务的生命周期,并不能看到 onStartCOmmand()的onDestroy()会被称为)

Because when I plug in the mobile again, the timer continues and the value of the counter is being increased from the value where I just unplugged the phone. So if the service has been destroyed then value would have been zero again. (also I debugging the lifecycle of the service, and cannot see onStartCOmmand(), onDestroy() would have been called )

我已经寻找它的解决方案,但我想我还没有恼火这种行为的正确答案。 我知道,我应该使用<一个href="http://stackoverflow.com/questions/8713361/keep-a-service-running-even-when-phone-is-asleep"><$c$c>AlarmManager定时器来代替。或者,它也将工作,如果我把服务前景通过 startForeground(),或者单独过程会解决这个问题。不过,我想知道为什么我的解决方案正在与充电。此外,我在哪里可以找到关于服务的这个空闲状态的相关信息。 (不执行定时器的时间表,但不被破坏)谢谢!

I have searched solutions for it, but I think I have not het the right answer for this behavior. I know that I should use AlarmManager instead of Timer. Or it would also work if I put the service foreground by startForeground(), or maybe separate process would solve this problem. But I would like to know why my solution is working with charging. Also where can I find infos about this "idle" state of a service. (not executing timer schedules, but not destroyed) Thanks!

推荐答案

您需要持有锁定如果你的服务必须运行在后台

You need to hold lock if your service has to be running in the background

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = 
                      pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
wl.acquire();
// when you done
wl.release();

更好的方法是使用 AlarmManager ,因为保持业务​​运行的所有的时间会耗尽电池,也浪费了系统资源。

Better way is to use AlarmManager because keep the service running all the time will drain the battery and waste the system resources.

这篇关于当手机没有充电的应用程序的服务停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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