Android:以编程方式从设备管理员中删除我的应用?

时间:2012-10-28 10:17:25

标签: android components device-admin

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

代码: -

按钮单击: -

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

1 个答案:

答案 0 :(得分:17)

就像你一样:

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