gpt4 book ai didi

java - 在 Linux 上使用 Java 程序通过 ADB 在 android 设备上安装 apk

转载 作者:太空宇宙 更新时间:2023-11-04 09:26:29 25 4
gpt4 key购买 nike

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

代码如下:

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:

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

最佳答案

我自己弄明白了,这是代码:

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);
}
}

关于java - 在 Linux 上使用 Java 程序通过 ADB 在 android 设备上安装 apk,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35824445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com