android framework層 學習筆記(二)
來源:程序員人生 發布時間:2014-12-22 08:59:21 閱讀次數:3373次
/framework/cmds 部份
這部份主要是命令的實現部份。 android 本身是支持1部份linux命令的,并且再次基礎上android又添加了1些他本身獨有的命令,而這些命令正在寄存在/framework/cmds文件夾下面的。
先來看第1個例子: am
am 命令,我沒能在源碼中找到解釋am具體的作用的描寫文檔,我只能根據源碼來自己形容他,這個是1個用于開啟組件的命令,包括activity 還有 service 。
ok,我的描寫結束,接下來看源碼:
public class Am extends BaseCommand
先去看1下他父類的源碼: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的時候調用這個函數,里面有調用展現用法的函數,這個也就解釋了為何在調用命令毛病的時候,1般會給出來當前命令的用法。
/**
* 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;
}
這里的3個函數,分別是用來處理下1個參數,下1個操作,或當需要參數的時候來調用的,由于是public 并且在本類中沒有被調用,所以我也不清楚具體的用法,等我在其他部份讀到調用這里的源碼的時候,才能知道他的作用,所以這里照舊放1放。
這個時候,再反過頭來看am的源碼,現在看來這里的代碼就簡單多了,重點是在 onRun 和 處理參數輸入的幾個函數上,其他都大同小異。
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈