這篇文章主要講如何自己來(lái)做一個(gè)apk實(shí)現(xiàn)遠(yuǎn)程調(diào)試,也就是說(shuō)我們先自己寫(xiě)一個(gè)apk來(lái)控制是否啟用遠(yuǎn)程調(diào)試的功能追迟,然后通過(guò)這個(gè)apk來(lái)啟用遠(yuǎn)程調(diào)試溶其,接著基于遠(yuǎn)程adb的方式來(lái)調(diào)試以后的程序。聽(tīng)起來(lái)真TM繞口敦间。沒(méi)關(guān)系瓶逃,跟著看就行了束铭。實(shí)現(xiàn)這個(gè)目標(biāo)分為3步。
1.了解shell命令
2.如何在android上執(zhí)行shell
3.pc端的命令
1.了解shell命令
好吧厢绝,這個(gè)逼格的東西并不需要你多么的了解契沫,我們只需要知道幾條基本的命令。
設(shè)置adb的調(diào)試端口昔汉,當(dāng)端口>-1的時(shí)候懈万,adb是wifi調(diào)試,我們默認(rèn)的一般將端口設(shè)置為5555
setprop service.adb.tcp.port 5555
對(duì)應(yīng)的將端口設(shè)置為-1或者更小的數(shù)值靶病,則將調(diào)試方式變?yōu)榱藆sb調(diào)試
setprop service.adb.tcp.port -1
關(guān)閉adb
stop adbd
打開(kāi)adb
start adbd
好了有了這幾個(gè)命令的基礎(chǔ)会通,就可以實(shí)現(xiàn)usb和wifi調(diào)試方式的轉(zhuǎn)換了
2.如何在android上執(zhí)行shell
怎么執(zhí)行,鬼才管呢娄周。我又不是搞底層的涕侈。對(duì)于執(zhí)行shell命令,自有高手早已寫(xiě)好的工具類(lèi)煤辨,這里將源碼貼上
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<String> 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<String> 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 <ul>
* <li>if isNeedResultMsg is false, {@link CommandResult#successMsg}
* is null and {@link CommandResult#errorMsg} is null.</li>
* <li>if {@link CommandResult#result} is -1, there maybe some
* excepiton.</li>
* </ul>
*/
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
* <ul>
* <li>{@link CommandResult#result} means result of command, 0 means normal,
* else means error, same to excute in linux shell</li>
* <li>{@link CommandResult#successMsg} means success message of command
* result</li>
* <li>{@link CommandResult#errorMsg} means error message of command result</li>
* </ul>
*
* @author <a target="_blank">Trinea</a>
* 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) {
解釋下三個(gè)參數(shù)的意思
參數(shù)1:需要執(zhí)行的命令數(shù)組
參數(shù)2:是否已經(jīng)root過(guò)裳涛。oh天,忘了說(shuō)众辨,你的手機(jī)必須要先root才能來(lái)做這件事情调违,至于root的方式,太多了泻轰,什么root大師,xx大師且轨。
參數(shù)3:是否需要返回結(jié)果浮声,這個(gè)可有可無(wú),如果你選擇返回結(jié)果旋奢,我想多半是你想知道這些命令有沒(méi)有執(zhí)行成功泳挥,你只需要判斷
CommandResult .result
的值是否為0,對(duì)的至朗,linux就是這樣屉符,等于0就是成功了的意思
ok,剩下的活你應(yīng)該會(huì)做了锹引,寫(xiě)一個(gè)button控件矗钟,監(jiān)聽(tīng)點(diǎn)擊事件,在事件中調(diào)用這個(gè)方法嫌变。至于參數(shù)一怎么寫(xiě)吨艇,當(dāng)需要打開(kāi)wifi調(diào)試的時(shí)候就這樣寫(xiě)
String[] commands = {
setprop service.adb.tcp.port 5555,
stop adbd,
start adbd
}
當(dāng)需要關(guān)閉wifi調(diào)試的時(shí)候,只需要將5555改為-1就行
3.pc端的命令
好的腾啥,現(xiàn)在你可以將apk編譯到你的手機(jī)上东涡,并且打開(kāi)wifi調(diào)試冯吓,接著在如下目錄
sdk\platform-tools
你可以通過(guò) shift+右鍵 的方式有個(gè)“在此處打開(kāi)命令行”。然后輸入
adb connect xxxx
xxxx 是你的手機(jī)ip疮跑,端口不用輸组贺,默認(rèn)就是5555,手機(jī)ip你可以在設(shè)置-關(guān)于手機(jī)-手機(jī)狀態(tài) 中找到
于是“噌”的一下祖娘,你的eclipse里的device窗口就顯示你的破手機(jī)已經(jīng)連接上了失尖,現(xiàn)在你可以丟掉數(shù)據(jù)線(xiàn),靜靜的裝逼了贿条。真是有逼格的燒連啊雹仿。
斷開(kāi)連接,你可以在手機(jī)上斷開(kāi)整以,也可以在pc上通過(guò)
adb disconnect xxxx
來(lái)斷開(kāi)胧辽,當(dāng)然在手機(jī)上斷開(kāi)保險(xiǎn)一點(diǎn)。
好的公黑,有問(wèn)題的同學(xué)可以留言邑商,啊哈哈哈哈哈,這都不會(huì)凡蚜,你好笨啊人断。