Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the proper, non-deprecated way to wake up the device?

My requirement is: after a GCM message arrives, the device should wake up to display a high-priority notification. The device should turn the screen on.

Currently I'm using WakeLock to achieve this. The newWakeLock() method expects a lock level AND a flag to be passed (as the 1st param, bitwise or'd).

I'm using PowerManager.ACQUIRE_CAUSES_WAKEUP flag since it does exactly what I need. However, I'm a bit frustrated about the lock level. So according to the docs, I got the following options:

  • PARTIAL_WAKE_LOCK - not compatible with ACQUIRE_CAUSES_WAKEUP / doesn't turn the screen on
  • SCREEN_DIM_WAKE_LOCK - deprecated
  • SCREEN_BRIGHT_WAKE_LOCK - deprecated
  • FULL_WAKE_LOCK - deprecated

The suggested FLAG_KEEP_SCREEN_ON is completely useless in this scenario. I ended up just supressing the deprecation warning:

@SuppressWarnings("deprecation")
PowerManager.WakeLock screenOn = ((PowerManager) c.getSystemService(Context.POWER_SERVICE)).newWakeLock(
PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, TAG);
screenOn.acquire();
mNotifyMgr.notify(mNotificationId, mBuilder.build());
screenOn.release();

The question: is there a non-deprecated reliable way to wake up the device in the described case?

EDIT I'm not asking for workarounds to wake up the device. My question is whether this is possible to wake up the device from the background (without a running Activity) using no deprecated APIs

like image 649
Droidman Avatar asked May 21 '15 09:05

Droidman


People also ask

What is wake lock in android?

A wakelock is a powerful concept in Android that allows the developer to modify the default power state of their device. The danger of using a wakelock in an application is that it will reduce the battery life of a device.

What is PARTIAL_ wake_ lock?

PARTIAL_WAKE_LOCK. Wake lock level: Ensures that the CPU is running; the screen and keyboard backlight will be allowed to go off.

How to prevent screen from sleeping android studio?

Using android:keepScreenOn="true" is equivalent to using FLAG_KEEP_SCREEN_ON . You can use whichever approach is best for your app. The advantage of setting the flag programmatically in your activity is that it gives you the option of programmatically clearing the flag later and thereby allowing the screen to turn off.

When partial wake locks are being acquired the android goes into?

Partial wake locks are a mechanism in the PowerManager API that lets developers keep the CPU running after a device's display turns off (whether due to system timeout or the user pressing the power button). Your app acquires a partial wake lock by calling acquire() with the PARTIAL_WAKE_LOCK flag.


1 Answers

Use the code I got from my question, and then just finish the activity, should leave the screen on for the users normal amount of time. Trust me this is the only way, after spending a good week on this problem. You could always set the activity to transparent with notitlebar, the user will never know.

@Override
protected void onCreate(Bundle savedInstanceState) {
    getWindow().addFlags(
        WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
        WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
        WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
        WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
        WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
    finish();
}
like image 107
RuAware Avatar answered Oct 12 '22 09:10

RuAware