【Android代码片段之九】监听Android系统Log
2012-06-14 10:04
253 查看
转载请注明出处,原文网址:/article/1656877.html作者:张燕广
实现原理:1)执行logcat命令;
2)在service中把监听到的log内容通过广播发送出去;
3)Client端接收广播,获取log内容;
4)注意,添加读取log的权限<uses-permission android:name="android.permission.READ_LOGS"/>
为什么要监听Log?
通过分析log可以监听系统安装、卸载软件等操作。
具体实现,见代码:
监听log的服务LogObserverService,代码如下:
点击下载源代码
实现原理: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屏幕是否锁屏
- 监听Android系统Log
- android 监听SDCard安装和卸载的代码片段(测试通过)
- 【Android代码片段之八】监听Android屏幕是否锁屏
- android 监听SDCard安装和卸载的代码片段(测试通过)
- android 监听SDCard安装和卸载的代码片段(测试通过)
- android 20个常用的系统调用代码片段
- Android中监听系统网络连接打开或者关闭的实现代码
- Android 系统语言切换监听和设置实例代码
- Android 监听 Android中监听系统网络连接打开或者关闭的实现代码
- android 系统常用的代码片段
- Android常用的几个系统参数相关代码片段
- android应用开发及系统有用代码片段总结
- 【Android代码片段之八】监听Android屏幕是否锁屏
- android系统中的log定向
- Android编程15个很有用的代码片段
- Android代码片段收集
- Android利用系统广播---监听应用程序安装和卸载
- Android常用代码之普通及系统权限静默安装APK
- 获取Erlang系统信息的代码片段