Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Timer ticks late when phone screen locked

I have a service that runs a TimerTask in a method, which must count every second and do something after some time. My service works properly and when i start the method in service, timer starts to tick every second (1000 milliseconds). The problem is, when device's screen turns off, my timer stops sometimes and doesn't count properly. I can see in the log file that timer stops for 15 seconds, then runs 5 seconds, then stops again... When the device is connected to computer, timer always works normally, this problem occurs when i disconnect the device from computer.

Here's my service:

public class MyService extends Service {
    private String TAG = getClass().getName();

    public void count() {

        Log.i(TAG, "Service Count Method Start");

        int counter;
        Timer mTimer = new Timer();

        mTimer.schedule(new TimerTask() {

            @Override
            public void run() {


                Log.i(TAG, "Timer count value : " + counter);

                if (!someCondition) {
                    counter++;
                    if (DefaultApplication.notOnDisplayTime >= 180) {
                        Log.d(TAG, "APPLICATION REACHED TIME LIMIT!");
                        someCondition = true;

                    } else
                                someCondition = false;
                } else {
                    counter = 0;
                }
            }
        }, 0, 1000);

    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return mBinder;
    }

    public class MyServiceBinder extends Binder {
        public MyService getService() {
            return MyService.this;
        }
    }

    private final IBinder mBinder = new MyServiceBinder();
    @Override
    public boolean onUnbind(Intent intent) {
        // TODO Auto-generated method stub
        Log.i(TAG, "MyService Unbound");
        return super.onUnbind(intent);
    }
}

And here's the logcat output:

    04-25 14:35:22.951 I/com.mypackagename.MyService( 6745): Timer count value : 73
    04-25 14:35:23.949 I/com.mypackagename.MyService( 6745): Timer count value : 74
    04-25 14:35:24.019 D/dalvikvm( 6832): GC_FOR_MALLOC freed 6656 objects / 495856 bytes in 61ms
    04-25 14:35:24.951 I/com.mypackagename.MyService( 6745): Timer count value : 75
    04-25 14:35:25.951 I/com.mypackagename.MyService( 6745): Timer count value : 76

#### Screen is getting locked here ####

    04-25 14:35:26.929 I/power ( 96): *** set_screen_state 0]
    04-25 14:35:26.959 V/KeyguardViewMediator( 96): password timeout now
    04-25 14:35:26.959 D/KeyguardViewManager( 96): show()
    04-25 14:35:26.999 D/Sensors ( 96): close_akm, fd=138
    04-25 14:35:26.999 I/com.mypackagename.MyService( 6745): Timer count value : 77
    04-25 14:35:27.039 D/HtcLockScreen( 96): onScreenRestart
    04-25 14:35:27.039 I/HtcLockScreen( 96): updateStatusViewByPriority, mIsSimCheckView = false, mIsBatteryInfo = false, mIsMusicPlaying = false, mIsAirPlaneMode = false
    04-25 14:35:27.049 I/HtcLockScreen( 96): HtcLockScreen:onResume 
    04-25 14:35:27.069 D/SurfaceFlinger( 96): Layer::setBuffers(this=0x785580), pid=96, w=480, h=762
    04-25 14:35:27.079 D/SurfaceFlinger( 96): Layer::setBuffers(this=0x785580), pid=96, w=480, h=762
    04-25 14:35:27.109 D/SurfaceFlinger( 96): Layer::requestBuffer(this=0x785580), index=0, pid=96, w=480, h=762 success
    04-25 14:35:27.339 D/alogcat ( 6832): stopping ...
    04-25 14:35:27.339 D/alogcat ( 6832): paused
    04-25 14:35:27.609 D/SurfaceFlinger( 96): About to give-up screen, flinger = 0xb4e28
    04-25 14:35:27.669 D/AK8973 ( 72): Compass CLOSE

#### Screen Locked ####

    04-25 14:35:27.949 I/com.mypackagename.MyService( 6745): Timer count value : 78
    04-25 14:35:28.949 I/com.mypackagename.MyService( 6745): Timer count value : 79
    04-25 14:35:44.602 I/com.mypackagename.MyService( 6745): Timer count value : 80
    04-25 14:35:45.603 I/com.mypackagename.MyService( 6745): Timer count value : 81
    04-25 14:35:45.784 I/wpa_supplicant( 256): CTRL-EVENT-SCAN-RESULTS Ready
    04-25 14:35:45.799 D/LocationMasfClient( 96): getNetworkLocation(): Returning cache location with accuracy 75.0
    04-25 14:35:46.603 I/com.mypackagename.MyService( 6745): Timer count value : 82
    04-25 14:35:47.603 I/com.mypackagename.MyService( 6745): Timer count value : 83
    04-25 14:35:48.604 I/com.mypackagename.MyService( 6745): Timer count value : 84
    04-25 14:35:49.604 I/com.mypackagename.MyService( 6745): Timer count value : 85
    04-25 14:36:10.558 D/SurfaceFlinger( 96): Layer::requestBuffer(this=0x785580), index=1, pid=96, w=480, h=762 success
    04-25 14:36:11.033 I/com.mypackagename.MyService( 6745): Timer count value : 86
    04-25 14:36:13.269 I/com.mypackagename.MyService( 6745): Timer count value : 87
    04-25 14:36:13.289 D/com.mypackagename.utils.XUtil( 6745): return 0 char $
    04-25 14:36:14.039 D/com.mypackagename.utils.XUtil( 6745): return 0 char $
    04-25 14:36:14.269 I/com.mypackagename.MyService( 6745): Timer count value : 88
    04-25 14:36:17.009 I/com.mypackagename.MyService( 6745): Timer count value : 89
    04-25 14:36:29.512 I/com.mypackagename.MyService( 6745): Timer count value : 90

You can see that after the screen locked, timer doesn't seem to tick every second. It stops for 15 - 20 seconds periods then works normally for 5 seconds...

Any opinion about how to prevent timer to stop when device screen locked?

like image 342
Yasin YILDIRIM Avatar asked Apr 25 '11 12:04

Yasin YILDIRIM


1 Answers

Sounds like you need a Partial Wake Lock This will stop the CPU from going into a sleep state when the screen is off. Be warned though, misuse can cause battery issues and you need a permission to use wake locks.

like image 64
Mike dg Avatar answered Nov 06 '22 05:11

Mike dg