访问活动开始之前 [英] Accessing Activity before it starts

查看:130
本文介绍了访问活动开始之前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在用的是InstrumentationTestCase类,以单元测试一些东西的活动中。

I am using the InstrumentationTestCase class in order to unit test some things within an activity.

我需要能够检查共享preferences的内容并进行编辑,在此之前的活动启动。

I need to be able to check the SharedPreferences's contents and edit them, before this activity is launched.

我不能使用安装方法来创建活动,并访问它的共享preferences对象进行编辑,然后再设置完成方法,因为它显然是锁定的测试处理之前关闭该活动。

I cannot use the setUp method to create the Activity and access it's SharedPreferences object to edit it, and then close that activity before finishing the setUp method because it apparently is locking the tests processing.

我也无法访问共享preferences后,我相继推出该活动的测试里面,因为一旦该活动推出后,它已经将更改共享preferences对象,并根据其采取行动之前,我有机会得到它的参考。

I also cannot access the SharedPreferences after I have launched the activity inside the test because as soon as the Activity is launched, it will already change the SharedPreferences object and act according to it, before I had the chance to get it's reference.

我显然无法访问共享preferences前两种,因为我没有任何活动的对象......而当我这样做时,已经执行code和正在启动......

I apparently cannot access the SharedPreferences before either, because I have no Activity object... and as soon as I do, it is already executing code and being launched...

所以,我的问题是,有没有什么办法我以前的活动实际上是通过一个Intent创建访问此活动的共享preferences(以及任何其他活动的信息)?

我不能,因为我的测试采用的是第二个活动在它的过程中更改为ActivityInstrumentationTestCase2,所以我不能只是改变这一类,并使用它的设定()方法来访问共享preferences。

I cannot change it to an ActivityInstrumentationTestCase2 because my test uses a second activity in it's process, so I can't just change to this class and use it's setUp() method to access the SharedPreferences.

推荐答案

我找到了最好的简单的方法只通过仪器来做到这一点,而无需修改应用程序的体系结构或任何的访问属性。

I found the best simpler way to do this through the instrumentation only, without having to edit the application's architecture or any of the access attributes.

我通过这次做到了这一点:

I achieved it through this:

Instrumentation instrumentation = getInstrumentation();
instrumentation.getTargetContext().getSharedPreferences(..);

这样我可以访问共享preferences之前的任何活动是由仪器启动。

This way I can access the SharedPreferences before any Activity is launched by the instrumentation.

感谢所有帮助,提示和其他替代品呢。

Thanks for all the help, hints and other alternatives anyway.

这篇关于访问活动开始之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆