首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Runtime.exec运行dpm (.)

用Runtime.exec运行dpm (.)
EN

Stack Overflow用户
提问于 2015-06-05 20:05:27
回答 2查看 3.8K关注 0票数 3

This answer建议安卓应用程序可以像这样运行dpm

代码语言:javascript
复制
Runtime.getRuntime().exec("dpm set-device-owner com.test.my_device_owner_app");

在我运行5.1.1的Nexus 4上,这是无声的失败。shell返回错误代码0(成功),并且没有控制台输出。尽管取得了明显的成功,但我的应用程序并没有成为设备所有者。该设备是刚从工厂重置,没有配置用户帐户。

作为一个控件,我尝试运行一个垃圾命令而不是dpm。如预期的那样失败了。

这有用吗?是故意神经紧张吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-06-05 20:37:33

当命令语法错误时,状态代码为0的dpm不正确地退出。正确的语法是dpm set-device-owner package/.ComponentName。当语法正确时,exec(...)将抛出一个SecurityException

代码语言:javascript
复制
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即可。

票数 1
EN

Stack Overflow用户

发布于 2021-01-11 17:10:35

作为一些额外的信息,我能够捕获输出(stdout和stderr)并将其记录到logcat。

代码语言:javascript
复制
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();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30674921

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档