【问题标题】:Android Install apk silently by busybox command-lineAndroid 通过busybox命令行静默安装apk
【发布时间】:2013-01-18 12:02:08
【问题描述】:

我想通过 BusyBox 命令在后台静默安装 .apk。我见过一些类似的问题,例如THIS,但我仍然无法正常工作...

我有:

  1. 我的 .apk 我需要安装在 /sdcard/download/app.apk 上
  2. BusyBox 已安装

代码(不工作):

String sss = Environment.getExternalStorageDirectory() + "/download/" + "app.apk";
Process install;
install = Runtime.getRuntime().exec("/system/xbin/busybox pm install " + sss); 
int success = install.waitFor();

如果我使用“install”而不是“pm install”,它会很好地复制文件。

附:上面的代码在 AsyncTask 中执行。没有错误,但也没有任何反应...... 请帮忙!

我也试过这个,但我得到退出值 139 并且没有结果:

        Process process;
        process = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(process.getOutputStream());
        os.writeBytes("pm install /mnt/sdcard/app.apk\n");
        os.flush();
        os.writeBytes("exit\n");
        os.flush();

        int i = process.waitFor();

【问题讨论】:

  • 应用无法安装其他应用。您需要以 root 权限 (su) 执行安装命令。在您链接到您的问题的问题的答案中已经提到了这一点。
  • @Robert 我只是在安装前尝试执行 su: install = Runtime.getRuntime().exec("su");它检查root权限但不安装.apk
  • 通过PC ADB工作: 1."adb shell"; 2.“pm install /mnt/sdcard/download/app.apk”。但是我不能通过执行命令以编程方式重复它。我的 Android 操作系统是 ICS。
  • 你的应用没有通过执行su获得root权限。您必须创建一个包含 su 和您的命令的命令行:su -c 'pm install ...'
  • @Robert 我明白,但我可以t write right commands. Can you help me? I tried this: process = Runtime.getRuntime().exec("su"); os.writeBytes("pm install /mnt/sdcard/app.apk\n"); But it doesnt 工作。

标签: android busybox android-install-apk


【解决方案1】:

也许这段代码会对你有所帮助

Process p = null;
try
{
    p = Runtime.getRuntime().exec("su");
    DataOutputStream outs=new DataOutputStream(p.getOutputStream());

    String cmd="pm install /mnt/sdcard/app.apk";
    outs.writeBytes(cmd+"\n");
}
catch (IOException e)
{
    e.printStackTrace();
}

【讨论】:

    【解决方案2】:

    在对许多 android 设备进行大量调查后,我意识到这段代码是正确且有效的!

    只有一台设备出现了一些问题(Samsung Galaxy Tab 2 7.0 - 4.0.3 ICS)。也许这是 ICS 的一些奇怪特性。更新固件至 4.1.2 (Jelly Bean) 后问题已解决。

    【讨论】:

    • 您能解释一下哪些代码适合您吗?如果可能,您能否发布工作代码?
    【解决方案3】:

    您可以简单地使用 adb install 命令静默安装/更新 APK。示例代码如下

    public static void InstallAPK(String filename){
        File file = new File(filename); 
        if(file.exists()){
            try {   
                String command;
                filename = StringUtil.insertEscape(filename);
                command = "adb install -r " + filename;
                Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", command });
                proc.waitFor();
            } catch (Exception e) {
            e.printStackTrace();
            }
         }
      }
    

    【讨论】:

    • 拜托,你能发布 StringUtil.insertEscape(filename) 的实现吗?
    猜你喜欢
    • 2012-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多