使用Runtime.getRuntime在Android上运行Shell命令 [英] Running shell commands on Android using Runtime.getRuntime

查看:546
本文介绍了使用Runtime.getRuntime在Android上运行Shell命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用设备管理应用程序(已由制造商签名)。我正在使用它使用以下针对Android 9的adb shell命令安装其他应用程序:-

I am working on a device admin app (has been signed by the manufacturer). I am using it to install other apps using the below adb shell command for android 9 :-

cat /sdcard/Download/myfolder/newapp.apk | pm install -S 1528293

而我只是这样通过:-

String command = "cat /sdcard/Download/myfolder/newapp.apk | pm install -S 1528293"
Runtime.getRuntime().exec(command);

但我收到错误消息 cat unknown option S。

But I get the error "cat unknown option S ".

当我从adb shell运行该命令时,该命令可以很好地工作。
不知道我在做什么错,可以使用一些帮助。

The same command works perfectly fine when I run it from adb shell. Don't know what I am doing wrong and could use some help.

编辑1:-
我尝试运行如下命令: -

EDIT 1:- I tried running the command like below:-

String[] commandInstall = {
                "/system/bin/sh",
                "-c",
                "cat /sdcard/Download/myfolder/newapp.apk | pm install -S 1528293"
        };
Process process = Runtime.getRuntime().exec(commandInstall);

但是现在我得到了错误:-

But now I get the error:-

ava.lang.SecurityException: Reverse mode only supported from shell
    at com.android.server.pm.PackageInstallerSession.doWriteInternal(PackageInstallerSession.java:679)
    at com.android.server.pm.PackageInstallerSession.write(PackageInstallerSession.java:612)
    at android.content.pm.PackageInstaller$Session.write(PackageInstaller.java:852)
    at com.android.server.pm.PackageManagerShellCommand.doWriteSplit(PackageManagerShellCommand.java:2447)
    at com.android.server.pm.PackageManagerShellCommand.runInstall(PackageManagerShellCommand.java:915)
    at com.android.server.pm.PackageManagerShellCommand.onCommand(PackageManagerShellCommand.java:158)
    at android.os.ShellCommand.exec(ShellCommand.java:103)
    at com.android.server.pm.PackageManagerService.onShellCommand(PackageManagerService.java:21330)
    at android.os.Binder.shellCommand(Binder.java:634)
    at android.os.Binder.onTransact(Binder.java:532)
    at android.content.pm.IPackageManager$Stub.onTransact(IPackageManager.java:2821)
    at com.android.server.pm.PackageManagerService.onTransact(PackageManagerService.java:3856)
    at android.os.Binder.execTransact(Binder.java:731)

编辑2:-在android 9之前,我可以做以下安装应用程序:-

Edit 2:- Prior to android 9, I could just do the below for installing apps:-

Runtime.getRuntime().exec("pm install -r app.apk");


推荐答案

查看PackageInstallerSession的源代码,我发现它更改为:-

Looking at the source code of PackageInstallerSession I found that it was changed to :-

switch (Binder.getCallingUid()) {
    case android.os.Process.SHELL_UID:
    case android.os.Process.ROOT_UID:
        break;
    default:
        throw new SecurityException("Reverse mode only supported from shell");
    }


因此,即使它是系统应用,用于安装的shell命令可能不起作用。从提交消息看来,这样做是为了让PackageInstaller来完成此工作。

Source So even if it is a system app, the shell command for install might not work. From the commit messages it seems, this was done to let PackageInstaller do this job.

但是,似乎在某些时候再次将其更改为,但可能不包含在其中android 9:-

However it seems this again got changed to at some point, but probably is not included in android 9:-

    switch (Binder.getCallingUid()) {
                case android.os.Process.SHELL_UID:
                case android.os.Process.ROOT_UID:
                case android.os.Process.SYSTEM_UID:
                    break;
                default:
                    throw new SecurityException(
                            "Reverse mode only supported from shell or system");
            }

来源

因此,如果该应用是系统应用,则最好的方法是使用PackageInstaller。

So if the app is a system app , then the best way would be to use PackageInstaller.

这篇关于使用Runtime.getRuntime在Android上运行Shell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆