Android logcat應用指南
作者:佚名 
  我們在這篇文章中為大家詳細介紹了有關Android logcat的基本概念以及應用方法。相信初學者們可以通過這里介紹的內容熟練的掌握這一技巧。
 Android是由谷歌推出的一款基于Linux平臺的開源手機操作系統(tǒng)。已經推出就伸手廣大編程人員的喜愛。在這里我們就先從Android logcat的相關應用來對這一系統(tǒng)進行一個深入的了解,以此方便我們的實際應用。
選項與說明
- -s 默認設置過濾器
 - - f 文件 輸出到日志文件
 - -c 清除日志
 - -d 獲取日志
 - -g 獲取日志的大小
 - - v 格式 設置日志(見下面的格式打印格式)
 
- v 格式與例范例
- brief W/tag ( 876): message
 - process W( 876) message (tag)
 - tag W/tag : message
 - thread W( 876:0x37c) message
 - raw message
 - time 09-08 05:40:26.729 W/tag ( 876): message
 - threadtime 09-08 05:40:26.729 876 892 W tag : message
 - long [ 09-08 05:40:26.729 876:0x37c W/tag ] message
 
代碼例子:
AndroidManifest.xml添加讀取權限
- < uses-permission android:name=
 
"android.permission.READ_LOGS" />- < uses-permission android:name=
 
"android.permission.READ_LOGS" />
清除日志
- try {
 - Runtime.getRuntime().exec("logcat -c");
 - } catch(Exception e) {
 - try {
 - Runtime.getRuntime().exec("logcat -c");
 - } catch(Exception e) {
 - }
 
獲取日志
- try {
 - ArrayList< String> commandLine = new ArrayList< String>();
 - commandLine.add( "logcat");
 - commandLine.add( "-d");
 - commandLine.add( "-v");
 - commandLine.add( "time");
 - commandLine.add( "-s");
 - commandLine.add( "tag:W");
 - Process process = Runtime.getRuntime().exec
 
( commandLine.toArray( new String[commandLine.size()]));- BufferedReader bufferedReader = new BufferedReader
 
( new InputStreamReader(process.getInputStream()), 1024);- String line = bufferedReader.readLine();
 - while ( line != null) {
 - log.append(line);
 - log.append("\n")
 - }
 - } catch ( IOException e) {
 - }
 - try {
 - ArrayList< String> commandLine = new ArrayList< String>();
 - commandLine.add( "logcat");
 - commandLine.add( "-d");
 - commandLine.add( "-v");
 - commandLine.add( "time");
 - commandLine.add( "-s");
 - commandLine.add( "tag:W");
 - Process process = Runtime.getRuntime().exec
 
( commandLine.toArray( new String[commandLine.size()]));- BufferedReader bufferedReader = new BufferedReader
 
( new InputStreamReader(process.getInputStream()), 1024);- String line = bufferedReader.readLine();
 - while ( line != null) {
 - log.append(line);
 - log.append("\n")
 - }
 - } catch ( IOException e) {
 - }
 
結果:
- 09-08 09:44:42.267 W/tag ( 754): message1
 - 09-08 09:44:42.709 W/tag ( 754): message2
 - 09-08 09:44:43.187 W/tag ( 754): message3
 - 09-08 09:44:45.295 E/tag ( 754): message8
 
【編輯推薦】
責任編輯:曹凱 
                    來源:
                    javaeye.com
 














 
 
 




 
 
 
 