I got an error when `devicePolicyManager.lockNow();`

error:
[Right after I clicked "lock" button](https://i.stack.imgur.com/9OdAa.png) 

[Debug: I did double check my permission(Activate this device admin app\_1)](https://i.stack.imgur.com/vEtg6.png)

[Debug: I did double check my permission(Activate this device admin app\_2)](https://i.stack.imgur.com/mclgt.png)

my goal: I was trying to onClick or shake the phone to lock it.

Here is the source:

```
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.hardware.SensorManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
Button b_enable, b_lock;

static final int RESULT_ENABLE = 1;
DevicePolicyManager devicePolicyManager;
ComponentName componentName;

private SensorManager sm;
private float acelVal; // Current acceleration value and gravity.
private float acellast; // Last Acceleration value and Gravity.
private float shake;  // Acceleration value different from gravity.

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

    sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    sm.registerListener(sensorListener, sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);

   acelVal = SensorManager.GRAVITY_EARTH;
   acellast = SensorManager.GRAVITY_EARTH;
   shake = 0.00f;

   b_enable = (Button) findViewById(R.id.b_enable);
   b_lock = (Button) findViewById(R.id.b_lock);

   devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
   componentName = new ComponentName(MainActivity.this, Controller.class);

   boolean active = devicePolicyManager.isAdminActive(componentName);

   if (active){
       b_enable.setText("Disable");
       b_lock.setVisibility(View.VISIBLE);
   }
   else{
       b_enable.setText("ENABLE");
       b_lock.setVisibility(View.GONE);
   }

   b_enable.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {
           boolean active = devicePolicyManager.isAdminActive(componentName);
           if(active){
               devicePolicyManager.removeActiveAdmin(componentName);
               b_enable.setText("ENABLE");
               b_lock.setVisibility(View.GONE);
           }
           else{
               Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
               intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,componentName);
               intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "You should enable the app!");
               startActivityForResult(intent, RESULT_ENABLE);
           }

       }
   });

   b_lock.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {
          // Toast toast = Toast.makeText(getApplicationContext(), "b_lock: onClick!!!!", Toast.LENGTH_LONG);
          // toast.show();

           boolean active = devicePolicyManager.isAdminActive(componentName);

           if(active){
               Toast toast = Toast.makeText(getApplicationContext(), "b_lock: onClick got permission!!!", Toast.LENGTH_LONG);
               toast.show();
           }else{
               Toast toast = Toast.makeText(getApplicationContext(), "b_lock: onClick did not have permission!!!", Toast.LENGTH_LONG);
               toast.show();
           }

           devicePolicyManager.lockNow();
       }
   });

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch(requestCode){
        case RESULT_ENABLE:
        if(resultCode == Activity.RESULT_OK){
            b_enable.setText("DISABLE");
            b_lock.setVisibility(View.VISIBLE);
        } else{
            Toast.makeText(this, "Failed", Toast.LENGTH_SHORT).show();
        }
        return;
    }
    super.onActivityResult(requestCode, resultCode, data);
}

private final SensorEventListener sensorListener = new SensorEventListener() {
    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {

        float x = sensorEvent.values[0];
        float y = sensorEvent.values[1];
        float z = sensorEvent.values[2];

        acellast = acelVal;
        acelVal= (float) Math.sqrt((double) (x*x + y*y + z*z));
        float delta = acelVal - acellast;
        shake = shake * 0.9f + delta;

        if (shake > 12) {
            // DO something
            //example:
            Toast toast = Toast.makeText(getApplicationContext(), "DO NOT SHAKE ME, PLEASE!!", Toast.LENGTH_LONG);
            toast.show();
            devicePolicyManager.lockNow();
        }
   }

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

    }
};
}`

```

update 1: 
@jozef Dochan

Here is the Logcat

```
01-16 00:47:45.910 15079-15079/com.example.eric.shake_lock_unlock I/zygote: Not late-enabling -Xcheck:jni (already on)
01-16 00:47:45.930 15079-15079/com.example.eric.shake_lock_unlock W/zygote: Unexpected CPU variant for X86 using defaults: x86
01-16 00:47:45.982 15079-15086/com.example.eric.shake_lock_unlock E/zygote: Failed writing handshake bytes (-1 of 14): Broken pipe
01-16 00:47:45.982 15079-15086/com.example.eric.shake_lock_unlock I/zygote: Debugger is no longer active
01-16 00:47:46.086 15079-15079/com.example.eric.shake_lock_unlock I/InstantRun: starting instant run server: is main process
01-16 00:47:46.337 15079-15096/com.example.eric.shake_lock_unlock D/OpenGLRenderer: HWUI GL Pipeline

                                                                                    [ 01-16 00:47:46.378 15079:15096 D/         ]
                                                                                    HostConnection::get() New Host Connection established 0x998a4a80, tid 15096
01-16 00:47:46.384 15079-15096/com.example.eric.shake_lock_unlock I/zygote: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
01-16 00:47:46.384 15079-15096/com.example.eric.shake_lock_unlock I/OpenGLRenderer: Initialized EGL, version 1.4
01-16 00:47:46.384 15079-15096/com.example.eric.shake_lock_unlock D/OpenGLRenderer: Swap behavior 1
01-16 00:47:46.389 15079-15096/com.example.eric.shake_lock_unlock W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
01-16 00:47:46.390 15079-15096/com.example.eric.shake_lock_unlock D/OpenGLRenderer: Swap behavior 0
01-16 00:47:46.402 15079-15096/com.example.eric.shake_lock_unlock D/EGL_emulation: eglCreateContext: 0xa310c240: maj 3 min 0 rcv 3
01-16 00:47:46.427 15079-15096/com.example.eric.shake_lock_unlock D/EGL_emulation: eglMakeCurrent: 0xa310c240: ver 3 0 (tinfo 0xa311b490)
01-16 00:47:46.430 15079-15096/com.example.eric.shake_lock_unlock E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
01-16 00:47:46.431 15079-15096/com.example.eric.shake_lock_unlock E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
01-16 00:47:46.432 15079-15096/com.example.eric.shake_lock_unlock E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
01-16 00:47:46.432 15079-15096/com.example.eric.shake_lock_unlock E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
01-16 00:47:46.481 15079-15096/com.example.eric.shake_lock_unlock D/EGL_emulation: eglMakeCurrent: 0xa310c240: ver 3 0 (tinfo 0xa311b490)
01-16 00:47:50.292 15079-15096/com.example.eric.shake_lock_unlock D/EGL_emulation: eglMakeCurrent: 0xa310c240: ver 3 0 (tinfo 0xa311b490)
01-16 00:47:50.315 15079-15096/com.example.eric.shake_lock_unlock D/EGL_emulation: eglMakeCurrent: 0xa310c240: ver 3 0 (tinfo 0xa311b490)
01-16 00:48:04.151 15079-15079/com.example.eric.shake_lock_unlock I/Choreographer: Skipped 31 frames!  The application may be doing too much work on its main thread.
01-16 00:48:05.050 15079-15096/com.example.eric.shake_lock_unlock D/EGL_emulation: eglMakeCurrent: 0xa310c240: ver 3 0 (tinfo 0xa311b490)
01-16 00:48:05.061 15079-15096/com.example.eric.shake_lock_unlock D/OpenGLRenderer: endAllActiveAnimators on 0x98a57c80 (RippleDrawable) with handle 0xa31479c0
01-16 00:48:11.973 15079-15096/com.example.eric.shake_lock_unlock D/EGL_emulation: eglMakeCurrent: 0xa310c240: ver 3 0 (tinfo 0xa311b490)
01-16 00:48:12.161 15079-15096/com.example.eric.shake_lock_unlock D/EGL_emulation: eglMakeCurrent: 0xa310c240: ver 3 0 (tinfo 0xa311b490)
01-16 00:48:19.348 15079-15096/com.example.eric.shake_lock_unlock D/EGL_emulation: eglMakeCurrent: 0xa310c240: ver 3 0 (tinfo 0xa311b490)
01-16 00:48:19.353 15079-15096/com.example.eric.shake_lock_unlock D/EGL_emulation: eglMakeCurrent: 0xa310c240: ver 3 0 (tinfo 0xa311b490)
01-16 00:48:20.420 15079-15084/com.example.eric.shake_lock_unlock I/zygote: Do partial code cache collection, code=26KB, data=30KB
01-16 00:48:20.421 15079-15084/com.example.eric.shake_lock_unlock I/zygote: After code cache collection, code=26KB, data=30KB
01-16 00:48:20.421 15079-15084/com.example.eric.shake_lock_unlock I/zygote: Increasing code cache capacity to 128KB
01-16 00:48:20.948 15079-15096/com.example.eric.shake_lock_unlock D/EGL_emulation: eglMakeCurrent: 0xa310c240: ver 3 0 (tinfo 0xa311b490)
01-16 00:48:20.954 15079-15096/com.example.eric.shake_lock_unlock D/OpenGLRenderer: endAllActiveAnimators on 0x98a57c80 (RippleDrawable) with handle 0xa3147670
01-16 00:48:22.143 15079-15096/com.example.eric.shake_lock_unlock D/EGL_emulation: eglMakeCurrent: 0xa310c240: ver 3 0 (tinfo 0xa311b490)
01-16 00:48:22.217 15079-15096/com.example.eric.shake_lock_unlock D/EGL_emulation: eglMakeCurrent: 0xa310c240: ver 3 0 (tinfo 0xa311b490)
01-16 00:48:24.781 15079-15079/com.example.eric.shake_lock_unlock D/AndroidRuntime: Shutting down VM
01-16 00:48:24.783 15079-15079/com.example.eric.shake_lock_unlock E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                    Process: com.example.eric.shake_lock_unlock, PID: 15079
                                                                                    java.lang.SecurityException: No active admin owned by uid 10080 for policy #3
                                                                                        at android.os.Parcel.readException(Parcel.java:2004)
                                                                                        at android.os.Parcel.readException(Parcel.java:1950)
                                                                                        at android.app.admin.IDevicePolicyManager$Stub$Proxy.lockNow(IDevicePolicyManager.java:4508)
                                                                                        at android.app.admin.DevicePolicyManager.lockNow(DevicePolicyManager.java:3113)
                                                                                        at android.app.admin.DevicePolicyManager.lockNow(DevicePolicyManager.java:3085)
                                                                                        at com.example.eric.shake_lock_unlock.MainActivity$2.onClick(MainActivity.java:100)
                                                                                        at android.view.View.performClick(View.java:6294)
                                                                                        at android.view.View$PerformClick.run(View.java:24770)
                                                                                        at android.os.Handler.handleCallback(Handler.java:790)
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                        at android.os.Looper.loop(Looper.java:164)
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:6494)
                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

```

update 2:

Controller.java:

```
package com.example.eric.shake_lock_unlock;

import android.app.admin.DeviceAdminReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class Controller extends DeviceAdminReceiver{

    public void onEnabled(Context context, Intent intent){
        Toast.makeText(context, "Enabled!", Toast.LENGTH_SHORT).show();
    }

    public void onDisabled(Context context, Intent intent){
        Toast.makeText(context, "Disabled!", Toast.LENGTH_SHORT).show();
    }
}

```

AndroidManifest.xml:

```
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.eric.shake_lock_unlock">

    <uses-feature
        android:name="android.hardware.sensor.accelerometer"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".Controller"
            android:description="@string/app_description"
            android:label="@string/app_name"
            android:permission="android.permission.BIND_DEVICE_ADMIN">
            <meta-data
                android:name="android.app.device_admin"
                android:resource="@xml/device_admin" />
             <intent-filter>
                <action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
             </intent-filter>
        </receiver>
    </application>

</manifest>

```

device\_admin.xml:

```
<?xml version="1.0" encoding="utf-8"?>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">

    <user-policies>
        <force-lock/>

    </user-policies>

</device-admin>

```