2

I have a Runnable inside an Android Service and the service is bound to an activity.

The problem is that when the screen gets locked, the runnable pauses. It resumes after the screen is unlocked.

Following is the code for the runnable -

private Runnable counterRunnable = new Runnable() {
        @Override
        public void run() {

            totalTime = SystemClock.uptimeMillis() - lngStartTime;
            totalTime += lngPauseTime;
            strMillis = String.valueOf(((Integer.valueOf(String.valueOf(totalTime))) / 10) % 60);
            strSecs = String.valueOf(((Integer.valueOf(String.valueOf(totalTime))) / 1000) % 60);
            strMins = String.valueOf((((Integer.valueOf(String.valueOf(totalTime))) / 1000) / 60) % 60);
            strHrs = String.valueOf(((((Integer.valueOf(String.valueOf(totalTime))) / 1000) / 60) / 60) % 60);

            if(strMillis.length() == 1)
                strMillis = "0" + strMillis;
            if(strSecs.length() == 1)
                strSecs = "0" + strSecs;
            if(strMins.length() == 1)
                strMins = "0" + strMins;
            if(strHrs.length() == 1)
                strHrs = "0" + strHrs;

            Log.i(TAG, strHrs + ":" + strMins + ":" + strSecs + ":" + strMillis);
            hndlrUpdateTime.postDelayed(this, 10);
        }
    }; 

I want to know the reason why the runnable is pausing when the screen pauses.

krtkush
  • 1,378
  • 4
  • 23
  • 46
  • I guess the android OS is killing it. You may want to use a service with a visible notification so android wont kill it. – Pedro Lobito Sep 28 '15 at 06:29
  • Now take a look at it the way around. What would happen if all of the apps I started would keep running after my phone goes to deep sleep? You would figure out that your battery would not last long, so this happening is intentionally. You can request a partial wake look or just extend WakefulIntentService, but the way I see your problem, it does not need to keep running. You can tell the time difference when your phone is turned on again and properly update the labels. – Bojan Kseneman Sep 29 '15 at 08:48

2 Answers2

1

Your runnable is attached to the process that created it.

The process is stopped when you lock the screen.

For more info take a look at this.

If you want to avoid it look at this.

Hope this helps.

Community
  • 1
  • 1
Nanoc
  • 2,381
  • 1
  • 20
  • 35
0

This is the way android Application should work , when the screen locks

  onPause() 

is called , try to override this method and leave it empty , but be careful this may solve your problem and create 100 more .

Sarah Maher
  • 790
  • 1
  • 10
  • 21