1.一般來說,專業的程序開發,在developer 階段,讓logcat 在控制檯顯示,在發佈階段,就屏蔽logcat的打印:
具體做法:
import android.util.Log;/**
* 開發階段,讓所有的log 都可見,發佈時,改變 state的值,達到屏蔽程序的logcat
*
*/
public class Logger {
public static int state = 0;
public static void v (String tag , String msg) {
if(state <=5){
Log.v(tag, msg);
}
}
public static void d (String tag , String msg) {
if(state <=4){
Log.d(tag, msg);
}
}
public static void i (String tag , String msg) {
if(state <=3){
Log.i(tag, msg);
}
}
public static void w (String tag , String msg) {
if(state <=2){
Log.w(tag, msg);
}
}
public static void e (String tag , String msg) {
if(state <=1){
Log.e(tag, msg);
}
}
}
2.如果想得到 其他程序在控制檯打印的 logcat ,如下:
2.1 首先介紹 logcat的分類:
Main ----> 打印的是 主節點 信息,即 默認狀態下 系統打印子在控制檯的 信息
Events -----》打印 系統 事件的信息 click move touch 等等
Radio ------> 打印的是電話狀態的信息,錄音等等
System-----》系統信息
main -
the main application log
events -
for system event information
radio -
for radio and phone-related informatio
system -
a log for low-level system messages and debugging
注意:the logging system does store messages in kernel buffers.
詳情(http://elinux.org/Android_Logging_Syste)
2.2 可以通過對文件的讀寫操作 將 logcat 的信息 獲取出來:
try {
Process process = Runtime.getRuntime().exec("adb -b Radio");
InputStream in = process.getInputStream();
BufferedReader read = new BufferedReader(new InputStreamReader(in));
FileOutputStream out = openFileOutput("log", Context.MODE_PRIVATE);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
String str = null;
while ((str=read.readLine()) != null){
writer.write(str);
}
} catch (Exception e) {
e.printStackTrace();
}
然後 通過 正則表到式( regexpress ) 將需要的信息給獲取到