"Enable Device Admin" 对话框未显示

"Enable Device Admin" Dialog not showing

我正在尝试在用户单击按钮时启用设备管理。但是,设备管理器永远不会显示。调试后,似乎应用程序通过了 Activity 并没有执行任何操作。

我在 API 16 和 API 21 设备上进行了测试,均高于 API 8 级要求。

以下是我的清单的相关部分:

<!-- 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>

我的device_adminxml文件:

<?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>

我的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);
}

我的设备管理员接收者:

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.
    }
}

我正跟随 this Android tutorial 到达我现在所在的位置。

有什么想法吗?

原来在 AndroidManifest 文件中,我在接收器的元数据标记中放置了 android:name="android.app.device.admin" 而不是 device_admin