我正在尝试为我的设备启用信息亭模式,但收到未知的管理错误。我很确定我成功地将我的应用程序设置为设备所有者,并且我认为这是最后一步,但这个错误给我带来了很多麻烦。
清单 - 我这里有一个接收器,应该将我的应用程序绑定为设备管理员。它还引用了 device_admin.xml ,我不完全确定我需要它;我通过 root 并将 device_owner.xml 放入 /data/system 来使我的设备成为设备所有者。这与设备管理不同吗?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:没有活动管理组件信息{com.example.setupmanager/com.example.setupmanager.AdminReceiver}
看来我是设备所有者,但 ComponentName 设备管理未从 AdminReceiver 类成功创建。
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。
Edit:
当我在连接到我的设备的终端中输入:
dpm 设置设备所有者 com.example.setupmanager/MyAdmin
我收到错误:
错误:未知管理员:ComponentInfo{com.example.setupmanager/MyAdmin}
...这就是为什么我选择 root -> move device_owner.xml to /data/system 方法来使我的应用程序成为设备所有者。我不确定无法通过终端管理我的设备是否与此问题有关。