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

代码:-

在按钮上单击:-

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

最佳答案

就像您一样:

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) + "'.";
}

09-05 10:58