Android dump的使用2

由于dumpsys命令只能dump出有限的几个service,例如SurfaceFlinger,window

那么其他类中的dump函数如何使用呢


以WindowManagerService相关的WindowState和WindowAnimator为例


PrintWriter pw = new PrintWriter(System.out, true);
WindowState win = windows.get(i);
WindowStateAnimator winAnimator = null;
win.dump(pw, "××××", true);
winAnimator = win.mWinAnimator;
winAnimator.dump(pw, "×××× ", true);

dump函数一般需要3个参数,第一个PrintWriter,通过System.out构造就可以将log输出到logcat中

第二个参数是一个前缀,随便写成什么字符串都可以,方便你阅读

第三个参数是dumpall,true/false。设置dump函数中是否输出全部信息

这个变量可以到dump函数中去修改


dump的好处是如果程序挂掉,你想看到挂掉之前的一些变量值,在不能debug的情况下,就非常方便了

其他类中的dump的使用当然就都是类似了,是不是还很简单。

但是我这坑爹的脑袋竟然搞了好久才发现^ ^


再拿ActivityManagerService举个例子

ActivityManagerService中有如下函数

@Override
    protected void dump(FileDescriptor fd, PrintWriter pw, String[] args)


那么这个函数如何使用呢

在你需要dump的代码处加入如下代码即可


String dumpFilePath = "/sdcard/dumpfile.txt";

        File dumpFile = null;
        FileOutputStream osDump = null;
        try {
            dumpFile = new File(dumpFilePath);
            osDump = new FileOutputStream(dumpFilePath);
            FileDescriptor fdout = osDump.getFD();
            PrintWriter pw = new PrintWriter(System.out, true);
            String [] args = {"-a", "a"};
            dump(fdout, pw, args);  // aaaaaaaaaaaaaaaaaaaa wang.yang
            args [1] = "p";
            dump(fdout, pw, args);
            osDump.close();
        } catch (FileNotFoundException fnfe) {
        } catch (IOException ioe) {
        } finally {
        }


  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值