Skip to content Skip to sidebar Skip to footer

Keep Activity Running Over Screen Lock When The Screens Alseep

So i built an app that functions as a lock screen replacement. I use a broadcast receiver and a service to start my activity after Intent.ACTION_SCREEN_OFF. So that every time the

Solution 1:

What if you use a wakelock. For example:

@OverridepublicvoidonCreate(Bundle savedInstanceState) { 
    PowerManagerpm= (PowerManager) getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLockwl= pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
    wl.acquire();
    // do your things, even when screen is off
}


@OverrideprotectedvoidonDestroy() {
    wl.release();
}

You must also have a wakelock permission is AndroidManifest.xml

uses-permission android:name="android.permission.WAKE_LOCK"

Solution 2:

One way you might want to try is to make sure you app never sleeps. On short sleeps it will stay running. On long sleeps your app itself is asleep. I was able to get around this myself with using the PowerManager.Wakelock. Only issue is that this will drain more battery if your app is using cpu cycles.

/** wake lock on the app so it continues to run in background if phone tries to sleep.*/
PowerManager.WakeLock wakeLock;

@OverridepublicvoidonCreate(Bundle savedInstanceState) {
    ...

    // keep the program running even if phone screen and keyboard go to sleepPowerManagerpm= (PowerManager) getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);

    ...
}

// use this when screen sleeps
wakeLock.acquire();

// use this once when phone stops sleeping
wakeLock.release();

Post a Comment for "Keep Activity Running Over Screen Lock When The Screens Alseep"