2

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;
}   

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.

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 )

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!

Community
  • 1
  • 1
laplasz
  • 3,359
  • 1
  • 29
  • 40

1 Answers1

3

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();

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

iTech
  • 18,192
  • 4
  • 57
  • 80
  • So is my problem in connection with charging? so when unplugged, system stops the service? (not distroy) – laplasz Feb 17 '13 at 18:16
  • The system might try to preserve resources when the phone is running on battery. If your app does not listen to `charge status` I do not see any other connection. – iTech Feb 17 '13 at 18:21
  • So where can i find info about this state of service? not destroyed just put in "idle" maybe it is running if the app is in foreground, I will test it - if so, it may connection with the process since app and service running on the same one if no other process is defined for the service – laplasz Feb 17 '13 at 18:27
  • 1
    @laplasz: "so when unplugged, system stops the service?" -- no, the CPU powers down (a.k.a., the device goes into sleep mode). – CommonsWare Feb 17 '13 at 18:47
  • @CommonsWare ok I found your [answer](http://stackoverflow.com/questions/5120185/android-sleep-standby-mode) related to sleep mode – laplasz Feb 17 '13 at 19:04
  • i spent 44 hours without sleeping thank you very much @iTech – Paratoner Feb 16 '23 at 18:07