轉載請註明出處,原文網址: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,代碼如下:
- package com.isoft.log;
- import java.io.DataInputStream;
- import java.io.IOException;
- import android.app.Service;
- import android.content.Intent;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.util.Log;
- public class LogObserverService extends Service implements Runnable{
- private String TAG = "LogObserverService";
- private boolean isObserverLog = false;
- private StringBuffer logContent = null;
- private Bundle mBundle = null;
- private Intent mIntent = null;
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- @Override
- public void onCreate() {
- super.onCreate();
- Log.i(TAG,"onCreate");
- mIntent = new Intent();
- mBundle = new Bundle();
- logContent = new StringBuffer();
- startLogObserver();
- }
- /**
- * 開啓檢測日誌
- */
- public void startLogObserver() {
- Log.i(TAG,"startObserverLog");
- isObserverLog = true;
- Thread mTherad = new Thread(this);
- mTherad.start();
- }
- /**
- * 關閉檢測日誌
- */
- public void stopLogObserver() {
- isObserverLog = false;
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- stopLogObserver();
- }
- /**
- * 發送log內容
- * @param logContent
- */
- private void sendLogContent(String logContent){
- mBundle.putString("log",logContent);
- mIntent.putExtras(mBundle);
- mIntent.setAction(LogObserverActivity.LOG_ACTION);
- sendBroadcast(mIntent);
- }
- @Override
- public void run() {
- Process pro = null;
- try {
- Runtime.getRuntime().exec("logcat -c").waitFor();
- pro = Runtime.getRuntime().exec("logcat");
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- DataInputStream dis = new DataInputStream(pro.getInputStream());
- String line = null;
- while (isObserverLog) {
- try {
- while ((line = dis.readLine()) != null) {
- String temp = logContent.toString();
- logContent.delete(0, logContent.length());
- logContent.append(line);
- logContent.append("\n");
- logContent.append(temp);
- //發送log內容
- sendLogContent(logContent.toString());
- Thread.yield();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
- package com.isoft.log;
- import android.app.Activity;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- public class LogObserverActivity extends Activity {
- private String TAG = "LogObserverActivity";
- public static String LOG_ACTION = "com.isoft.log.LOG_ACTION";
- private TextView logContent = null;
- private Button start = null;
- private Intent logObserverIntent = null;
- private LogBroadcastReceiver mLogBroadcastReceiver = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //初始化視圖
- initView();
- //註冊log廣播接收者
- registerLogBroadcastReceiver();
- }
- private void initView() {
- logContent = (TextView) findViewById(R.id.logContent);
- logContent.setText("show log");
- start = (Button)findViewById(R.id.start);
- start.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- startLogObserverService();
- start.setEnabled(false);
- }
- });
- }
- private void startLogObserverService() {
- logObserverIntent = new Intent(this, LogObserverService.class);
- startService(logObserverIntent);
- }
- /**
- * 註冊log廣播接收者
- */
- private void registerLogBroadcastReceiver(){
- mLogBroadcastReceiver = new LogBroadcastReceiver();
- IntentFilter filter = new IntentFilter();
- filter.addAction(LOG_ACTION);
- registerReceiver(mLogBroadcastReceiver, filter);
- }
- /**
- * log 廣播接收者
- * @author zhangyg
- *
- */
- private class LogBroadcastReceiver extends BroadcastReceiver{
- private String action = null;
- private Bundle mBundle = null;
- @Override
- public void onReceive(Context context, Intent intent) {
- action = intent.getAction();
- if(LOG_ACTION.equals(action)){
- mBundle = intent.getExtras();
- logContent.setText(mBundle.getString("log"));
- }
- }
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- stopService(logObserverIntent);
- unregisterReceiver(mLogBroadcastReceiver);
- }
- }