我尝试在用户点击按钮时启用设备管理员。但是,设备管理员永远不会显示。调试之后,应用程序似乎会在不执行任何操作的情况下通过活动。
我在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_admin xml文件:
<?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>
我的活动:
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到达我目前的位置。
有什么想法吗?
答案 0 :(得分:3)
在AndroidManifest文件中,我将android:name="android.app.device.admin"
而不是device_admin
放在接收器的元数据标记中。