This answer建议安卓应用程序可以像这样运行dpm:
Runtime.getRuntime().exec("dpm set-device-owner com.test.my_device_owner_app");在我运行5.1.1的Nexus 4上,这是无声的失败。shell返回错误代码0(成功),并且没有控制台输出。尽管取得了明显的成功,但我的应用程序并没有成为设备所有者。该设备是刚从工厂重置,没有配置用户帐户。
作为一个控件,我尝试运行一个垃圾命令而不是dpm。如预期的那样失败了。
这有用吗?是故意神经紧张吗?
发布于 2015-06-05 20:37:33
当命令语法错误时,状态代码为0的dpm不正确地退出。正确的语法是dpm set-device-owner package/.ComponentName。当语法正确时,exec(...)将抛出一个SecurityException
java.lang.SecurityException: Neither user 10086 nor current process has android.permission.MANAGE_DEVICE_ADMINS.
at android.os.Parcel.readException(Parcel.java:1546)
at android.os.Parcel.readException(Parcel.java:1499)
at android.app.admin.IDevicePolicyManager$Stub$Proxy.setActiveAdmin(IDevicePolicyManager.java:2993)
at com.android.commands.dpm.Dpm.runSetDeviceOwner(Dpm.java:110)
at com.android.commands.dpm.Dpm.onRun(Dpm.java:82)
at com.android.internal.os.BaseCommand.run(BaseCommand.java:47)
at com.android.commands.dpm.Dpm.main(Dpm.java:38)
at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:249)将此权限添加到清单中没有帮助,因此它可能是一个系统专用权限。
在没有NFC的设备上部署kiosk模式应用程序已经很麻烦了,因为您必须启用开发模式并通过adb安装应用程序。我想提供程序只需手动运行dpm即可。
发布于 2021-01-11 17:10:35
作为一些额外的信息,我能够捕获输出(stdout和stderr)并将其记录到logcat。
DevicePolicyManager dpm = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
Runtime rt = Runtime.getRuntime();
Process proc = null;
try {
proc = rt.exec("dpm set-device-owner com.myapp/.DeviceOwnerReceiver");
BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
// Read the output from the command
System.out.println("Here is the standard output of the command:\n");
String s = null;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// Read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
} catch (IOException e) {
e.printStackTrace();
}https://stackoverflow.com/questions/30674921
复制相似问题