转载请注明出处,原文网址: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. import java.io.DataInputStream;
  3. import java.io.IOException;
  4. import android.app.Service;
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.os.IBinder;
  8. import android.util.Log;
  9. public class LogObserverService extends Service implements Runnable{
  10. private String TAG = "LogObserverService";
  11. private boolean isObserverLog = false;
  12. private StringBuffer logContent = null;
  13. private Bundle mBundle = null;
  14. private Intent mIntent = null;
  15. @Override
  16. public IBinder onBind(Intent intent) {
  17. return null;
  18. }
  19. @Override
  20. public void onCreate() {
  21. super.onCreate();
  22. Log.i(TAG,"onCreate");
  23. mIntent = new Intent();
  24. mBundle = new Bundle();
  25. logContent = new StringBuffer();
  26. startLogObserver();
  27. }
  28. /**
  29. * 开启检测日志
  30. */
  31. public void startLogObserver() {
  32. Log.i(TAG,"startObserverLog");
  33. isObserverLog = true;
  34. Thread mTherad = new Thread(this);
  35. mTherad.start();
  36. }
  37. /**
  38. * 关闭检测日志
  39. */
  40. public void stopLogObserver() {
  41. isObserverLog = false;
  42. }
  43. @Override
  44. public void onDestroy() {
  45. super.onDestroy();
  46. stopLogObserver();
  47. }
  48. /**
  49. * 发送log内容
  50. * @param logContent
  51. */
  52. private void sendLogContent(String logContent){
  53. mBundle.putString("log",logContent);
  54. mIntent.putExtras(mBundle);
  55. mIntent.setAction(LogObserverActivity.LOG_ACTION);
  56. sendBroadcast(mIntent);
  57. }
  58. @Override
  59. public void run() {
  60. Process pro = null;
  61. try {
  62. Runtime.getRuntime().exec("logcat -c").waitFor();
  63. pro = Runtime.getRuntime().exec("logcat");
  64. } catch (InterruptedException e) {
  65. e.printStackTrace();
  66. } catch (IOException e) {
  67. e.printStackTrace();
  68. }
  69. DataInputStream dis = new DataInputStream(pro.getInputStream());
  70. String line = null;
  71. while (isObserverLog) {
  72. try {
  73. while ((line = dis.readLine()) != null) {
  74. String temp = logContent.toString();
  75. logContent.delete(0, logContent.length());
  76. logContent.append(line);
  77. logContent.append("\n");
  78. logContent.append(temp);
  79. //发送log内容
  80. sendLogContent(logContent.toString());
  81. Thread.yield();
  82. }
  83. } catch (Exception e) {
  84. e.printStackTrace();
  85. }
  86. }
  87. }
  88. }

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

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

运行效果截图如下:

点击下载源代码

监听Android系统Log相关推荐

  1. android 监听媒体库,一个蛋疼的功能,监听android系统媒体库的变动

    思考了很久,最后决定写博客,这是我入android坑两年多以来的第一篇博客,如果写的不好,往见谅. 废话不多说,直接上菜!!! 最近遇到一个非常奇葩的功能,做一个类似相册类的应用,名曰:智能相册,涉及 ...

  2. 监听Android系统截屏

    1. 原理 因为Android系统没有提供截屏的相关API,所以需要我们自己去实现.国内的Android手机都是使用定制系统的,截图方式五花八门,采用对截图按键的监听的方案并不合适.Android系统 ...

  3. android 屏幕方向监听,Android如何监听屏幕旋转

    背景 关于个人,前段时间由于业务太忙,所以一直没有来得及思考并且沉淀点东西:同时组内一个个都在业务上能有自己的思考和总结,在这样的氛围下,不由自主的驱使周末开始写点东西,希望自己除了日常忙于业务,可以 ...

  4. 开发一款抓取Android系统Log的APP(logcat, kernel, Memory, cpu)

    近期项目需要一款抓取系统log的实用工具,具体的内容包括kernel中的log, cpu中的log,  memory 中的log, 以及system中的log,在Android4.1之后 认为应用读取 ...

  5. android 监听物理返回键,Android应用开发之react-native 监听Android物理返回键

    本文将带你了解Android应用开发之react-native 监听Android物理返回键,希望本文对大家学Android有所帮助. 1. componentWillMount(){         ...

  6. android 回退函数,详解React Native监听Android回退按键与程序化退出应用

    详解React Native监听Android回退按键与程序化退出应用 发布时间:2020-09-29 09:25:52 来源:脚本之家 阅读:137 作者:lqh 详解React Native监听A ...

  7. 查看oracle监听服务器,处理Oracle 监听文件listener.log问题

    如果连接时候变得较慢 查看Oracle日志记录,可能是因为此文件太大,超过2G, 需要定期清理,(如果多用户,记得用root,可能没权限) 查看listener.log? find / -name l ...

  8. 监听android.intent.action.PHONE_STATE状态重复执行问题

    /*** 监听android.intent.action.PHONE_STATE广播时,API21以上会收到两次回调,这两次的state(idle.ringing.offhook)是一样的,使用lab ...

  9. ionic监听android息屏和后台运行的生命周期

    Hi,宝宝们,我又来了,我最近遇到一个问题,就是在ionic中监听android的息屏.应用退出在后台运行,之后重新进入应用的事件,这个写过原生的都知道,android会在不用的时间,运行不同的生命周 ...

最新文章

  1. element的多级选中_element-ui(Vue.js) 我在做二级select联动时选中值是循环的value怎么解?...
  2. JSR303(Bean Validation 1.0)
  3. 【Pygame小游戏】首月破亿下载 一款高度融合了「超休闲游戏特性」的佳作~
  4. mysql snmp agent_WebNMS SNMP Micro Agent for MySQL - MySQL Management Console
  5. Ehcache BigMemory: 摆脱GC困扰(转)
  6. cs1.5 linux服务端,Linux下架设CS1.5服务器
  7. (软件工程复习核心重点)第十章面向对象设计-第四节:设计人机交互子系统和设计任务管理子系统
  8. nyoj(简单数学)Oh, my Paper!
  9. MAPX中的数据绑定问题
  10. eclipse汉化.设置为中文 简单好操作 java初学者看过来
  11. 定性和定量大数据分析方法指南
  12. 各种 lightbox 实现
  13. swift unowned和weak的使用
  14. QQ邮箱发送验证码(springboot、redis整合)
  15. Maven读书笔记之六(仓库)
  16. SQL中 where, group by,having,order by 的重点
  17. Makfile: [ GCC编译选项 ] >CFLAGS参数 -c -o
  18. 传统语音识别介绍【四】—— 语言模型
  19. 12.1.2、Doris__基本使用、doris的基本命令、建表概念、语句、建表语法、建表方式(引擎存储规则)、导入数据的方式、支持的数据类型、rollup索引
  20. 【Python】python的加、减、乘、除、取整、取余计算

热门文章

  1. 3月份安全软件品牌报告 金山、360负面缠身
  2. YOLOv3无法调动gpu
  3. 斯皮尔曼spearman相关系
  4. Eclipse设置开发背景色护眼模式
  5. Gin+Gateway+Fabric2.4.4演示(二)初始化账本和前端写入数据到账本
  6. arcgis(18)——矢量带标注转dwg格式的cad数据
  7. 江苏省教育考试院计算机二级c报名时间,江苏省计算机二级考试时间报名和考试时间...
  8. SpectatorView For Hololens
  9. python量化交易:Joinquant_量化交易基础【六】:循环与多股票策略
  10. MySQL数据表操作-创建数据表(CREATE TABLE)