user980188
user980188

Reputation: 11

Device Policy Administrator unable to retrieve Device Policy

I'm trying to get a Device Policy Manager to work, but i get stuck when i try to enable it. Googled a lot and already getting agressive about that error:

10-05 10:39:07.147: WARN/DeviceAdminAdd(144): Unable to retrieve device policy ComponentInfo{test.devadmin/test.devadmin.DeviceAdmin$MyDeviceAdminReceiver}
   10-05 10:39:07.147: WARN/DeviceAdminAdd(144): android.content.pm.PackageManager$NameNotFoundException: ComponentInfo{test.devadmin/test.devadmin.DeviceAdmin$MyDeviceAdminReceiver}

Manifest:

<application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".DeviceAdmin" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>     
<activity android:name=".app.DeviceAdmin$Controller"
  android:label="@string/activity_sample_device_admin">
     <intent-filter>
         <action android:name="android.intent.action.MAIN" />
         <category android:name="android.intent.category.SAMPLE_CODE" />
     </intent-filter>
</activity> 
<receiver android:name="MyDeviceAdminReceiver"
      android:label="@string/app_label"
      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

<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>

enabling method

    public void enable() {
    if (!mDPM.isAdminActive(mDeviceAdmin)) {
        Intent intent = new Intent(
                DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
                mDeviceAdmin);
        intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
                "Additional text explaining why this needs to be added.");
        startActivityForResult(intent, REQUEST_ENABLE);
    } else {
        mDPM.lockNow();
    }

Reciever Class is basicly the same as found here: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/DeviceAdminSample.html

http://rootfs.wordpress.com/2010/09/09/android-make-your-application-a-device-administrator/

any help or advice would be much appreciated!

Upvotes: 1

Views: 4368

Answers (3)

Benjamin Charais
Benjamin Charais

Reputation: 1368

I have been struggling with this issue for the last day... Just came across my own issue. My issue was in my componentName I was doing my constructor such as:

val adminName = ComponentName(this, ::DeviceAdminReceiver.javaClass)

and finally noticed I was receiving a Warning about Public constructors PackageManager.NameNotFoundException... A quick shift to

val adminName = ComponentName("com.project.myApp", "com.project.myApp.DeviceAdminReceiver")

and everything worked immediately.

Upvotes: 0

18Teen
18Teen

Reputation: 51

I think if you recheck your DeviceAdmin java file the receiver name you are trying to extend is different than the one you are specifying in the android manifest.xml file. Just a wild guess as you have not shown your DeviveAdmin java file

Upvotes: 3

SudP
SudP

Reputation: 88

Check your receiver name <receiver android:name="MyDeviceAdminReceiver"

I think you forgotten to add . before the receiver name:

<receiver android:name=".MyDeviceAdminReceiver"

Upvotes: 3

Related Questions