11

我正在尝试向我的应用程序添加一个按钮以将其从设备管理员中删除,并且正在使用下面的代码,但我的应用程序只是崩溃了。

代码:-

按钮点击:-

{
    ComponentName devAdminReceiver = new ComponentName(this, DemoDeviceAdminReceiver.class);
    DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
    dpm.removeActiveAdmin(devAdminReceiver);
}

在上面的代码中,DemoDeviceAdminReceiver 是一个扩展 DeviceAdminReceiver 的类。

错误日志:-

10-28 15:26:09.295: E/AndroidRuntime(26101): FATAL EXCEPTION: main
10-28 15:26:09.295: E/AndroidRuntime(26101): java.lang.IllegalArgumentException: Unknown admin: ComponentInfo{com.dragonnis.intellicover/com.dragonnis.intellicover.DemoDeviceAdminReceiver}
10-28 15:26:09.295: E/AndroidRuntime(26101):    at android.os.Parcel.readException(Parcel.java:1331)
10-28 15:26:09.295: E/AndroidRuntime(26101):    at android.os.Parcel.readException(Parcel.java:1281)
10-28 15:26:09.295: E/AndroidRuntime(26101):    at android.app.admin.IDevicePolicyManager$Stub$Proxy.removeActiveAdmin(IDevicePolicyManager.java:2940)
10-28 15:26:09.295: E/AndroidRuntime(26101):    at android.app.admin.DevicePolicyManager.removeActiveAdmin(DevicePolicyManager.java:183)
4

2 回答 2

20

就像你做的那样:

DevicePolicyManager mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
mDPM.removeActiveAdmin(mDeviceAdminReceiver);

但是您需要在 AndroidManifest.xml 中将这些过滤器添加到接收器中:

    <intent-filter>
        <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
        <action android:name="android.app.action.DEVICE_ADMIN_DISABLED" />
    </intent-filter>


@Override
public CharSequence onDisableRequested(Context context, Intent intent) {
    return "Admin rights are beeing requested to be disabled for the app called: '" + context.getString(R.string.app_name) + "'.";
}
于 2012-10-28T10:39:04.620 回答
0

编写了一个启用设备管理员(DevicePolicyManager)并安装的应用程序。但是当我想卸载它时,它返回失败并显示此消息

于 2021-12-03T09:18:52.260 回答