简体   繁体   English

如何检查一个活动是否收到另一个活动发送的意图?

[英]How to check if an Activity receives an intent sent from another activity?

I send an intent from activity1 to activity2, after clicking on a button.单击按钮后,我将意图从活动 1 发送到活动 2。 I send extras with this intent.我带着这个意图发送额外的内容。

Using espresso, I need to test whether the activity2 received this intent, sent from activity1.使用espresso,我需要测试activity2是否收到了从activity1发送的这个意图。

I have no idea, how to do it.我不知道,该怎么做。 I have not written any code.我没有写任何代码。

Let's say from ActivityA you click a button which starts ActivityB:假设从 ActivityA 中单击一个启动 ActivityB 的按钮:

Intent intent = new Intent(this, ActivityB.class);
                intent.putExtra("MY_EXTRA", "MY EXTRA VALUE");

A test which checks that you're sending the right data to ActivityB would be:检查您是否向 ActivityB 发送正确数据的测试将是:

@Rule
public IntentsTestRule<ActivityA> intentsTestRule = new IntentsTestRule<>(ActivityA.class);

@Test
public void testIntents() {
    //from ActivityA, click the button which starts the ActivityB
    onView(withText("ClickMe")).perform(click());

    //validate intent and check its data
    intended(allOf(
            toPackage("com.your.package.name"),
            hasExtra("MY_EXTRA", "MY EXTRA VALUE")
    ));
}

Check out the examples provided by google: https://github.com/googlesamples/android-testing/tree/master/ui/espresso/IntentsBasicSample查看谷歌提供的示例: https : //github.com/googlesamples/android-testing/tree/master/ui/espresso/IntentsBasicSample

and some documentation here: https://developer.android.com/training/testing/espresso/intents.html .以及这里的一些文档: https : //developer.android.com/training/testing/espresso/intents.html

first use getIntent()首先使用getIntent()

Intent intent = getIntent();

then using intent object to receive data from another activity然后使用意图对象从另一个活动接收数据

String id = intent.getStringExtra("your key");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过意图发送从另一个活动发送的数据? - How to send the data which is sent from another activity by intent? 如何检查发送的预期意图而不是在Espresso中实际启动活动? - How to check expected intent sent without actually launching activity in Espresso? 如何将 MutableMap 从活动发送到另一个有意图的活动 - How to send MutableMap from activity to another with intent 图像不会使用 Intent 发送到另一个活动 - Image is not sent to another activity using Intent 如何将字符串从片段转移到另一个活动 - How to intent string from fragment to another activity 如何使用其他活动的意图在带标签的活动中打开部分? - How to open a section in a tabbed activity using an intent from another activity? 如何在Android中没有意图的情况下将数据从活动发送到另一个活动 - How to send data from activity to another activity without Intent in android 使用RecyclerView将意图从活动发送到另一个活动 - Send Intent from Activity to another Activity with RecyclerView 通过意图关闭来自其他活动的活动 - Close activity from another activity through an intent 将 Intent 从一个活动传递到另一个活动 - Pass Intent from one activity to another activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM