1

我正在尝试让设备策略管理器工作,但是当我尝试启用它时我卡住了。谷歌搜索了很多,并且已经对这个错误变得积极:

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}

显现:

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

使能方法

    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();
    }

接收器类与此处的基本相同: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/

任何帮助或建议将不胜感激!

4

3 回答 3

3

检查您的接收者姓名<receiver android:name="MyDeviceAdminReceiver"

我认为您忘记.在接收者名称之前添加:

<receiver android:name=".MyDeviceAdminReceiver"
于 2011-11-03T06:27:21.480 回答
3

我认为如果您重新检查您的 DeviceAdmin java 文件,您尝试扩展的接收器名称与您在 android manifest.xml 文件中指定的名称不同。只是一个疯狂的猜测,因为您没有显示您的 DeviveAdmin java 文件

于 2011-12-15T16:46:33.427 回答
0

最后一天我一直在努力解决这个问题......刚刚遇到了我自己的问题。我的问题在于我componentName 正在做我的构造函数,例如:

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

最后注意到我收到了关于公共构造函数 PackageManager.NameNotFoundException 的警告...

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

一切都立即奏效。

于 2019-06-21T17:09:23.243 回答