Android framework layer learning notes (2), androidframework

Source: Internet
Author: User

Android framework layer learning notes (2), androidframework

/Framework/cmds Section

This part is mainly the implementation part of the command. Android itself supports some linux commands, and then adds some of its own unique commands Based on android, which are stored in the/framework/cmds folder.

Let's take a look at the first example: am

Am command. I failed to find the description document explaining the specific role of am in the source code. I can only describe it by myself based on the source code. This is a command used to enable components, including activity and service.

OK, my description is complete. Next, let's look at the source code:


Public class Am extends BaseCommand


Let's take a look at the source code of his parent class: package com. android. internal. OS. BaseCommand


There are several main parts


 /**     * 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 function is called when the command is executed. The parameter String args [] in it is the parameter carried after the command is executed.


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

This is a display usage, such as what parameters are added and what is used, are implemented 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);    }

When an error occurs, this function is called, and the function that shows the usage is called. This explains why the current command is generally used when a 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 inherit and implement sub-classes. Their specific functions can be inferred based on their names.


 /**     * 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;    }

The three functions here are used to process the next parameter, the next operation, or to call the parameters when they are required, because they are public and are not called in this class, so I am not clear about the specific usage. I will only be able to know its role when I read the source code called here in other parts, so I will put it here.



At this time, let's look at the am Source Code. Now it seems that the code here is much simpler. The focus is on the onRun and several functions that process parameter input. The rest are similar.


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.