基於Android實現一段音頻和視頻播放的安卓代碼
http://www.eoeandroid.com/thread-195269-1-1.html
http://www.eoeandroid.com/thread-195269-1-1.html
Android手機測周圍環境分貝的功能源碼
http://www.eoeandroid.com/thread-194968-1-1.html
http://www.eoeandroid.com/thread-194968-1-1.html
代碼倉庫——游戲源碼匯總
http://www.eoeandroid.com/thread-195034-1-1.html
http://www.eoeandroid.com/thread-195034-1-1.html
滑動切換加載網絡圖片可放大縮小例子
http://www.eoeandroid.com/thread-194644-1-1.html
http://www.eoeandroid.com/thread-194644-1-1.html
實現uc和墨跡天氣那樣的左右拖動效果
http://www.eoeandroid.com/thread-194450-1-1.html
http://www.eoeandroid.com/thread-194450-1-1.html
相信眾多android開發者在開發程序的過程中會經常用到Log打印信息
以方便了解當前程序的運行狀況以及在出現BUG的時候能夠快速定位問題
大多數童鞋會使用官方的打印log的方法,設置TAG,然后在Eclipse里面設置過濾標簽,切換來回的看Log,但這樣卻效率很低;
下面分享一個Log打印信息的封裝類,主要提供以下功能:
1.使用一個標簽來標記當前的AP(避免設置過多的TAG來過濾顯示不同Java文件下的Log)
2.顯示當前的線程ID,用於辨別主線程還是子線程
3.顯示當前的Java文件與打印log的行號,便於快速定位到源文件
4.最后顯示你設置打印出來的信息
不羅嗦,上代碼:
public class CommonLog { private String tag = "CommonLog"; public static int logLevel = Log.VERBOSE; public static boolean isDebug = true; public CommonLog() { } public CommonLog(String tag) { this.tag = tag; } public void setTag(String tag) { this.tag = tag; } private String getFunctionName() { StackTraceElement[] sts = Thread.currentThread().getStackTrace(); if (sts == null) { return null; } for (StackTraceElement st:sts) { if (st.isNativeMethod()) { continue; } if (st.getClassName().equals(Thread.class.getName())) { continue; } if (st.getClassName().equals(this.getClass().getName())) { continue; } return "["+Thread.currentThread().getId()+": "+st.getFileName()+":"+st.getLineNumber()+"]"; } return null; } public void info(Object str) { if (logLevel <= Log.INFO) { String name = getFunctionName(); String ls=(name==null?str.toString():(name+" - "+str)); Log.i(tag, ls); } } public void i(Object str) { if (isDebug) { info(str); } } public void verbose(Object str) { if (logLevel <= Log.VERBOSE) { String name = getFunctionName(); String ls=(name==null?str.toString():(name+" - "+str)); Log.v(tag, ls); } } public void v(Object str) { if (isDebug) { verbose(str); } } public void warn(Object str) { if (logLevel <= Log.WARN) { String name = getFunctionName(); String ls=(name==null?str.toString():(name+" - "+str)); Log.w(tag, ls); } } public void w(Object str) { if (isDebug) { warn(str); } } public void error(Object str) { if (logLevel <= Log.ERROR) { String name = getFunctionName(); String ls=(name==null?str.toString():(name+" - "+str)); Log.e(tag, ls); } } public void error(Exception ex) { if (logLevel <= Log.ERROR) { StringBuffer sb = new StringBuffer(); String name = getFunctionName(); StackTraceElement[] sts = ex.getStackTrace(); if (name != null) { sb.append(name+" - "+ex+"\r\n"); } else { sb.append(ex+"\r\n"); } if (sts != null && sts.length > 0) { for (StackTraceElement st:sts) { if (st != null) { sb.append("[ "+st.getFileName()+":"+st.getLineNumber()+" ]\r\n"); } } } Log.e(tag, sb.toString()); } } public void e(Object str) { if (isDebug) { error(str); } } public void e(Exception ex) { if (isDebug) { error(ex); } } public void debug(Object str) { if (logLevel <= Log.DEBUG) { String name = getFunctionName(); String ls = (name == null?str.toString():(name+" - "+str)); Log.d(tag, ls); } } public void d(Object str) { if (isDebug) { debug(str); } } }
看ACTIVITY里的調用:
public class DebugDemoActivity extends Activity implements OnClickListener{ /** Called when the activity is first created. */ private CommonLog mCommonLog = LogFactory.createLog(); private Button mBtn1; private Button mBtn2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initView(); mCommonLog.e("onCreate..."); } @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); mCommonLog.e("onStart..."); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); mCommonLog.e("onResume..."); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); mCommonLog.e("onPause..."); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); mCommonLog.e("onStop..."); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); mCommonLog.e("onDestroy..."); } public void initView() { mBtn1 = (Button) findViewById(R.id.button1); mBtn1.setOnClickListener(this); mBtn2 = (Button) findViewById(R.id.button2); mBtn2.setOnClickListener(this); } @Override public void onClick(View view) { // TODO Auto-generated method stub switch(view.getId()) { case R.id.button1: { mCommonLog.e("R.id.button1 onClick..."); } break; case R.id.button2: { SubThread subThread = new SubThread(); subThread.start(); } break; default: break; } } }
最后看效果圖:
源碼下載:DebugDemo