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 和 處理參數輸入的幾個函數上,其他都大同小異。