1
在Android 6.0項目上,我嘗試使用接近傳感器解鎖手機。disableKeyguard在應用程序上調用凍結手機
這是我對OnSensorChanged事件:
public void onSensorChanged(SensorEvent event) {
proxValue = (event.values[0]); //sensor value stored in proxValue
keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);
lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
screenLock = ((PowerManager)getSystemService(POWER_SERVICE)).newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
/*when service is running and phone is locked
and proximity sensor value is less than 2.0 centimeters*/
if(isMyServiceRunning(StartStopService.class) && lockStatus == true && proxValue < 2){
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(500); // vibrate for 500 ms
screenLock.acquire(); //to wake the phone up
lock.disableKeyguard(); //to unlock the phone
keyguardDisabled = true; // boolean to check keyguard status
}
}
在我ScreenReceiver我:
public class ScreenReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);
lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
screenLock = ((PowerManager)getSystemService(POWER_SERVICE)).newWakeLock(
PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
if((intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) || !(intent.getAction().equals(Intent.ACTION_USER_PRESENT)))
{
lockStatus = true;
if(keyguardDisabled){
lock.reenableKeyguard();
screenLock.release();
keyguardDisabled = false;
}
}
else
{
lockStatus = false;
}
}
}
當我打電話lock.disableKeyguard()時出現問題。它會解鎖併成功喚醒手機,但當按下主頁按鈕時手機會凍結。我知道我需要在某個時間點重新啓用鍵盤鎖,但是在哪裏?什麼可能是解決方案或有任何其他方式來解鎖手機編程?