android – 使用浓缩咖啡解锁仿真器屏幕

前端之家收集整理的这篇文章主要介绍了android – 使用浓缩咖啡解锁仿真器屏幕前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发我的第一个 Android应用程序,我正在设置CI服务器.我的espresso咖啡测试在我的机器上正常运行,但travis错误出现以下情况

java.lang.RuntimeException: Waited for the root of the view hierarchy to have window focus and not be requesting layout for over 10 seconds.

在运行测试之前,我似乎需要解锁仿真器屏幕.为此,我必须使用所需权限向src / debug添加清单,然后使用以下命令解锁屏幕:

KeyguardManager mKeyGuardManager = (KeyguardManager) ctx.getSystemService(Context.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock mLock = mKeyGuardManager.newKeyguardLock(name);
mLock.disableKeyguard();

问题是我不想乱丢我的活动,上面的代码包含在if块中.有没有办法从浓缩咖啡测试中解锁屏幕?

我的浓咖啡测试:

@RunWith(AndroidJUnit4.class)
public class EspressoSetupTest {

    @Rule
    public final ActivityTestRule<WelcomeActivity> activity =
            new ActivityTestRule<>(WelcomeActivity.class,true,true);

    @Test
    public void launchTest() {
        onView(withId(R.id.welcome_textView_hello))
                .perform(click())
                .check(matches(withText("RetroLambda is working")));
    }
}

解决方法

您可以在Espresso Test中使用setUp()方法,如:
@UiThreadTest
@Before
public void setUp() throws Exception {
   final Activity activity = mActivityRule.getActivity();
    mActivityRule.runOnUiThread(new Runnable() {
        @Override
        public void run() {
          KeyguardManager mKG = (KeyguardManager) activity.getSystemService(Context.KEYGUARD_SERVICE);
          KeyguardManager.KeyguardLock mLock = mKG.newKeyguardLock(KEYGUARD_SERVICE);
          mLock.disableKeyguard();

        //turn the screen on
         activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                    | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                    | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                    | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                    | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
          }
      });
}

SRC /调试/ AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" >
    <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
</manifest>

猜你在找的Android相关文章