在Linux上使用Java程序通过ADB在Android设备上安装APK [英] Installing apk on android device via ADB with Java program on Linux

查看:397
本文介绍了在Linux上使用Java程序通过ADB在Android设备上安装APK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Linux上的Java程序将Java的内置文件管理器传递到ADB的路径,以在Android设备上安装apk。执行代码时,永远不会安装使用文件管理器选择的apk。

I am trying to pass a path from Java's inbuilt file manager to ADB with java program on Linux to install apk on android device. When the code is executed the apk selected using file manager never gets installed.

这里是代码:

JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
        "APK Files", "apk");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(getParent());
if(returnVal == JFileChooser.APPROVE_OPTION) {
    System.out.println("You choose to open this file: " + chooser.getSelectedFile().getName());
    File file = new File("");
    System.out.println(file.getAbsolutePath().toString());

    try {
        Process p1 = Runtime.getRuntime().exec("adb kill-server"); //for killing old adb instance
        Process p2 = Runtime.getRuntime().exec("adb start-server");
        Process p3 = Runtime.getRuntime().exec("adb install \"" + file.getAbsolutePath() + "\"");
        p3.waitFor();
        Process p4 = Runtime.getRuntime().exec("adb kill-server");
    } catch (Exception e1) {

        System.err.println(e1);
    }

以下代码应安装apk:

The following code should install the apk:

Process p3 = Runtime.getRuntime().exec("adb install \"" + file.getAbsolutePath() + "\"");


推荐答案

我自己弄清楚了,这是代码:

I figured it out myself, and here is the code:

JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("APK Files", "apk");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(getParent());
if (returnVal == JFileChooser.APPROVE_OPTION) {
    File file = chooser.getSelectedFile();
    String filename = chooser.getSelectedFile().getName();
    try {
        String[] commands = new String[3];
        commands[0] = "adb";
        commands[1] = "install";
        commands[2] = file.getAbsolutePath();
        Process p1 = Runtime.getRuntime().exec(commands, null);
        p1.waitFor();
                            } catch (Exception e1) {
        System.err.println(e1);
    }
}

这篇关于在Linux上使用Java程序通过ADB在Android设备上安装APK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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