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

Android调试程序正确进行方式

2018年07月27日 ⁄ 综合 ⁄ 共 2064字 ⁄ 字号 评论关闭

 

转自:http://developer.51cto.com/art/201001/180491.htm

Android
是一款开源手机操作系统。大家可以在模拟器的帮助下对这一操作系统进行相应的编写,一满足自己的特定需求。Android程序下用System已经是失效了(起码我用是失效了的),那么如何实现Android调试程序呢?

第一种是用Debug,设个断点就可以跟踪,但是我觉得不爽,我用System.out用惯了,不用System.out也可以用Log的。

第二种就是我要介绍的Log,看了别人介绍的方法之后,自己亲身试验了再写上来的哦~。首先简单介绍一下Android,Android实际上应该算是一种Linux移动平台的另一个版本(我对Android研究不深,我就是这么认为的),那么既然是Linux就必定会用到命令。那么如何用命令运行程序呢?用adb命令!键入“cmd”,再键入“adb shell”,出现了个“#”号,恭喜恭喜,你可以使用命令来控制Android了。

运行“am -help”,可以查看“am”命令的帮助信息,试试运行“am start -n com.google.android.browser/com.google.android.browser.BrowserActivity”看看?呵呵,在模拟器里面可以看到运行了浏览器,哈哈,Android调试程序就是这么简单。

还有:

  1. //运行浏览器,打开中华网  
  2. # am start -a android.intent.action.VIEW -d http://www.china.com   
  3. am start -a android.intent.action.VIEW -d http://www.china.com  
  4. Starting: Intent { action=android.intent.action. VIEW data=http://www.china.com }  

  1. //拨打电话,号码是123456789  
  2. # am start -a android.intent.action.CALL -d tel:123456789   
  3. am start -a android.intent.action.CALL -d tel:123456789   
  4. Starting: Intent { action=android.intent.action.CALL  data=tel:123456789 }  
  1. # am start -a android.intent.action.ALL_APPS   
  2. am start -a android.intent.action.ALL_APPS   
  3. Starting: Intent { action=android.intent.action.ALL_APPS }  

  1. //google地图,到shanghai这个点包(注:点包为方言,就是地方的意思)  
  2. # am start -a android.intent.action.VIEW geo:0,0?q=shanghai   
  3. am start -a android.intent.action.VIEW geo:0,0?q=shanghai   
  4. Starting: Intent { action=android.intent.action. VIEW data=geo:0,0?q=shanghai } 

好了,简单的介绍了一下Android调试程序中使用命令,然后如何查看输出语句呢?在Android中可以使用Log类,Log类在android.util包中。Log 类提供了若干静态方法 :

  1. Log.v(String tag, String msg);   
  2. Log.d(String tag, String msg);   
  3. Log.i(String tag, String msg);   
  4. Log.w(String tag, String msg);   
  5. Log.e(String tag, String msg);  

分别对应 Verbose,Debug,Info,Warning,Error。

tag是一个标识,可以是任意字符串,通常可以使用类名+方法名, 主要是用来在查看日志时提供一个筛选条件.

程序运行后 并不会在 ide的控制台内输出任何信息,那么如何查看日志输出?使用"adb logcat" 命令:

  1. adb logcat  

当执行 adb logcat 后会以tail方式实时显示出所有的日志信息.

这时候我们通常需要对信息进行过滤,来显示我们需要的信息, 这时候我们指定的 tag就派上了用场.

  1. adb logcat -s MyAndroid:I 

解释:只显示tag为MyAndroid,级别为I或级别高于I(Warning,Error)的日志信息。

还有一种更好的方法,如果你的IDE用的是Eclipse的话,在show view中选择Locat就可以直接看到输出了。

好了,自己可以来实现一下Android调试程序。

抱歉!评论已关闭.