Unlock screen programmatically doesn't work

I am creating a small application where when I draw my phone next to me the screen will close, and when I hold the phone away from me the phone will unlock. I used a proximity sensor for this check. I can lock the screen when I touch the sensor. But I want the phone screen lock to open when I lift my finger away from the sending phone. I used DeviceManager to block screeen, but didn't unblock it. I've tried many suggestions in SO posts, but none seem to work. The suggestion given in the link also doesn't work in my case. When I add this code to mine onSensorChanged

, the phone screen is automatically locked.

public class MainActivity extends AppCompatActivity implements SensorEventListener {

    private DevicePolicyManager mgr=null;
    private ComponentName cn=null;
    private SensorManager mSensorManager;
    private Sensor mProximity;
    private static final int SENSOR_SENSITIVITY = 4;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        cn=new ComponentName(this, AdminReceiver.class);
        mgr=(DevicePolicyManager)getSystemService(DEVICE_POLICY_SERVICE);
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mProximity = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mSensorManager.registerListener(this, mProximity, SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mSensorManager.unregisterListener(this);
    }


    public void lockMeNow(View v) {
        if (mgr.isAdminActive(cn)) {
            mgr.lockNow();
        }
        else {
            Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, cn);
            intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, getString(R.string.device_admin_explanation));
            startActivity(intent);
        }
    }


    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
            if (event.values[0] >= -SENSOR_SENSITIVITY && event.values[0] <= SENSOR_SENSITIVITY) {
                //Its near. Lock screen here                
                if (mgr.isAdminActive(cn)) {
                    mgr.lockNow();
                }else {
                    Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
                    intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, cn);
                    intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, getString(R.string.device_admin_explanation));
                    startActivity(intent);
                }
            } else {
                //Its far. Need to unlock here
                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);
            }
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
}
      

Run codeHide result


+3
android device-policy-manager


source to share


No one has answered this question yet

See similar questions:

nineteen
Turning on the screen from the receiver / service
nine
Unlock screen programmatically

or similar:

1770
Get screen dimensions in pixels
501
getting software screen density in android?
6
How to keep fingerprint unlocked after using lockNow in Android 6.0?
4
Video not showing when playing mp4 file on Android
3
Glass: how to gracefully enter the bottom light in the main menu
3
How to read heart rate from Android Wear
2
setText on a button from another kind of android activity
1
dalvikvm: Could not find class
0
java.lang.NullPointerException when calling onLoadFinished ()
0
Acceleromter service does not work on Android TV Box



All Articles
Loading...
X
Show
Funny
Dev
Pics