Android 如何在仪器内部注册广播接收器?

Android 如何在仪器内部注册广播接收器?,android,broadcastreceiver,android-bluetooth,android-uiautomator,android-instrumentation,Android,Broadcastreceiver,Android Bluetooth,Android Uiautomator,Android Instrumentation,我正试图通过一个apk获得蓝牙发现结果,该apk作为android junit runner运行。一切都很好,但当我遇到以下错误时。原因可能是什么 java.lang.SecurityException:给定的调用程序包com.ex.test未在进程ProcessRecord{d740580 19462:com.ex/u0a302}中运行 代码- startDiscovery工作正常,但在ctx.registerReceiver(数据接收器,过滤器)处,应用程序正在引发异常 仪器指令- adb

我正试图通过一个apk获得蓝牙发现结果,该apk作为android junit runner运行。一切都很好,但当我遇到以下错误时。原因可能是什么

java.lang.SecurityException:给定的调用程序包com.ex.test未在进程ProcessRecord{d740580 19462:com.ex/u0a302}中运行

代码-

startDiscovery工作正常,但在
ctx.registerReceiver(数据接收器,过滤器)处,应用程序正在引发异常

仪器指令-

adb shell am instrument-w-r-e debug false-e class com.ex.main#demo com.ex/android.support.test.runner.AndroidJUnitRunner


我自己找到了答案。使用
InstrumentationRegistry.getTargetContext()
解决了我的问题


InstrumentationRegistry.getInstrumentation()
,返回当前正在运行的检测


InstrumentationRegistry.getContext()
,返回此检测程序包的上下文


InstrumentationRegistry.getTargetContext()
,返回目标应用程序的应用程序上下文

这里有一些信息-


但我仍然不确定何时使用
InstrumentationRegistry.getContext()

我自己找到了答案。使用
InstrumentationRegistry.getTargetContext()
解决了我的问题


InstrumentationRegistry.getInstrumentation()
,返回当前正在运行的检测


InstrumentationRegistry.getContext()
,返回此检测程序包的上下文


InstrumentationRegistry.getTargetContext()
,返回目标应用程序的应用程序上下文

这里有一些信息-


但我仍然不确定何时使用
InstrumentationRegistry.getContext()

InstrumentationRegistry.getTargetContext()返回被测应用程序的上下文


InstrumentationRegistry.getContext()
返回运行测试的检测的上下文

然后,如果您想要注册一个接收器,就像您描述的那样,那么您需要您的应用程序上下文。但是,这并不是真正测试应用程序是否接收广播,因为接收器不是it的一部分

无论如何,在回答您的第二个问题时,使用
InstrumentationRegistry.getContext()
的原因是当您的测试需要访问不属于应用程序但仅在测试中使用的资源或文件时

编辑 这里有一个例子。两个文件,一个在应用程序中,另一个在测试中

src/androidTest/assets/sometestfile
src/main/assets/someappfile
然后您可以根据上下文访问它们

@Test
public final void testAccessToAppAssetsFromTest() throws IOException {
    final AssetManager assetManager = mInstrumentation.getTargetContext().getAssets();
    assetManager.open("someappfile");
}

@Test
public final void testAccessToTestAssetsFromTest() throws IOException {
    final AssetManager assetManager = mInstrumentation.getContext().getAssets();
    assetManager.open("sometestfile");
}

如果尝试相反的方法,测试将失败。

InstrumentationRegistry.getTargetContext()
返回被测应用程序的上下文


InstrumentationRegistry.getContext()
返回运行测试的检测的上下文

然后,如果您想要注册一个接收器,就像您描述的那样,那么您需要您的应用程序上下文。但是,这并不是真正测试应用程序是否接收广播,因为接收器不是it的一部分

无论如何,在回答您的第二个问题时,使用
InstrumentationRegistry.getContext()
的原因是当您的测试需要访问不属于应用程序但仅在测试中使用的资源或文件时

编辑 这里有一个例子。两个文件,一个在应用程序中,另一个在测试中

src/androidTest/assets/sometestfile
src/main/assets/someappfile
然后您可以根据上下文访问它们

@Test
public final void testAccessToAppAssetsFromTest() throws IOException {
    final AssetManager assetManager = mInstrumentation.getTargetContext().getAssets();
    assetManager.open("someappfile");
}

@Test
public final void testAccessToTestAssetsFromTest() throws IOException {
    final AssetManager assetManager = mInstrumentation.getContext().getAssets();
    assetManager.open("sometestfile");
}

如果您尝试相反的方法,测试将失败。

谢谢!一如既往,您帮助澄清了与UIA相关的问题。即使是4年前的AndroidViewClient。在这里,我仍然没有得到
getContext
在插装中的用法。请给出一个我们使用-
InstrumentationRegistry.getContext()
的例子。即使没有getContext,我们也可以访问资源。对吧?谢谢!一如既往,您帮助澄清了与UIA相关的问题。甚至4年前的OrandroidViewClient。在这里,我仍然没有得到
getContext
在插装中的用法。请给出一个我们使用-
InstrumentationRegistry.getContext()
的例子。即使没有getContext,我们也可以访问资源。正当