android framework層 學習筆記(二),androidframework

來源:互聯網
上載者:User

android framework層 學習筆記(二),androidframework

 /framework/cmds   部分

  

  這部分主要是命令的實現部分。 android 本身是支援一部分linux命令的,並且再次基礎上android又添加了一些他自身專屬的命令,而這些命令正在存放在/framework/cmds檔案夾下面的。

  

  先來看第一個例子: am 

  am 命令,我沒能在源碼中找到解釋am具體的作用的描述文檔,我只能根據源碼來自己形容他,這個是一個用於開啟組件的命令,包括activity 還有 service 。

  ok,我的描述結束,接下來看源碼:

  


public class Am extends BaseCommand 


先去看一下他父類的源碼:package com.android.internal.os.BaseCommand


主要有這麼幾部分


 /**     * Call to run the command.     */    public void run(String[] args) {        if (args.length < 1) {            onShowUsage(System.out);            return;        }        mArgs = args;        mNextArg = 0;        mCurArgData = null;        try {            onRun();        } catch (IllegalArgumentException e) {            onShowUsage(System.err);            System.err.println();            System.err.println("Error: " + e.getMessage());        } catch (Exception e) {            e.printStackTrace(System.err);            System.exit(1);        }    }

這是函數是執行的命令的時候調用的,  裡面的參數 String args[]  也就是你執行命令後面攜帶的參數


 /**     * Convenience to show usage information to error output.     */    public void showUsage() {        onShowUsage(System.err);    }

這個是顯示用法的,比如加什麼參數,是做什麼用的,都是通過這個函數實現的。


/**     * Convenience to show usage information to error output along     * with an error message.     */    public void showError(String message) {        onShowUsage(System.err);        System.err.println();        System.err.println(message);    }

當error的時候調用這個函數,裡面有調用展示用法的函數,這個也就解釋了為什麼在調用命令錯誤的時候,一般會給出來當前命令的用法。


 /**     * Implement the command.     */    public abstract void onRun() throws Exception;        /**     * Print help text for the command.     */    public abstract void onShowUsage(PrintStream out);

這兩個方法是抽象方法,用來讓子類繼承實現的,具體作用,根據名字也可以推斷出來了。


 /**     * Return the next option on the command line -- that is an argument that     * starts with '-'.  If the next argument is not an option, null is returned.     */    public String nextOption() {        if (mCurArgData != null) {            String prev = mArgs[mNextArg - 1];            throw new IllegalArgumentException("No argument expected after \"" + prev + "\"");        }        if (mNextArg >= mArgs.length) {            return null;        }        String arg = mArgs[mNextArg];        if (!arg.startsWith("-")) {            return null;        }        mNextArg++;        if (arg.equals("--")) {            return null;        }        if (arg.length() > 1 && arg.charAt(1) != '-') {            if (arg.length() > 2) {                mCurArgData = arg.substring(2);                return arg.substring(0, 2);            } else {                mCurArgData = null;                return arg;            }        }        mCurArgData = null;        return arg;    }    /**     * Return the next argument on the command line, whatever it is; if there are     * no arguments left, return null.     */    public String nextArg() {        if (mCurArgData != null) {            String arg = mCurArgData;            mCurArgData = null;            return arg;        } else if (mNextArg < mArgs.length) {            return mArgs[mNextArg++];        } else {            return null;        }    }        /**     * Return the next argument on the command line, whatever it is; if there are     * no arguments left, throws an IllegalArgumentException to report this to the user.     */    public String nextArgRequired() {        String arg = nextArg();        if (arg == null) {            String prev = mArgs[mNextArg - 1];            throw new IllegalArgumentException("Argument expected after \"" + prev + "\"");        }        return arg;    }

這裡的三個函數,分別是用來處理下一個參數,下一個操作,或者當需要參數的時候來調用的,因為是public 並且在本類中沒有被調用,所以我也不清楚具體的用法,等我在其他部分讀到調用這裡的源碼的時候,才能知道他的作用,所以這裡依舊放一放。



 這個時候,再反過頭來看am的源碼,現在看來這裡的代碼就簡單多了,重點是在 onRun 和 處理參數輸入的幾個函數上,其他都大同小異。


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.