3

这个答案表明 Android 应用程序可以dpm像这样运行:

Runtime.getRuntime().exec("dpm set-device-owner com.test.my_device_owner_app");

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

作为控制,我尝试运行垃圾命令而不是dpm. 它按预期失败。

这有用吗?是故意削弱的吗?

4

2 回答 2

1

dpm当您得到命令语法错误时,错误地以状态码 0 退出。正确的语法是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手动运行。

于 2015-06-05T20:37:33.037 回答
0

作为一些额外的信息,我能够捕获输出(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();
}


于 2021-01-11T17:10:35.323 回答