对应用程序的 disableKeyguard 调用冻结 phone
disableKeyguard call on application freezes phone
在 Android 6.0 项目中,我正在尝试使用接近传感器解锁 phone。
这是我对 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() 时出现问题。它成功解锁并唤醒了 phone,但是当我按下主页按钮时 phone 冻结了。我知道我需要在某个时候重新启用键盘锁,但是在哪里?解决方案是什么,或者是否有任何其他方法可以通过编程方式解锁 phone?
如果有人遇到同样的问题,我通过创建 activity 解决了这个问题,并在 onCreate 方法上添加了:
//StartStopActivity
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
在我的服务中,我这样启动 activity:
//StartStopService
Intent dialogIntent = new Intent(this, StartStopActivity.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialogIntent);
旧方法不适用于 Marshmallow,已弃用。
在 Android 6.0 项目中,我正在尝试使用接近传感器解锁 phone。
这是我对 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() 时出现问题。它成功解锁并唤醒了 phone,但是当我按下主页按钮时 phone 冻结了。我知道我需要在某个时候重新启用键盘锁,但是在哪里?解决方案是什么,或者是否有任何其他方法可以通过编程方式解锁 phone?
如果有人遇到同样的问题,我通过创建 activity 解决了这个问题,并在 onCreate 方法上添加了:
//StartStopActivity
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
在我的服务中,我这样启动 activity:
//StartStopService
Intent dialogIntent = new Intent(this, StartStopActivity.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialogIntent);
旧方法不适用于 Marshmallow,已弃用。