Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Enable Device Admin" Dialog not showing

I'm trying to enable Device Admin when a user clicks a button. However, the Device Admin never displays. After debugging it, it appears that the app passes over the Activity without executing anything.

I tested on an API 16 and API 21 device, both above the API level 8 requirement.

Here are the relevant parts to my manifest:

<!-- Add Device Activity -->
        <activity
            android:name=".AddDeviceActivity"
            android:label="@string/title_activity_add_device" />

<!-- Device Admin Receiver -->
        <receiver
            android:name=".AmayalockAdminReceiver"
            android:description="@string/txt_device_admin_description"
            android:label="@string/title_device_admin"
            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 file:

<?xml version="1.0" encoding="utf-8"?>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-policies>
        <force-lock />
    </uses-policies>
</device-admin>

My Activity:

public class AddDeviceActivity extends Activity {

private static final int REQUEST_CODE_ENABLE_ADMIN = 1;

    // Interaction with the DevicePolicyManager
    DevicePolicyManager mDPM;
    ComponentName mDeviceAdmin;

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_device);

        // Prepare to work with the Device Policy Manager
        mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
        mDeviceAdmin = new ComponentName(this, AmayalockAdminReceiver.class);

        // Set OnClickListener for addDevice Button
        Button addDevice = (Button)findViewById(R.id.btnAddDevice);
        addDevice.setOnClickListener(new AddDeviceOnClickListener());
}

private class AddDeviceOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
        // Launch the Activity to have the user enable Device Admin
                Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
                intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mDeviceAdmin);
                intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "EXPLANATION");
                startActivityForResult(intent, REQUEST_CODE_ENABLE_ADMIN);
}

My Device Admin Receiver:

import android.app.admin.DeviceAdminReceiver;
import android.content.Context;
import android.content.Intent;

public class AmayalockAdminReceiver extends DeviceAdminReceiver {

    @Override
    public void onEnabled(Context context, Intent intent) {

    }

    @Override
    public CharSequence onDisableRequested(Context context, Intent intent) {
        return "";
    }

    @Override
    public void onDisabled(Context context, Intent intent) {
        // Inform parent of app being disabled.
    }
}

I was following this Android tutorial to arrive where I currently am.

Any ideas?

like image 484
Peter Avatar asked May 08 '15 18:05

Peter


1 Answers

Turns out in the AndroidManifest file, I put android:name="android.app.device.admin" instead of device_admin in the meta-data tag for the receiver.

like image 52
Peter Avatar answered Sep 27 '22 17:09

Peter