androidandroid-sensorskeyguard

disableKeyguard call on application freezes phone


On an Android 6.0 project, I am trying to unlock the phone using the proximity sensor.

This what I have for OnSensorChanged event:

    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

            }
        }

On my ScreenReceiver I have:

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;
            }
        }
    }

The problem occurs when I call lock.disableKeyguard(). It unlocks and wakes up the phone successfully but phone freezes when I press the home button. I know I need to re-enable the keyguard at some point but where? What could be the solution or is there any other way to unlock the phone programmatically?


Solution

  • In case anyone experiences the same problem, I solved it by creating an activity and on onCreate method I added:

    //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);
    

    On my service I started the activity like this:

    //StartStopService
    Intent dialogIntent = new Intent(this, StartStopActivity.class);
                    dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(dialogIntent);
    

    Old method does not work for Marshmallow and is deprecated.