I have an application that should block the device after a certain time, I found that it can be done with the lockNow()
instruction, but when I put it and execute it, I get the following error.
E / AndroidRuntime: FATAL EXCEPTION: Thread-7 Process: com.example.software.appvideos, PID: 11915 java.lang.SecurityException: Do not activate admin owned by uid 10151 for policy # 3 at android.os.Parcel.readException (Parcel.java:1704) at android.os.Parcel.readException (Parcel.java:1654) at android.app.admin.IDevicePolicyManager $ Stub $ Proxy.lockNow (IDevicePolicyManager.java:5262) at android.app.admin.DevicePolicyManager.lockNow (DevicePolicyManager.java:2533) at com.example.software.appvideos.MainActivity $ 11.run (MainActivity.java:256)
Because of what little I understood when searching, permission is needed to execute the instruction if you could help me with this little problem I would be very grateful. Annex my manifest.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.software.appvideos">
<uses-permission android:name="android.permission.USES_POLICY_FORCE_LOCK" />
<uses-permission-sdk-23 android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<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"
android:theme="@style/AppTheme.Fullscreen"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
And the code where I declare the lockNow ().
DevicePolicyManager pm = (DevicePolicyManager) this.getSystemService(Context.DEVICE_POLICY_SERVICE);
public void apagar(){
Thread t = new Thread(){
public void run() {
while(true) {
try {
Thread.sleep(10000);
pm.lockNow();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
t.start();
}
Thank you in advance.