スクリーンロックを解除する方法
方法
KeyguardManager.KeyguardLockクラスとPowerManager.WakeLockクラスを使います
KeyguardManager.KeyguardLock
スクリーンロックをはずすためのクラスです。
パーミッションにDISABLE_KEYGUARDが必要
PowerManager.WakeLock
スクリーンを明るくするためのクラスです。
実際にはスクリーンを明るくするためだけでなく、CPUの動きをどうするかという重要な役割があったりするんですけど
今回はスクリーンを明るくするために使用します。
パーミッションにWAKE_LOCKが必要
ソース
package com.tomorrowkey.android.simpletimer; import android.app.KeyguardManager; import android.content.Context; import android.os.PowerManager; public class ScreenLockManager { private Context mContext; private KeyguardManager.KeyguardLock mKeyguardLock; private boolean isScreenLock; private PowerManager.WakeLock mWakelock; public ScreenLockManager(Context context) { mContext = context; } public void getUnlock() { // acquire wake lock PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); mWakelock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "SimpleTimer"); mWakelock.acquire(); // unlock screen KeyguardManager km = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE); mKeyguardLock = km.newKeyguardLock(Log.TAG); if (km.inKeyguardRestrictedInputMode()) { mKeyguardLock.disableKeyguard(); isScreenLock = true; } else { isScreenLock = false; } } public void releaseUnlock() { // release screen if (isScreenLock) { mKeyguardLock.reenableKeyguard(); isScreenLock = false; } // release wake lock if (mWakelock.isHeld()) { mWakelock.release(); } } }
相変わらず命名センスないですorz
注意点
ロックを解除したあとには、必ずロックをかけること!
端末が意図しない動きを始めます。おそろしや