監聽Android系統Log

轉載請註明出處,原文網址:http://blog.csdn.net/m_changgong/article/details/7661940作者:張燕廣

實現原理:1)執行logcat命令;

2)在service中把監聽到的log內容通過廣播發送出去;

3)Client端接收廣播,獲取log內容;

4)注意,添加讀取log的權限<uses-permission android:name="android.permission.READ_LOGS"/>

爲什麼要監聽Log?

通過分析log可以監聽系統安裝、卸載軟件等操作。

具體實現,見代碼:

監聽log的服務LogObserverService,代碼如下:

[java] view plain copy
  1. package com.isoft.log;  
  2.   
  3. import java.io.DataInputStream;  
  4. import java.io.IOException;  
  5.   
  6. import android.app.Service;  
  7. import android.content.Intent;  
  8. import android.os.Bundle;  
  9. import android.os.IBinder;  
  10. import android.util.Log;  
  11.   
  12. public class LogObserverService extends Service implements Runnable{  
  13.     private String TAG = "LogObserverService";  
  14.     private boolean isObserverLog = false;  
  15.     private StringBuffer logContent = null;  
  16.     private Bundle mBundle = null;  
  17.     private Intent mIntent = null;  
  18.     @Override  
  19.     public IBinder onBind(Intent intent) {  
  20.         return null;  
  21.     }  
  22.   
  23.     @Override  
  24.     public void onCreate() {  
  25.         super.onCreate();  
  26.         Log.i(TAG,"onCreate");  
  27.         mIntent = new Intent();  
  28.         mBundle = new Bundle();  
  29.         logContent = new StringBuffer();  
  30.         startLogObserver();  
  31.     }  
  32.   
  33.     /** 
  34.      * 開啓檢測日誌 
  35.      */  
  36.     public void startLogObserver() {  
  37.         Log.i(TAG,"startObserverLog");  
  38.         isObserverLog = true;  
  39.         Thread mTherad = new Thread(this);  
  40.         mTherad.start();  
  41.     }  
  42.   
  43.     /** 
  44.      * 關閉檢測日誌 
  45.      */  
  46.     public void stopLogObserver() {  
  47.         isObserverLog = false;  
  48.     }  
  49.   
  50.     @Override  
  51.     public void onDestroy() {  
  52.         super.onDestroy();  
  53.         stopLogObserver();  
  54.     }  
  55.   
  56.     /** 
  57.      * 發送log內容 
  58.      * @param logContent 
  59.      */  
  60.     private void sendLogContent(String logContent){  
  61.         mBundle.putString("log",logContent);  
  62.         mIntent.putExtras(mBundle);  
  63.         mIntent.setAction(LogObserverActivity.LOG_ACTION);  
  64.         sendBroadcast(mIntent);  
  65.     }  
  66.       
  67.       
  68.     @Override  
  69.     public void run() {  
  70.         Process pro = null;  
  71.         try {  
  72.             Runtime.getRuntime().exec("logcat -c").waitFor();  
  73.               
  74.             pro = Runtime.getRuntime().exec("logcat");  
  75.         } catch (InterruptedException e) {  
  76.             e.printStackTrace();  
  77.         } catch (IOException e) {  
  78.             e.printStackTrace();  
  79.         }  
  80.   
  81.         DataInputStream dis = new DataInputStream(pro.getInputStream());  
  82.         String line = null;  
  83.         while (isObserverLog) {  
  84.             try {  
  85.                 while ((line = dis.readLine()) != null) {  
  86.                     String temp = logContent.toString();  
  87.                     logContent.delete(0, logContent.length());  
  88.                     logContent.append(line);  
  89.                     logContent.append("\n");  
  90.                     logContent.append(temp);  
  91.                     //發送log內容  
  92.                     sendLogContent(logContent.toString());  
  93.                     Thread.yield();  
  94.                 }  
  95.             } catch (Exception e) {  
  96.                 e.printStackTrace();  
  97.             }  
  98.         }  
  99.     }  
  100. }  
使用log監聽服務的Client端LogObserverActivity,代碼如下:

[java] view plain copy
  1. package com.isoft.log;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.BroadcastReceiver;  
  5. import android.content.Context;  
  6. import android.content.Intent;  
  7. import android.content.IntentFilter;  
  8. import android.os.Bundle;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.widget.Button;  
  12. import android.widget.TextView;  
  13.   
  14. public class LogObserverActivity extends Activity {  
  15.     private String TAG = "LogObserverActivity";  
  16.     public static String LOG_ACTION = "com.isoft.log.LOG_ACTION";  
  17.     private TextView logContent = null;  
  18.     private Button start = null;  
  19.     private Intent logObserverIntent = null;  
  20.     private LogBroadcastReceiver mLogBroadcastReceiver = null;  
  21.     @Override  
  22.     public void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.main);  
  25.         //初始化視圖  
  26.         initView();  
  27.         //註冊log廣播接收者  
  28.         registerLogBroadcastReceiver();  
  29.     }  
  30.   
  31.     private void initView() {  
  32.         logContent = (TextView) findViewById(R.id.logContent);  
  33.         logContent.setText("show log");  
  34.         start = (Button)findViewById(R.id.start);  
  35.         start.setOnClickListener(new OnClickListener() {  
  36.             @Override  
  37.             public void onClick(View v) {  
  38.                 startLogObserverService();  
  39.                 start.setEnabled(false);  
  40.             }  
  41.         });  
  42.     }  
  43.   
  44.     private void startLogObserverService() {  
  45.         logObserverIntent = new Intent(this, LogObserverService.class);  
  46.         startService(logObserverIntent);  
  47.     }  
  48.   
  49.     /** 
  50.      * 註冊log廣播接收者 
  51.      */  
  52.     private void registerLogBroadcastReceiver(){  
  53.         mLogBroadcastReceiver = new LogBroadcastReceiver();  
  54.         IntentFilter filter = new IntentFilter();  
  55.         filter.addAction(LOG_ACTION);  
  56.         registerReceiver(mLogBroadcastReceiver, filter);  
  57.     }  
  58.       
  59.     /** 
  60.      * log 廣播接收者 
  61.      * @author zhangyg 
  62.      * 
  63.      */  
  64.     private class LogBroadcastReceiver extends BroadcastReceiver{  
  65.         private String action = null;  
  66.         private Bundle mBundle = null;  
  67.         @Override  
  68.         public void onReceive(Context context, Intent intent) {  
  69.             action = intent.getAction();  
  70.             if(LOG_ACTION.equals(action)){  
  71.                 mBundle = intent.getExtras();  
  72.                 logContent.setText(mBundle.getString("log"));  
  73.             }  
  74.         }  
  75.     }  
  76.       
  77.     @Override  
  78.     protected void onDestroy() {  
  79.         super.onDestroy();  
  80.         stopService(logObserverIntent);  
  81.         unregisterReceiver(mLogBroadcastReceiver);  
  82.     }  
  83. }  
運行效果截圖如下:

點擊下載源代碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章