这篇文章主要讲如何自己来做一个apk实现远程调试,也就是说我们先自己写一个apk来控制是否启用远程调试的功能,然后通过这个apk来启用远程调试,接着基于远程adb的方式来调试以后的程序。听起来真TM绕口。没关系,跟着看就行了。实现这个目标分为3步。
1.了解shell命令
2.如何在android上执行shell
3.pc端的命令
1.了解shell命令
好吧,这个逼格的东西并不需要你多么的了解,我们只需要知道几条基本的命令。
设置adb的调试端口,当端口>-1的时候,adb是wifi调试,我们默认的一般将端口设置为5555
setprop service.adb.tcp.port 5555
对应的将端口设置为-1或者更小的数值,则将调试方式变为了usb调试
setprop service.adb.tcp.port -1
关闭adb
stop adbd
打开adb
start adbd
好了有了这几个命令的基础,就可以实现usb和wifi调试方式的转换了
2.如何在android上执行shell
怎么执行,鬼才管呢。我又不是搞底层的。对于执行shell命令,自有高手早已写好的工具类,这里将源码贴上
public class ShellUtils {
public static final String COMMAND_SU = "su";
public static final String COMMAND_SH = "sh";
public static final String COMMAND_EXIT = "exit\n";
public static final String COMMAND_LINE_END = "\n";
private ShellUtils() {
throw new AssertionError();
}
/**
* check whether has root permission
*
* @return
*/
public static boolean checkRootPermission() {
return execCommand("echo root", true, false).result == 0;
}
/**
* execute shell command, default return result msg
*
* @param command
* command
* @param isRoot
* whether need to run with root
* @return
* @see ShellUtils#execCommand(String[], boolean, boolean)
*/
public static CommandResult execCommand(String command, boolean isRoot) {
return execCommand(new String[] { command }, isRoot, true);
}
/**
* execute shell commands, default return result msg
*
* @param commands
* command list
* @param isRoot
* whether need to run with root
* @return
* @see ShellUtils#execCommand(String[], boolean, boolean)
*/
public static CommandResult execCommand(List commands,
boolean isRoot) {
return execCommand(
commands == null ? null : commands.toArray(new String[] {}),
isRoot, true);
}
/**
* execute shell commands, default return result msg
*
* @param commands
* command array
* @param isRoot
* whether need to run with root
* @return
* @see ShellUtils#execCommand(String[], boolean, boolean)
*/
public static CommandResult execCommand(String[] commands, boolean isRoot) {
return execCommand(commands, isRoot, true);
}
/**
* execute shell command
*
* @param command
* command
* @param isRoot
* whether need to run with root
* @param isNeedResultMsg
* whether need result msg
* @return
* @see ShellUtils#execCommand(String[], boolean, boolean)
*/
public static CommandResult execCommand(String command, boolean isRoot,
boolean isNeedResultMsg) {
return execCommand(new String[] { command }, isRoot, isNeedResultMsg);
}
/**
* execute shell commands
*
* @param commands
* command list
* @param isRoot
* whether need to run with root
* @param isNeedResultMsg
* whether need result msg
* @return
* @see ShellUtils#execCommand(String[], boolean, boolean)
*/
public static CommandResult execCommand(List commands,
boolean isRoot, boolean isNeedResultMsg) {
return execCommand(
commands == null ? null : commands.toArray(new String[] {}),
isRoot, isNeedResultMsg);
}
/**
* execute shell commands
*
* @param commands
* command array
* @param isRoot
* whether need to run with root
* @param isNeedResultMsg
* whether need result msg
* @return
* - if isNeedResultMsg is false, {@link CommandResult#successMsg}
* is null and {@link CommandResult#errorMsg} is null.
* - if {@link CommandResult#result} is -1, there maybe some
* excepiton.
*
*/
public static CommandResult execCommand(String[] commands, boolean isRoot,
boolean isNeedResultMsg) {
int result = -1;
if (commands == null || commands.length == 0) {
return new CommandResult(result, null, null);
}
Process process = null;
BufferedReader successResult = null;
BufferedReader errorResult = null;
StringBuilder successMsg = null;
StringBuilder errorMsg = null;
DataOutputStream os = null;
try {
process = Runtime.getRuntime().exec(
isRoot ? COMMAND_SU : COMMAND_SH);
os = new DataOutputStream(process.getOutputStream());
for (String command : commands) {
if (command == null) {
continue;
}
// donnot use os.writeBytes(commmand), avoid chinese charset
// error
os.write(command.getBytes());
os.writeBytes(COMMAND_LINE_END);
os.flush();
}
os.writeBytes(COMMAND_EXIT);
os.flush();
result = process.waitFor();
// get command result
if (isNeedResultMsg) {
successMsg = new StringBuilder();
errorMsg = new StringBuilder();
successResult = new BufferedReader(new InputStreamReader(
process.getInputStream()));
errorResult = new BufferedReader(new InputStreamReader(
process.getErrorStream()));
String s;
while ((s = successResult.readLine()) != null) {
successMsg.append(s);
}
while ((s = errorResult.readLine()) != null) {
errorMsg.append(s);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (os != null) {
os.close();
}
if (successResult != null) {
successResult.close();
}
if (errorResult != null) {
errorResult.close();
}
} catch (IOException e) {
e.printStackTrace();
}
if (process != null) {
process.destroy();
}
}
return new CommandResult(result, successMsg == null ? null
: successMsg.toString(), errorMsg == null ? null
: errorMsg.toString());
}
/**
* result of command
*
* - {@link CommandResult#result} means result of command, 0 means normal,
* else means error, same to excute in linux shell
* - {@link CommandResult#successMsg} means success message of command
* result
* - {@link CommandResult#errorMsg} means error message of command result
*
*
* @author Trinea
* 2013-5-16
*/
public static class CommandResult {
/** result of command **/
public int result;
/** success message of command result **/
public String successMsg;
/** error message of command result **/
public String errorMsg;
public CommandResult(int result) {
this.result = result;
}
public CommandResult(int result, String successMsg, String errorMsg) {
this.result = result;
this.successMsg = successMsg;
this.errorMsg = errorMsg;
}
}
}
我们需要用到的方法是
public static CommandResult execCommand(String[] commands, boolean isRoot,
boolean isNeedResultMsg) {
解释下三个参数的意思
参数1:需要执行的命令数组
参数2:是否已经root过。oh天,忘了说,你的手机必须要先root才能来做这件事情,至于root的方式,太多了,什么root大师,xx大师。
参数3:是否需要返回结果,这个可有可无,如果你选择返回结果,我想多半是你想知道这些命令有没有执行成功,你只需要判断
CommandResult .result
的值是否为0,对的,linux就是这样,等于0就是成功了的意思
ok,剩下的活你应该会做了,写一个button控件,监听点击事件,在事件中调用这个方法。至于参数一怎么写,当需要打开wifi调试的时候就这样写
String[] commands = {
setprop service.adb.tcp.port 5555,
stop adbd,
start adbd
}
当需要关闭wifi调试的时候,只需要将5555改为-1就行
3.pc端的命令
好的,现在你可以将apk编译到你的手机上,并且打开wifi调试,接着在如下目录
sdk\platform-tools
你可以通过 shift+右键 的方式有个“在此处打开命令行”。然后输入
adb connect xxxx
xxxx 是你的手机ip,端口不用输,默认就是5555,手机ip你可以在设置-关于手机-手机状态 中找到
于是“噌”的一下,你的eclipse里的device窗口就显示你的破手机已经连接上了,现在你可以丢掉数据线,静静的装逼了。真是有逼格的烧连啊。
断开连接,你可以在手机上断开,也可以在pc上通过
adb disconnect xxxx
来断开,当然在手机上断开保险一点。
好的,有问题的同学可以留言,啊哈哈哈哈哈,这都不会,你好笨啊。