Last active
August 22, 2017 14:18
-
-
Save crgarridos/f951ccf77161b5d42522b8a050172d0b to your computer and use it in GitHub Desktop.
turn on and unlockscren for android programmatically
This file contains hidden or 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
<uses-permission android:name="android.permission.WAKE_LOCK" /> | |
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/> |
This file contains hidden or 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
abstract class BaseActivity : AppCompatActivity() { | |
fun unlockScreen() { | |
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD) | |
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED) | |
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON) | |
} | |
@Suppress("DEPRECATION") | |
fun turnScreenOn() { | |
val powerManager = applicationContext.getSystemService(Context.POWER_SERVICE) as PowerManager | |
val result = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH && powerManager.isInteractive | |
|| Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT_WATCH && powerManager.isScreenOn | |
if (!result) { | |
val wl = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK or PowerManager.ACQUIRE_CAUSES_WAKEUP or PowerManager.ON_AFTER_RELEASE, "MH24_SCREENLOCK") | |
wl.acquire(10000) | |
val wl_cpu = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MH24_SCREENLOCK") | |
wl_cpu.acquire(10000) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment