Skip to content Skip to sidebar Skip to footer

Timer Stops Every Time The Phone Sleeps

I'm creating this incredibly simple app, since I'm just starting with Android and I have no experience with Java what so ever. Anyhow, all I have is a timer and a button. Obviously

Solution 1:

You need to make this using BroadCastRecievers for system-calls. (Related Helpful question)

You can also play off the life-cyles of the Activity using the PowerManager.


Example using PowerManager:

@OverrideprotectedvoidonPause()
{
    super.onPause();

    // If the screen is off then the device has been lockedPowerManagerpowerManager= (PowerManager) getSystemService(POWER_SERVICE);
    booleanisScreenOn= powerManager.isScreenOn();

    if (!isScreenOn) {
        // The screen has been locked// Continue your timer
    }
}

Post a Comment for "Timer Stops Every Time The Phone Sleeps"