/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); } }
/** * 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); }
/** * 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; }
这个时候,再反过头来看am的源码,现在看来这里的代码就简单多了,重点是在 onRun 和 处理参数输入的几个函数上,其他都大同小异。
原文:http://blog.csdn.net/i7788/article/details/41699715