Question

I created a BroadcastReceiver to turn the screen on from an AlarmManager. Here is the code of my OnReceive.

@Override
public void onReceive(Context arg0, Intent arg1) {
    Log.i("", "OnReceive");

    // Initializes the PowerManager.
    PowerManager powerManager = (PowerManager) arg0
            .getSystemService(Context.POWER_SERVICE);

    // Wakes device.
    WakeLock wakeLock = powerManager.newWakeLock(
            PowerManager.FULL_WAKE_LOCK
                    | PowerManager.ACQUIRE_CAUSES_WAKEUP
                    | PowerManager.ON_AFTER_RELEASE, "Stretching");
    wakeLock.acquire();
    wakeLock.release();
    wakeLock = null;

    // Calls activity.
    Intent intent = new Intent(arg0, TrainingActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    arg0.startActivity(intent);
}

The problem occurs because FULL_WAKE_LOCK is deprecated, so I tried to use PARTIAL_WAKE_LOCK, but then the screen doesn't turn on. What should I use instead? I searched this How to turn screen on during partial wake lock but it is also using FULL_WAKE_LOCK.

Was it helpful?

Solution

Either use the deprecated FULL_WAKE_LOCK (and be prepared for your app to potentially no longer work in the future), or do not write the app. PARTIAL_WAKE_LOCK specifically does not turn on the screen.

OTHER TIPS

Unfortunately the FULL_WAKE_LOCK has been deprecated in favour of battery energy saving.

The only plausible workaround is to test for android version and if the version is earlier than KitKat i.e. Lollipop and up (up till KitKat FULL_WAKE_LOCK has been working fine but deprecated in API LEVEL 17) you can use FULL_WAKE_LOCK otherwise use PARTIAL_WAKE_LOCK. Like this:

    if (wakeLock != null) wakeLock.release();

    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(
        (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT ? PowerManager.PARTIAL_WAKE_LOCK : PowerManager.FULL_WAKE_LOCK) |
        PowerManager.ACQUIRE_CAUSES_WAKEUP |
        PowerManager.ON_AFTER_RELEASE, "Some Tag");

    wakeLock.acquire(10000);

Hope that helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top