国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > 综合技术 > android framework层 学习笔记(二)

android framework层 学习笔记(二)

来源:程序员人生   发布时间:2014-12-22 08:59:21 阅读次数:3178次

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


 

生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生