Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在将活动发送到后台后重新获得对其的访问权限_Android_Testing_Android Espresso_Android Instrumentation - Fatal编程技术网

Android 如何在将活动发送到后台后重新获得对其的访问权限

Android 如何在将活动发送到后台后重新获得对其的访问权限,android,testing,android-espresso,android-instrumentation,Android,Testing,Android Espresso,Android Instrumentation,使用Espresso,我尝试将活动发送到后台,使用Home(主页)按钮,然后再次将其放在前台进行检查: @EspressoTest public void test() { onSomeView().check(matches(isDisplayed())); getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_HOME); Context context = getInstrumentation().getT

使用Espresso,我尝试将活动发送到后台,使用Home(主页)按钮,然后再次将其放在前台进行检查:

@EspressoTest
public void test() {
    onSomeView().check(matches(isDisplayed()));
    getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_HOME);

    Context context = getInstrumentation().getTargetContext();
    Intent intent = new Intent(context, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);

    onSomeView().check(matches(isDisplayed()));
}
我必须使用
intent.setFlags(intent.FLAG\u ACTIVITY\u NEW\u TASK)是由一个异常建议的,但除此之外,我还进行了测试,将其作为启动程序活动启动,或者使用

将活动重新排序标记到前面,但视图不可见。即使考试通过了。

好吧,我想出来了。首先需要使用getActivity提供的活动上下文,然后我可以使用intents在后台和前台发送活动,使用HOME类别和标志\u Activity\u REORDER\u to \u FRONT:

private void goHome(Activity activity) {
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    activity.startActivity(intent);
}

private void bringToForeground(Activity activity) {
    Intent intent = new Intent(activity, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    activity.startActivity(intent);
}

@EspressoTest
public void test() {
    //do some ui stuff to ensure the activity is up and running
    goHome(getActivity());
    // eventually sleep, or implement an idling resource
    bringToForeground(getActivity());
    // do some ui tests
}

您可以使用UI Automator(您可以同时使用espresso和UI Automator)

这使我的应用程序成为背景,并将其推向前台

请考虑此响应,当它工作时,100%。 单击鼠标返回页面,返回背景


如果我想在家庭活动中也这样做呢?我有一些测试需要测试家庭活动。基本上,应用程序需要回到后台,然后再次回到前台来重现案例。你认为这可能是什么?这一概念适用于所有活动。你说的家庭活动是什么意思?我指的是发射器活动。我用意大利浓咖啡的UIAutover实现了这个解决方案。是的,现在你可以做到这一点,请考虑以上的答案作为遗产。如果您还可以提及正在使用的Testrunner ans android support测试版本,那就太好了。androidTestCompile('com.android.support.test.uiautomator:uiautomator-v18:2.1.1')和androidTestCompile('com.android.support.test:runner:1.0.0')以及新的android测试运行程序,这个答案更准确。谢谢。只需添加两次呼叫
按RecentApps()
将使应用程序离开前台并在稍短的时间内继续time@rSilva不,在带有SDK 28的Pixel one中,它给出了“App is not found”(应用程序未找到)吐司:(在尝试恢复应用程序(最后一行)之前,我必须添加
Thread.sleep(1500L)
)。上述答案和两次调用
pressRecentApps()
都适用于此小更改。不适用于9/28/P。操作系统版本不可知。
val device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
device.pressHome()
device.pressRecentApps()
val selector = UiSelector()
val recentApp = device.findObject(selector.descriptionContains("App Description"))
recentApp.click()
UiDevice device = UiDevice.getInstance(getInstrumentation());
        device.pressHome();
        device.pressRecentApps();
        device.findObject(new UiSelector().text(getTargetContext().getString(getTargetContext().getApplicationInfo().labelRes)))
                .click();