Lock device using DeviceAdminReceiver

Umayal picture Umayal · Dec 15, 2010 · Viewed 13.4k times · Source

i am try to lock the device using DeviceAdminReceiver and try to enable administration like following:

if (!mDPM.isAdminActive(mDeviceAdminSample)) {
  Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN)
  intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mDeviceAdminSample);
  intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Additional text explaining why this needs to be added.");   startActivity(intent);
 }

and getting error something like the following:

ERROR/Error(1022): java.lang.SecurityException: No active admin owned by uid 10045 for policy #3

kindly give me some code solutions and how to enable the administration permission.

Answer

Anton picture Anton · Jan 25, 2011

Judging by your error message it seems that you may have forgotten to set your device_admin_sample.xml to ask for the policy you wish to use.

For instance if in your AndroidManifest.xml you have the following receiver code for DeviceAdminSample.

<receiver android:name=".app.DeviceAdminSample"
          android:label="@string/sample_device_admin"
          android:description="@string/sample_device_admin_description"
          android:permission="android.permission.BIND_DEVICE_ADMIN">
    <meta-data android:name="android.app.device_admin"
               android:resource="@xml/device_admin_sample" />
    <intent-filter>
        <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
    </intent-filter>
</receiver>

Then make sure that you also set your device_admin_sample.xml to the following xml so that you can use each of the device admin's abilities.

<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
  <uses-policies>
    <limit-password />
    <watch-login />
    <reset-password />
    <force-lock />
    <wipe-data />
  </uses-policies>
</device-admin>