java代码使用adb,编写java代码

用Java调用adb出现问题

①、重启

网站的建设创新互联专注网站定制,经验丰富,不做模板,主营网站定制开发.小程序定制开发,H5页面制作!给你焕然一新的设计体验!已为成都发电机租赁等企业提供专业服务。

ADB 响应失败,最先想到的就是重启工具了

不过重启一般都没有用,建议跳过该方法哈哈

②、使用 cmd 工具重启 ADB 进程

关掉所有工具

快捷键 Windows + R,输入 cmd

进入 AndroidStudio 的 sdk 工具文件夹 

以我的AndroidStudio安装在D盘为例

其中,cd 命令为打开某文件夹,这里我们需要打开 sdk 中的 platform-tools 文件夹

杀死并启动 adb 进程

其中 adb kill-server 为杀死 adb 进程 

adb start-server 为启动 adb 进程

若显示 上图中的 daemon started successfully 那么恭喜你,你的问题解决了,启动开发工具就可以正常使用了

③、关闭 adb 被占用的进程

如果使用第2种方法的时候,最终显示 failed to start deamon 那么,你就需要用到本方法了

关掉所有工具

快捷键 Windows + R,输入 cmd

输入netstat -aon|findstr “5037”

可以看到进程号为9460的进程(这个进程号因机器和时间而异)在占用5037端口(adb需要使用此端口)

打开任务管理器,选择“进程”选项卡,点击选项栏“查看-选择列…”,勾选“PID(进程标识符)”,点确定。会看到每个进程都会显示它们的PID了。找到进程号为9406的进程,结束这个进程。 

在cmd中,重新 adb start-server,可以看到成功启动

最后启动开发工具,就可以正常使用了

④、更改 Genymotion 使用的 ADB 路径

在第3种方法中,我们在任务管理器里有时是无法关闭某进程的,它会不断自动重启,比较顽固

这就需要放大招了:

打开 Genyotion 模拟器,依次选择 Settings、ADB 

选中上图中的 Use Custom Android SDK tools

点击 Browse 选择 AndroidStudio 所使用的 SDK 的文件夹

启动 Genymotion ,启动 AndroidStudio,正常使用

该解决方法对应的原因就是,Genymotion 本身有自带的 adb 工具,启动时会打开自带的 adb。

但是 Genymotion 和 AS 一起使用的时候,两者都会启动一份 adb ,所以这里我们让两者都启动 AS 的 adb ,问题就解决了!

如何在Java代码中调用adb命令

代码如下:

package com.symbio.ltp.adb;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.util.ArrayList;

import java.util.List;

import com.symbio.ltp.model.ConfigPropertiesData;

import com.symbio.ltp.util.Log;

public class ShellCommand {

private String name;

private Process process;

private BufferedWriter writer;

private BufferedReader reader;

private BufferedReader errorReader;

private ListString list;

private String[] returnValue;

public ShellCommand(String name) {

this.name = name;

}

public String getName() {

return name;

}

public Process getProcess() {

return process;

}

public BufferedWriter getOutputWriter() {

return writer;

}

public BufferedReader getInputReader() {

return reader;

}

public BufferedReader getErrorReader() {

return errorReader;

}

public boolean start(String cmd) {

try {

process = Runtime.getRuntime().exec(cmd);

writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));

reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));

} catch (IOException e) {

Log.debug("Exception in shell(" + name + ") -- " + e.getMessage());

return false;

}

return true;

}

public boolean exec(String cmd) {

String line;

try {

writer.write(cmd + "\n");

writer.flush();

while((line = reader.readLine()) != null) {

Log.debug(line);

if(line.equals(ConfigPropertiesData.ltp_success)) {

return true;

} else if(line.equals(ConfigPropertiesData.ltp_fail)) {

return false;

}

}

} catch (IOException e) {

Log.debug("Exception in shell(" + name + ") -- " + e.getMessage());

return false;

}

return true;

}

public String [] execReturn(String cmd) {

String line;

list = new ArrayListString();

try {

writer.write(cmd + "\n");

writer.flush();

line = reader.readLine();

while((line = reader.readLine()) != null) {

if(line.length()0 !(line.startsWith("#"))){

Log.debug(line);

list.add(line);

if(line.equals(ConfigPropertiesData.ltp_success)) {

break;

} else if(line.equals(ConfigPropertiesData.ltp_fail)) {

break;

}

}

}

int size = list.size();

returnValue = new String[size];

for (int i = 0; i size; i++) {

returnValue[i] = list.get(i);

}

} catch (IOException e) {

Log.debug("Exception in shell(" + name + ") -- " + e.getMessage());

return null;

}

return returnValue;

}

public void terminate() {

try {

writer.write(0x03);

writer.flush();

} catch (IOException e) {

Log.debug("Exception in shell(" + name + ") -- " + e.getMessage());

}

}

}

用Java调用adb,不出来是为什么

你需要在前面加上cmd /c

即exec中执行的命令应该是cmd /c adb shell input ...

如何在android程序中执行adb shell命令

Android中执行adb shell命令的方式如下:pre t="code" l="java" /**

* 执行一个shell命令,并返回字符串值

*

* @param cmd

* 命令名称参数组成的数组(例如:{"/system/bin/cat", "/proc/version"})

* @param workdirectory

* 命令执行路径(例如:"system/bin/")

* @return 执行结果组成的字符串

* @throws IOException

*/

public static synchronized String run(String[] cmd, String workdirectory)

throws IOException {

StringBuffer result = new StringBuffer();

try {

// 创建操作系统进程(也可以由Runtime.exec()启动)

// Runtime runtime = Runtime.getRuntime();

// Process proc = runtime.exec(cmd);

// InputStream inputstream = proc.getInputStream();

ProcessBuilder builder = new ProcessBuilder(cmd);

InputStream in = null;

// 设置一个路径(绝对路径了就不一定需要)

if (workdirectory != null) {

// 设置工作目录(同上)

builder.directory(new File(workdirectory));

// 合并标准错误和标准输出

builder.redirectErrorStream(true);

// 启动一个新进程

Process process = builder.start();

// 读取进程标准输出流

in = process.getInputStream();

byte[] re = new byte[1024];

while (in.read(re) != -1) {

result = result.append(new String(re));

}

}

// 关闭输入流

if (in != null) {

in.close();

}

} catch (Exception ex) {

ex.printStackTrace();

}

return result.toString();

} android系统底层采用的是linux,所以adb这样的linux指令是可以在java代码中调用的,可以使用ProcessBuilder 这个方法来执行对应的指令。还可以通过如下方式执行:

pre t="code" l="java"Process p = Runtime.getRuntime().exec("ls");

String data = null;

BufferedReader ie = new BufferedReader(new InputStreamReader(p.getErrorStream()));

BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));

String error = null;

while ((error = ie.readLine()) != null

!error.equals("null")) {

data += error + "\n";

}

String line = null;

while ((line = in.readLine()) != null

!line.equals("null")) {

data += line + "\n";

}

Log.v("ls", data);

java通过exec条用cmd执行adb无效

"cmd.exe /c adb devices"

java的Runtime环境已经是命令行模式,类似已经打开cmd.exe 所以,

执行后续命令无需加上 cmd.exe,命令修改为:

Runtime.getRuntime().exec("adb devices")


本文题目:java代码使用adb,编写java代码
网页链接:http://pwwzsj.com/article/hcgdih.html