/framework/cmds part
This part is mainly the implementation part of the command. Android itself is a part of the Linux command, and on the basis of Android has added some of his own unique commands, which are stored under the/framework/cmds folder.
Let's take a look at the first example: AM
Am command, I did not find in the source code to explain am specific role of the description document, I can only according to the source to describe himself, this is a command to open the component, including activity and service.
OK, my description is over, next look at the source code:
public class Am extends Basecommand
Let's go look at his father. Source: Package Com.android.internal.os.BaseCommand
There are so few points.
/** * 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); } }
This is called when the function is executing the command, and the parameter inside the String args[] is the parameter you carry after executing the command.
/** * Convenience to show usage information to error output. * /public void Showusage () { onshowusage (system.err); }
This is the display of the usage, such as the addition of parameters, what is used, is done through this function.
/** * 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); }
This function is called when error is called, and there are functions that invoke the use of the display, which explains why the command is usually used when the command is called incorrectly.
/** * Implement the command. * /public abstract void OnRun () throws Exception; /** * Print help text for the command. * /public abstract void Onshowusage (PrintStream out);
These two methods are abstract methods, used to allow subclasses to inherit the implementation, the specific role, according to the name can also be inferred.
/** * Return The next option on the command line-which is a argument that * starts with '-'. If the next argument is not a 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) {m Curargdata = 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 is * 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 is * no arguments left, throws a illegalargumentexception 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; }
Here are the three functions, which are used to deal with the next parameter, the next operation, or when the need for parameters to call, because it is public and is not called in this class, so I do not know the specific usage, and so I read in the other part of the source code here to know his role, So there's still a place to put it.
This time, and then look back to the source of AM, now it seems that the code here is much simpler, the focus is on the OnRun and processing parameter input of several functions, the others are similar.
Android Framework Layer Learning notes (ii)