Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wake and unlock Android phone screen when compile and run project?

When build and run iOS app using Xcode, the phone becomes awake and app runs. Is there a way to wake and unlock Android phone (or tablet) screen then run installed Android app (Eclipse, Android Studio)?

like image 328
vladof81 Avatar asked Mar 11 '14 17:03

vladof81


People also ask

How can I unlock my Android phone automatically?

In the Smart Lock settings menu, tap Trusted Places, then tap Home. Tap Turn on this location and you'll be asked to choose a "Home" address if you haven't already set one up. Set up other places to keep your phone unlocked by tapping Add trusted place.

How do I wake up my Android screen?

Turn on Lift to wake From Settings, search for and select Lift to wake. Tap the switch next to Lift to wake to turn this feature on. Note: Lift to wake replaced the Direct call feature available in previous Android versions.


1 Answers

One solution: set the following flags in your activity class(es):

    if (BuildConfig.DEBUG) {
        // These flags cause the device screen to turn on (and bypass screen guard if possible) when launching.
        // This makes it easy for developers to test the app launch without needing to turn on the device
        // each time and without needing to enable the "Stay awake" option.
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
    }

These flags will:

  1. Turn on the screen
  2. Bypass the lock-screen
  3. Allow showing the activity even if the device is locked

By setting those flags in your base activity, you'll be able to continue using your application even if the device was off and/or locked at the time of running. If you try to leave your application's process (i.e., hitting the home button, or switching to another app), the lock screen will appear, and you'll have to manually unlock to continue using the device.

Warning: this should only be used while developing/debugging your application, so I recommend keeping the if (BuildConfig.DEBUG) check, as it is in this example

like image 74
Joe Avatar answered Oct 26 '22 14:10

Joe