Created
February 7, 2015 01:59
-
-
Save JakeWharton/f50f3b4d87e57d8e96e9 to your computer and use it in GitHub Desktop.
Rise and Shine™, unlock and wake up your device automatically when you deploy from the IDE. Put this somewhere in your `src/debug/` code and run it when the application or main activity starts. Apache 2.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Show the activity over the lockscreen and wake up the device. If you launched the app manually | |
* both of these conditions are already true. If you deployed from the IDE, however, this will | |
* save you from hundreds of power button presses and pattern swiping per day! | |
*/ | |
public static void riseAndShine(Activity activity) { | |
activity.getWindow().addFlags(FLAG_SHOW_WHEN_LOCKED); | |
PowerManager power = (PowerManager) activity.getSystemService(POWER_SERVICE); | |
PowerManager.WakeLock lock = | |
power.newWakeLock(FULL_WAKE_LOCK | ACQUIRE_CAUSES_WAKEUP | ON_AFTER_RELEASE, "wakeup!"); | |
lock.acquire(); | |
lock.release(); | |
} |
Hi, Sorry for this question but I am a novice here. I need to wake my app using a certain device code (i.e a hardware interrupt when I plug in a certain hardware through the device power port), Is there a way I can do it ?, any help would be appreciated, thanks
I've noticed some weird behavior when using this. Sometimes it goes back to the lock screen, especially when starting a new Activity, and especially when I have a secured lock screen.
Or maybe I don't use it well? I called it for the first Activity that starts on the app.
Here's the same code after converting to Kotlin and fixing the issues the IDE reports about:
@JvmStatic
fun wakeUp(activity: Activity) {
activity.window.addFlags(FLAG_SHOW_WHEN_LOCKED)
val power = activity.getSystemService(POWER_SERVICE) as PowerManager
val lock = power.newWakeLock(PowerManager.FULL_WAKE_LOCK or PowerManager.ACQUIRE_CAUSES_WAKEUP
or PowerManager.ON_AFTER_RELEASE, activity.packageName + ":wakeup!")
lock.acquire(1000)
lock.release()
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is there a reason that you don't just unlock the device when pushing from the IDE? https://gist.github.com/androidfu/3ae247867b68d6ead7dc