Device Administration not working

Viewed 2093

I have created a kiosk app, and I am trying to set my device as administrator. Whenever I check if I am active admin it return back false

private void provisionOwner() {
    DevicePolicyManager manager =
            (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
    ComponentName componentName = BasicDeviceAdminReceiver.getComponentName(this);

    if(!manager.isAdminActive(componentName)) {
        Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
        startActivityForResult(intent, 0);
        return;
    }

    if (manager.isDeviceOwnerApp(getPackageName()))
        manager.setLockTaskPackages(componentName, new String [] {getPackageName()});
}

I followed the instructions of doing command line script before checking if device is admin and I am receiving an error.

adb shell dpm set-device-owner com.testapp/.DeviceAdminReceiver

The error I get back is

enter image description here

For AndroidManifest.xml I add permission for Bluetooth

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

For AndroidManifest.xml I register DeviceAdminReceiver

    <receiver
        android:name="<my package>.DeviceAdminReceiver"
        android:label="@string/app_name"
        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>

My device_admin.xml is the following:

<?xml version="1.0" encoding="utf-8"?>
<device-admin>
    <uses-policies>
        <wipe-data/>
        <force-lock/>
        <disable-camera/>
        <disable-keyguard-features/>
    </uses-policies>
</device-admin>

Is it necessary, for setting up Kiosk mode that I make my device admin? And if so, why am I receiving this error?

2 Answers

you simply use : startActivity(new Intent().setComponent(new ComponentName("com.android.settings", "com.android.settings.DeviceAdminSettings")));

Related