爱学习受.NET

www.icjyw.com 记录开发技术收藏天地
公告信息
www.icjyw.com 记录开发技术收藏天地
文章分类
文章档案
文章
Android SDK开发中,如何在程序中输出日志
2011/8/1 23:48:57

Android SDK下, 如何在程序中输出日志 以及如何查看日志.

闲话少说,直接进入正题


在程序中输出日志, 使用 android.util.Log 类.
该类提供了若干静态方法

Log.v(String tag, String msg);
Log.d(String tag, String msg);
Log.i(String tag, String msg);
Log.w(String tag, String msg);
Log.e(String tag, String msg);

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

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


程序运行后 并不会在 ide的控制台内输出任何信息.

如果要后查看日志 请使用

adb logcat

关于adb的更多信息请查看官方网站.

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

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

adb logcat -s MyAndroid:I

这时将只显示tag为MyAndroid,级别为I或级别高于I(Warning,Error)的日志信息.

示例代码如下:

Java代码 复制代码 收藏代码
  1. package com.zijun;   
  2.   
  3. import android.app.Activity;   
  4. import android.content.Context;   
  5. import android.graphics.Canvas;   
  6. import android.os.Bundle;   
  7. import android.util.Log;   
  8. import android.view.MotionEvent;   
  9. import android.view.View;   
  10.   
  11. public class MyAndroid extends Activity {   
  12.        
  13.     protected static final String ACTIVITY_TAG="MyAndroid";   
  14.        
  15.     @Override  
  16.     protected void onCreate(Bundle icicle) {   
  17.         super.onCreate(icicle);   
  18.         setContentView(new MyView(this));   
  19.     }   
  20.     public class MyView extends View {   
  21.         public MyView(Context c) {   
  22.             super(c);   
  23.         }   
  24.         @Override  
  25.         protected void onDraw(Canvas canvas) {   
  26.     
  27.         }   
  28.         @Override  
  29.         public boolean onMotionEvent(MotionEvent event) {   
  30.             Log.i(MyAndroid.ACTIVITY_TAG, "=============================");   
  31.                
  32.             Log.d(MyAndroid.ACTIVITY_TAG, "Haha , this is a DEBUG of MyAndroid. ");   
  33.             Log.i(MyAndroid.ACTIVITY_TAG, "Haha , this is a INFO of MyAndroid. ");   
  34.             Log.w(MyAndroid.ACTIVITY_TAG, "Haha , this is a WARNING of MyAndroid. ");   
  35.   
  36.             return true;   
  37.         }   
  38.            
  39.     }   
  40.   
  41. }  
package com.zijun;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class MyAndroid extends Activity {
    
   	protected static final String ACTIVITY_TAG="MyAndroid";
   	
    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(new MyView(this));
    }
    public class MyView extends View {
        public MyView(Context c) {
            super(c);
        }
        @Override
        protected void onDraw(Canvas canvas) {
 
        }
        @Override
        public boolean onMotionEvent(MotionEvent event) {
        	Log.i(MyAndroid.ACTIVITY_TAG, "=============================");
        	
            Log.d(MyAndroid.ACTIVITY_TAG, "Haha , this is a DEBUG of MyAndroid. ");
            Log.i(MyAndroid.ACTIVITY_TAG, "Haha , this is a INFO of MyAndroid. ");
            Log.w(MyAndroid.ACTIVITY_TAG, "Haha , this is a WARNING of MyAndroid. ");

            return true;
        }
        
    }

}




以上程序运行后, pdf在命令行执行  adb logcat -s MyAndroid:I
然后在手机模拟器的屏幕上 点击 拖动鼠标 就能看到相应的日志信息.

新浪微博粉丝精灵,刷粉丝、刷评论、刷转发、企业商家微博营销必备工具"
 MyQBlog   浏览(2266)   评论(0)   关键字
  
Copyright © 2010-2020 power by CYQ.Blog - 秋色园 v2.0 All Rights Reserved