【发布时间】:2017-08-05 02:53:34
【问题描述】:
我正在尝试为我的设备启用自助服务终端模式,但我收到了未知管理员错误。我很确定我设法将我的应用设置为设备所有者,我认为这是最后一步,但这个错误给我带来了很多麻烦。
Manifest - 我在这里有一个接收器,应该将我的应用程序绑定为设备管理员。它还指的是我不完全确定我需要的 device_admin.xml;我通过将我的设备设为设备所有者并将其放入 /data/system 中的 device_owner.xml。这和设备管理员有区别吗,需要device_admin.xml吗?
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<receiver
android:name="com.example.setupmanager.AdminReceiver"
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>
...
</application>
AdminReceiver - 在当前状态下永远不会调用它。它应该,我认为,但它似乎没有被正确地创建。
public class AdminReceiver extends DeviceAdminReceiver {
@Override
public void onEnabled(Context context, Intent intent) {
Log.v(TAG, "Device Admin Enabled");
Toast.makeText(context, context.getString(R.string.device_admin_enabled), Toast.LENGTH_SHORT).show();
}
@Override
public CharSequence onDisableRequested(Context context, Intent intent) {
return context.getString(R.string.device_admin_warning);
}
@Override
public void onDisabled(Context context, Intent intent) {
Log.v(TAG, "device admin disabled");
Toast.makeText(context, context.getString(R.string.device_admin_disabled), Toast.LENGTH_SHORT).show();
}
@Override
public void onLockTaskModeEntering(Context context, Intent intent, String pkg) {
Log.v(TAG, "kiosk mode enabled");
Toast.makeText(context, context.getString(R.string.kiosk_mode_enabled), Toast.LENGTH_SHORT).show();
}
@Override
public void onLockTaskModeExiting(Context context, Intent intent) {
Log.v(TAG, "kiosk mode disabled");
Toast.makeText(context, context.getString(R.string.kiosk_mode_disabled), Toast.LENGTH_SHORT).show();
}
}
device_admin.xml - 这只是一个空白的 xml 文件。同样,我不确定我是否需要它,或者它有什么用途。
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
</device-admin>
MainActivity.java - OnCreate - 这是我尝试将它们连接在一起的地方。从我收到的日志消息来看(精简):
com.example.setupmanager/.AdminReceiver
不是设备管理员
差不多了……
D/AndroidRuntime:关闭虚拟机
E/AndroidRuntime: 致命异常: main java.lang.RuntimeException:无法启动活动
java.lang.SecurityException: 没有活动管理员 ComponentInfo{com.example.setupmanager/com.example.setupmanager.AdminReceiver}
似乎我是设备所有者,但没有从 AdminReceiver 类成功创建 ComponentName 设备管理员。
ComponentName deviceAdmin = new ComponentName(this, AdminReceiver.class);
DevicePolicyManager mDpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
if (deviceAdmin == null)
Log.v(TAG, "No device admin exists");
else
Log.v(TAG, deviceAdmin.flattenToShortString());
if (!mDpm.isAdminActive(deviceAdmin)) {
Log.v(TAG, "Not device admin");
Toast.makeText(this, getString(R.string.not_device_admin), Toast.LENGTH_SHORT).show();
}
if (mDpm.isDeviceOwnerApp(getPackageName())) {
Log.v(TAG, "Almost there...");
mDpm.setLockTaskPackages(deviceAdmin, new String[]{getPackageName()});
} else {
Log.v(TAG, "Not device owner");
Toast.makeText(this, getString(R.string.not_device_owner), Toast.LENGTH_SHORT).show();
}
我做错了什么,我应该提供更多信息/要采取的步骤吗?我的设备版本是 5.0.2。
编辑:
当我输入连接到我的设备的终端时: dpm set-device-owner com.example.setupmanager/MyAdmin
我得到错误: 错误:未知管理员:ComponentInfo{com.example.setupmanager/MyAdmin}
...这就是为什么我选择 root -> 将 device_owner.xml 移动到 /data/system 方法以使我的应用成为设备所有者。我不确定无法通过终端设置我的设备管理员是否与此问题有关。
【问题讨论】:
标签: android