I want to turn off / lock the screen of my device programmatically.
For the moment, when I try :
DevicePolicyManager mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
mDPM.lockNow();
I have this error :
java.lang.SecurityException: No active admin owned by uid 10176 for policy #3
This is my AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="opteamit.com.belami" >
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature android:name="android.hardware.location.gps" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<application...
What is wrong ?
Well something with high necessity can't finished with two lines of code, lock off screen required device admin. you may follow this :
and AdminReceiverClass:
also we need declares the security policies used in metadata so for examples with Path android:resource="@xml/device_admin_sample" :
in our case we just need :
Now declare it in our manifist.xml :
Hope it will Help you.