Android偵錯工具正確進行方式

來源:互聯網
上載者:User

    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偵錯工具就是這麼簡單(簡單個P,為了找這個東西花了我好久的時間)。

    還有:

 
  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偵錯工具。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.