21

I would like my app to be able to turn the screen on and display my app. Let's say I'm setting an alarm and every hour I want my app to be displayed for 2 mins before the device naturally sleeps.

I see that WakeLock (FULL_LOCK) and KeyguardManager are deprecated.

I have created a WakefulBroadcastReceiver and service and these are working.

@Override
protected void onHandleIntent(Intent intent) {
    // I need to show the screen here!

    for (int i=0; i<5; i++) {
        Log.i("SimpleWakefulReceiver", "Running service " + (i + 1)
                + "/5 @ " + SystemClock.elapsedRealtime());
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
        }
    }
    Log.i("SimpleWakefulReceiver", "Completed service @ " + 
          SystemClock.elapsedRealtime());
    SimpleWakefulReceiver.completeWakefulIntent(intent);
}

How do I programmatically turn on the screen, get past lock and display my Activity from the IntentService ?

Thanks

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
RuAware
  • 979
  • 1
  • 9
  • 26
  • 'I see that WakeLock and KeyguardManager are deprecated.' add a link please – Mr_and_Mrs_D May 16 '15 at 14:29
  • The FULL lock need http://developer.android.com/reference/android/os/PowerManager.html#FULL_WAKE_LOCK and http://developer.android.com/reference/android/app/KeyguardManager.KeyguardLock.html – RuAware May 17 '15 at 07:57

3 Answers3

27

You can use this code to turn the screen on.

lock = ((KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE)).newKeyguardLock(KEYGUARD_SERVICE);
powerManager = ((PowerManager) getSystemService(Context.POWER_SERVICE));
wake = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");

lock.disableKeyguard();
wake.acquire();
           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);

You need the following permission in AndroidManifest.xml file:

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />

EDIT (USE THIS ONE, NOTHING IS DEPRECATED):
There is an one more alternative for doing this, for that you need to launch an activity, In the activity onCreate() you need to add the flags to the window. For example:

   @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);`
}
RuAware
  • 979
  • 1
  • 9
  • 26
Kartheek
  • 7,104
  • 3
  • 30
  • 44
  • Thanks, but please see that the PowerManager.FULL_WAKE_LOCK amd Keyguard are depreciated. I want an app that once they remove this it still works. WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD will get you around the keyguard. but still need to wake the display. – RuAware May 21 '15 at 17:37
  • 1
    There is an one more alternative for doing this, for that you need to launch an activity, In the activity onCreate() you need to add the flags to the window., and FLAG_DISMISS_KEYGUARD is not deprecated. 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); – Kartheek May 22 '15 at 05:07
  • thanks, launching a new activity with these setting works, if you put this as the solution i will mark as correct. – RuAware May 22 '15 at 11:47
  • Done, sorry I was away. Should have bounty now, thanks for your help – RuAware May 24 '15 at 13:32
  • @Kartheek: Does it work when I set password or pattern for screen's locking? – user3051460 Sep 25 '16 at 10:08
  • It works perfectly (the edit with 5 flags), however, some flags seem to be deprecated now. As the javadoc suggests, the deprecated FLAG_SHOW_WHEN_LOCKED can be replaced by activity.setShowWhenLocked(true), which seems to work fine. When I do the same for FLAG_TURN_SCREEN_ON - replaced by activity.setTurnScreenOn(true) - the screen won't turn on anymore. Anyone got a suggestion to make this last piece of the puzzle work? – P Kuijpers Mar 06 '18 at 16:20
  • how is this the accepted answer. `getWindow()` doesn't exist in a service – Hilikus Jan 15 '21 at 23:43
2

I don't know what you're talking about, wakelock is definitely not deprecated. Certain types are no longer the Google preferred way of doing things, but normal wakelocks are still around and still the easiest way of doing this. Make sure to add the ACQUIRE_CAUSES_WAKEUP flag when taking the lock. In fact notice that a WakefulBroadcastReceiver is implemented by using wakelocks.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • 1
    I can wake the screen with PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP But PowerManager.FULL_WAKE_LOCK depreciated in 17. Along with KeyguardManager, just looking for the correct way to do it now – RuAware May 14 '15 at 20:24
1

You can use this code to turn the screen on.

private void turnScreenOn() {
    int flags = WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
    getWindow().addFlags(flags);
}

You can use this code to keep it on until the wake lock is dimissed.

<uses-permission android:name="android.permission.WAKE_LOCK" />

private PowerManager mPowerManager;
private PowerManager.WakeLock mWakeLock;

@Override
public void onCreate() {
super.onCreate();
    mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mWakeLock = mPowerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "Service");
}

private void acquireWakeLock() {
    try {
        mWakeLock.acquire();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

private void releaseWakeLock() {
    try {
        mWakeLock.release();
    }
    catch (Exception e) {

    }
}
Bojan Kseneman
  • 15,488
  • 2
  • 54
  • 59
  • Thanks, I feel like i'm getting somewhere and have added something similar but PowerManager.SCREEN_DIM_WAKE_LOCK is depreciated along with BRIGHT and FULL you also have to make sure you add flags on UIthread so tried Receive msg -> run on ui add keep_screen_on -> get partial lock with Acquire wakes. But this does not turn the screen on, the only think that does is the depreciated params – RuAware May 17 '15 at 16:27
  • did you try the turnScreenOn function? That one I used in GCM message receiver if a certain type of message was received (priority based) – Bojan Kseneman May 17 '15 at 16:36
  • yeah without putting on uithread i get "Only the original thread that created a view hierarchy can touch its views." but still doesn't turn the stupid screen on! with any of the deprciated options though it does, seems to me they are planning to take away something and not give us a way to do it. – RuAware May 17 '15 at 16:44
  • Yeah, it was a security issue after all in my opinion. I could turn on the screen and dismiss the keyguard with GCM messages at the time of developing that app. I don't know how it works now, as I left that company and joined another :) – Bojan Kseneman May 17 '15 at 17:12