监听Android系统Log
2016-02-26 22:06
441 查看
转载请注明出处,原文网址: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
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,代码如下:
[java] view
plain copy
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);
}
}
运行效果截图如下:
点击下载源代码
实现原理: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
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,代码如下:
[java] view
plain copy
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);
}
}
运行效果截图如下:
点击下载源代码
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories