现在的位置: 首页 > 综合 > 正文

android framework层 学习笔记(二)

2018年03月22日 ⁄ 综合 ⁄ 共 2824字 ⁄ 字号 评论关闭

 /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 和 处理参数输入的几个函数上,其他都大同小异。

 ok,接下来是第二个cmd的例子


   第二个cmd的例子是 app_process  


    这个属于一个比较特别的命令,不能在shell 里面直接输入调用。因为这个只能被系统级别调用,这个也是android 开启farmework 层java代码的第一个程序。

这个程序的入口是在  init.rc 文件中,在linux 刚刚开启的时候时候就会被系统调用。 下面是这个命令的源码


   

抱歉!评论已关闭.