利用shell命令實(shí)現(xiàn)Eeclipse對(duì)Android的遠(yuǎn)程調(diào)試

這篇文章主要講如何自己來(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ì)凡蚜,你好笨啊人断。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市朝蜘,隨后出現(xiàn)的幾起案子恶迈,更是在濱河造成了極大的恐慌,老刑警劉巖谱醇,帶你破解...
    沈念sama閱讀 217,084評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件暇仲,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡副渴,警方通過(guò)查閱死者的電腦和手機(jī)奈附,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,623評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)煮剧,“玉大人斥滤,你說(shuō)我怎么就攤上這事∶阒眩” “怎么了佑颇?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,450評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀(guān)的道長(zhǎng)菇篡。 經(jīng)常有香客問(wèn)我漩符,道長(zhǎng),這世上最難降的妖魔是什么驱还? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,322評(píng)論 1 293
  • 正文 為了忘掉前任嗜暴,我火速辦了婚禮凸克,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘闷沥。我一直安慰自己萎战,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,370評(píng)論 6 390
  • 文/花漫 我一把揭開(kāi)白布舆逃。 她就那樣靜靜地躺著蚂维,像睡著了一般。 火紅的嫁衣襯著肌膚如雪路狮。 梳的紋絲不亂的頭發(fā)上虫啥,一...
    開(kāi)封第一講書(shū)人閱讀 51,274評(píng)論 1 300
  • 那天,我揣著相機(jī)與錄音奄妨,去河邊找鬼涂籽。 笑死,一個(gè)胖子當(dāng)著我的面吹牛砸抛,可吹牛的內(nèi)容都是我干的评雌。 我是一名探鬼主播,決...
    沈念sama閱讀 40,126評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼直焙,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼景东!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起奔誓,我...
    開(kāi)封第一講書(shū)人閱讀 38,980評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤斤吐,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后厨喂,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體曲初,經(jīng)...
    沈念sama閱讀 45,414評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,599評(píng)論 3 334
  • 正文 我和宋清朗相戀三年杯聚,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片抒痒。...
    茶點(diǎn)故事閱讀 39,773評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡幌绍,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出故响,到底是詐尸還是另有隱情傀广,我是刑警寧澤,帶...
    沈念sama閱讀 35,470評(píng)論 5 344
  • 正文 年R本政府宣布彩届,位于F島的核電站伪冰,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏樟蠕。R本人自食惡果不足惜贮聂,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,080評(píng)論 3 327
  • 文/蒙蒙 一靠柑、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧吓懈,春花似錦歼冰、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,713評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至甘穿,卻和暖如春腮恩,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背温兼。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,852評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工秸滴, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人妨托。 一個(gè)月前我還...
    沈念sama閱讀 47,865評(píng)論 2 370
  • 正文 我出身青樓缸榛,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親兰伤。 傳聞我的和親對(duì)象是個(gè)殘疾皇子内颗,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,689評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • android開(kāi)發(fā)環(huán)境中,ADB是我們進(jìn)行android開(kāi)發(fā)經(jīng)常要用的調(diào)試工具敦腔,它的使用當(dāng)然是我們Android開(kāi)...
    Memebox閱讀 6,081評(píng)論 0 32
  • 昨晚均澳,看到了自己這樣的信念:我是一個(gè)有罪的該受懲罰的生命。幾乎所有的人生經(jīng)驗(yàn)都證明了符衔,我就是這個(gè)樣子找前,非常認(rèn)同。 ...
    張力平閱讀 308評(píng)論 0 0
  • 這個(gè)社會(huì)總會(huì)遇到一些令人添堵的人或事判族,但是請(qǐng)不要在意這些躺盛,它們不過(guò)是你人生的過(guò)客而已,不形帮!或許連過(guò)客還算不上槽惫,只是...
    太陽(yáng)的星1閱讀 116評(píng)論 0 0
  • 1第一部手機(jī)叫虛偽 十年前界斜,我考上了大學(xué),離開(kāi)那個(gè)土里土氣的村落合冀,去了遙遠(yuǎn)而美麗的海濱城市各薇。作為獎(jiǎng)勵(lì),也為了方便和...
    沖浪小魚(yú)兒閱讀 796評(píng)論 8 13
  • 時(shí)間是向前奔流的河流君躺,絲毫不會(huì)停歇峭判,過(guò)去未來(lái)开缎,都只是不斷前進(jìn),即便會(huì)被幾塊礁石或是幾片綠洲所阻擋朝抖,仍然無(wú)非是多出幾...
    歌聲楚楚閱讀 622評(píng)論 0 3