Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.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 当手机处于锁定/休眠状态时,ActivityInstrumentationTestCase2中的onCreate()未被调用_Android_Testing_Android Activity_Android Emulator - Fatal编程技术网

Android 当手机处于锁定/休眠状态时,ActivityInstrumentationTestCase2中的onCreate()未被调用

Android 当手机处于锁定/休眠状态时,ActivityInstrumentationTestCase2中的onCreate()未被调用,android,testing,android-activity,android-emulator,Android,Testing,Android Activity,Android Emulator,我正在使用ActivityInstrumentationTestCase2编写一个测试 当模拟器打开并且活动可以进入前台时,我的测试就可以完美地工作 当我在手机锁定的情况下启动测试时,不会创建活动。 这似乎“有道理”,因为如果某个活动无法进入前台(手机已锁定!),则没有理由加载该活动。然而,当运行测试套件时,这会导致误判 这是一个样本测试 public void testSplashscreenRedirectsSomewhereAfterTimeout() throws Exception {

我正在使用ActivityInstrumentationTestCase2编写一个测试

当模拟器打开并且活动可以进入前台时,我的测试就可以完美地工作

当我在手机锁定的情况下启动测试时,不会创建活动。 这似乎“有道理”,因为如果某个活动无法进入前台(手机已锁定!),则没有理由加载该活动。然而,当运行测试套件时,这会导致误判

这是一个样本测试

public void testSplashscreenRedirectsSomewhereAfterTimeout() throws Exception {

    Instrumentation.ActivityMonitor monitor = getInstrumentation().addMonitor(SomeActivity.class.getName(), null, false);

    getActivity(); // to make sure that the activity gets launched
    Activity activity = monitor.waitForActivityWithTimeout(10000); // waiting for the other activity
    assertNotNull("BaselineActivity was not called", activity);
}
当手机被锁定时,getActivity()返回正确的活动,但其onCreate()方法从未被调用


即使屏幕被锁定,是否有办法让测试正常工作?

假设您对活动的判断是正确的,而不是创建活动,为什么不直接用手机解锁呢。除非在手机锁定和解锁时,或者您正在测试一项服务时必须执行特定操作,否则我只需运行“准备测试环境”功能,检查屏幕是否锁定和解锁,然后运行测试

。如果没有,则解锁它并运行测试

KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE); 
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();
使现代化 使用以下命令解除钥匙防护锁

//Get the current window 
Window window = getWindow();  
//Add the Flag from the window manager class
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

假设您对未创建的活动的看法是正确的,那么为什么不使用解锁按钮解锁手机呢。除非在手机锁定和解锁时,或者您正在测试一项服务时必须执行特定操作,否则我只需运行“准备测试环境”功能,检查屏幕是否锁定和解锁,然后运行测试

。如果没有,则解锁它并运行测试

KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE); 
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();
使现代化 使用以下命令解除钥匙防护锁

//Get the current window 
Window window = getWindow();  
//Add the Flag from the window manager class
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

KeyguardManager已弃用。有其他选择吗?不推荐使用KeyguardManager。还有别的选择吗?