Turning on screen from receiver/service
Asked Answered
C

3

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

Charlatanry answered 14/5, 2015 at 20:9 Comment(2)
'I see that WakeLock and KeyguardManager are deprecated.' add a link pleaseBrendin
The FULL lock need developer.android.com/reference/android/os/… and developer.android.com/reference/android/app/…Charlatanry
P
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);`
}
Payoff answered 21/5, 2015 at 5:58 Comment(7)
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.Charlatanry
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);Payoff
thanks, launching a new activity with these setting works, if you put this as the solution i will mark as correct.Charlatanry
Done, sorry I was away. Should have bounty now, thanks for your helpCharlatanry
@Kartheek: Does it work when I set password or pattern for screen's locking?Brake
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?Wilke
how is this the accepted answer. getWindow() doesn't exist in a servicePreconceive
B
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.

Buckram answered 14/5, 2015 at 20:13 Comment(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 nowCharlatanry
S
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) {

    }
}
Spokeswoman answered 17/5, 2015 at 11:8 Comment(4)
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 paramsCharlatanry
did you try the turnScreenOn function? That one I used in GCM message receiver if a certain type of message was received (priority based)Spokeswoman
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.Charlatanry
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 :)Spokeswoman

© 2022 - 2024 — McMap. All rights reserved.