监听Android系统Log
转载请注明出处,原文网址: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();
- }
- }
- }
- }
使用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系统Log相关推荐
- android 监听媒体库,一个蛋疼的功能,监听android系统媒体库的变动
思考了很久,最后决定写博客,这是我入android坑两年多以来的第一篇博客,如果写的不好,往见谅. 废话不多说,直接上菜!!! 最近遇到一个非常奇葩的功能,做一个类似相册类的应用,名曰:智能相册,涉及 ...
- 监听Android系统截屏
1. 原理 因为Android系统没有提供截屏的相关API,所以需要我们自己去实现.国内的Android手机都是使用定制系统的,截图方式五花八门,采用对截图按键的监听的方案并不合适.Android系统 ...
- android 屏幕方向监听,Android如何监听屏幕旋转
背景 关于个人,前段时间由于业务太忙,所以一直没有来得及思考并且沉淀点东西:同时组内一个个都在业务上能有自己的思考和总结,在这样的氛围下,不由自主的驱使周末开始写点东西,希望自己除了日常忙于业务,可以 ...
- 开发一款抓取Android系统Log的APP(logcat, kernel, Memory, cpu)
近期项目需要一款抓取系统log的实用工具,具体的内容包括kernel中的log, cpu中的log, memory 中的log, 以及system中的log,在Android4.1之后 认为应用读取 ...
- android 监听物理返回键,Android应用开发之react-native 监听Android物理返回键
本文将带你了解Android应用开发之react-native 监听Android物理返回键,希望本文对大家学Android有所帮助. 1. componentWillMount(){ ...
- android 回退函数,详解React Native监听Android回退按键与程序化退出应用
详解React Native监听Android回退按键与程序化退出应用 发布时间:2020-09-29 09:25:52 来源:脚本之家 阅读:137 作者:lqh 详解React Native监听A ...
- 查看oracle监听服务器,处理Oracle 监听文件listener.log问题
如果连接时候变得较慢 查看Oracle日志记录,可能是因为此文件太大,超过2G, 需要定期清理,(如果多用户,记得用root,可能没权限) 查看listener.log? find / -name l ...
- 监听android.intent.action.PHONE_STATE状态重复执行问题
/*** 监听android.intent.action.PHONE_STATE广播时,API21以上会收到两次回调,这两次的state(idle.ringing.offhook)是一样的,使用lab ...
- ionic监听android息屏和后台运行的生命周期
Hi,宝宝们,我又来了,我最近遇到一个问题,就是在ionic中监听android的息屏.应用退出在后台运行,之后重新进入应用的事件,这个写过原生的都知道,android会在不用的时间,运行不同的生命周 ...
最新文章
- element的多级选中_element-ui(Vue.js) 我在做二级select联动时选中值是循环的value怎么解?...
- JSR303(Bean Validation 1.0)
- 【Pygame小游戏】首月破亿下载 一款高度融合了「超休闲游戏特性」的佳作~
- mysql snmp agent_WebNMS SNMP Micro Agent for MySQL - MySQL Management Console
- Ehcache BigMemory: 摆脱GC困扰(转)
- cs1.5 linux服务端,Linux下架设CS1.5服务器
- (软件工程复习核心重点)第十章面向对象设计-第四节:设计人机交互子系统和设计任务管理子系统
- nyoj(简单数学)Oh, my Paper!
- MAPX中的数据绑定问题
- eclipse汉化.设置为中文 简单好操作 java初学者看过来
- 定性和定量大数据分析方法指南
- 各种 lightbox 实现
- swift unowned和weak的使用
- QQ邮箱发送验证码(springboot、redis整合)
- Maven读书笔记之六(仓库)
- SQL中 where, group by,having,order by 的重点
- Makfile: [ GCC编译选项 ] >CFLAGS参数 -c -o
- 传统语音识别介绍【四】—— 语言模型
- 12.1.2、Doris__基本使用、doris的基本命令、建表概念、语句、建表语法、建表方式(引擎存储规则)、导入数据的方式、支持的数据类型、rollup索引
- 【Python】python的加、减、乘、除、取整、取余计算
热门文章
- 3月份安全软件品牌报告 金山、360负面缠身
- YOLOv3无法调动gpu
- 斯皮尔曼spearman相关系
- Eclipse设置开发背景色护眼模式
- Gin+Gateway+Fabric2.4.4演示(二)初始化账本和前端写入数据到账本
- arcgis(18)——矢量带标注转dwg格式的cad数据
- 江苏省教育考试院计算机二级c报名时间,江苏省计算机二级考试时间报名和考试时间...
- SpectatorView For Hololens
- python量化交易:Joinquant_量化交易基础【六】:循环与多股票策略
- MySQL数据表操作-创建数据表(CREATE TABLE)