一般的应用不太会用到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的生命周期。



更多相关文章

  1. android 中 unable to start service 错误解决方法
  2. Android之Servic的生命周期和调用方法
  3. Android中fragment A里面点击button跳转到fragment B实现方法
  4. Android Activity之间传递图片(Bitmap)的方法
  5. 【android】ORMLite框架 的使用方法---给你的数据库操作插上翅膀
  6. Android错误解决方法大集合
  7. APP横竖屏不切换,不重走生命周期

随机推荐

  1. [Android(安卓)Training视频系列]1.1 Cre
  2. Android(安卓)framework Watchdog的监控
  3. Android(安卓)UI开发专题
  4. Android(安卓)简介:Android(安卓)SDK 和开
  5. Android(安卓)基础 源码 工具
  6. Android中的人脸检测(静态和动态)
  7. android 屏幕方向切换 锁定方向
  8. Android进程与线程基本知识
  9. android 界面布局-各个布局的属性介绍
  10. Android文档阅读03—开发工具