关于android instrumentation的理解、使用
来源:互联网 发布:手机浏览器知乎 编辑:程序博客网 时间:2024/06/07 18:47
一般的应用不太会用到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的生命周期。
- 关于android instrumentation的理解、使用
- Android Instrumentation基础使用
- android的Instrumentation详解
- android的Instrumentation详解
- Android的Testing和Instrumentation
- Android Instrumentation
- android Instrumentation
- android Instrumentation
- android Instrumentation
- android Instrumentation
- Android 使用Instrumentation进行自动化测试
- Android Instrumentation 判断跳转后的Activity
- Android Instrumentation 判断跳转后的 Activity
- Android中Hook Instrumentation的一些思考
- Android中Hook Instrumentation 的实现
- 一个小程序:Instrumentation的使用
- 关于Android中RSA数字签名的理解及使用
- 关于Android数据库—realm的使用及理解
- HDU 2807 The Shortest Path 把矩阵抽象成点+Floyd
- 匿名内部类是否可以继承其它类?是否可以实现接口? 代码讲解
- SurfaceView 基础用法
- C++ 局部类
- Android Studio 使用---(一)Gradle使用手册1
- 关于android instrumentation的理解、使用
- 57. Insert Interval
- Android 软键盘 弹出,默认隐藏,强制隐藏,自动变大写等
- centos + mysql-python
- Chrome console控制台
- 嵌套ScrollView时解决高度不一致问题方案
- 你没看错!TCL品牌日10万台洗衣机免费送
- 得到网页中的图片链接
- sql 增加自增列