HardBirch

【Android代码片段之九】监听Android系统Log

时间:12-06-14 栏目:iOS移动应用开发技术 作者:张飞不张,文采横飞 评论:0 点击: 1,570 次

 

实现原理: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();
			}
		}
	}
}

使用log监听服务的Client端LogObserverActivity,代码如下:

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);
	}
}

运行效果截图如下:

点击下载源代码

声明: 本文由( 张飞不张,文采横飞 )原创编译,转载请保留链接: 【Android代码片段之九】监听Android系统Log

【Android代码片段之九】监听Android系统Log:等您坐沙发呢!

发表评论


QQ群互动

Linux系统与内核学习群:194051772

WP建站技术学习交流群:194062106

魔豆之路QR

魔豆的Linux内核之路

魔豆的Linux内核之路

优秀工程师当看优秀书籍

优秀程序员,要看优秀书!

赞助商广告

友荐云推荐