关于android instrumentation的理解、使用

一般的应用不太会用到instrumentation,所以网上对其介绍也比较少。

但因其强大的跟踪application及activity生命周期的功能,用于android 应用测试框架中,被做为基类使用。

instrumentation的官方文档:http://developer.android.com/intl/zh-cn/reference/android/app/Instrumentation.html

查看其提供的方法,比如:callActivityOnCreate,callApplicationOnCreate,newActivity,callActivityOnNewIntent 等基本上在application和activity的所有生命周期调用中,都会先调用instrumentation的相应方法。并且针对应用内的所有activity都生效。为程序员提供了一个强大的能力,有更多的可能性进入android app框架执行流程。


对于每一个android app来说,它的总入口都是ActivityThread::main. 每一个应用的进程都有一个ActivityThread对象,而每一个ActivityThread对象都有一个Instrumentation mInstrumentation;成员变量。mInstrumentation的初始化在ActivityThread::handleBindApplication函数中:

if (data.instrumentationName !=null) {

...

java.lang.ClassLoadercl = instrContext.getClassLoader();

mInstrumentation = (Instrumentation)

                    cl.loadClass(data.instrumentationName.getClassName()).newInstance();

...} else {

mInstrumentation =newInstrumentation();

}

自定义的instrumentation只能通过调用Context.startInstrementation或者命令行am instrument启动,这个过程会将目标进程杀死再重启(声明instrumention的应用与目标应用签名必需一致)。如果应用不是通过上述方式启动而是正常情况下(通过launcher或者startActivity)启动,会默认new 一个系统的Instrumentation.

如果某应用要自定义instrumentation且用于跟踪本应用内的application及activity生命周期,通过继承实现Instrumentation并反射修改ActivityThread.mInstrumentation的方法可以做到。以下是具体做法:

1、自定义Instrumentation

class MyInstrumentation extends Instrumentation {

...

};

2、反射调用修改ActivityThread.mInstrumentation

MyInstrumentation ins = new MyInstrumentation();

Class cls = Class.forName("android.app.ActivityThread"); // ActivityThread被隐藏了,所以通过这种方式获得class对象

Method mthd = cls.getDeclaredMethod("currentActivityThread", (Class[]) null); // 获取当前ActivityThread对象引用

Object currentAT = mthd.invoke(null, (Object[]) null);

Field mInstrumentation = currentAT.getClass().getDeclaredField("mInstrumentation");

mInstrumentation.setAccessible(true);

mInstrumentation.set(currentAT, ins); // 修改ActivityThread.mInstrumentation值


至此即可通过MyInstrumentation来跟踪应用内application及activity的生命周期。



  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值