android解锁屏幕实例

android解锁屏幕实例

在进行自动化测试中,经常会遇到,黑屏跟需要解锁的情况,如果这种情况不处理,自动化测试无法开始执行,下面是唤醒屏幕跟解锁的方法,代码如下:

package com.hxkid.parent.utils;

import android.annotation.SuppressLint;
import android.app.Instrumentation;
import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.content.Context;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.test.InstrumentationTestCase;
import android.util.Log;

@SuppressLint("Wakelock")
@SuppressWarnings("deprecation")
public class Util {

/**
* wake up screen if needed
*
* @param owner
* @
*/
public static WakeLock wakeScreen(InstrumentationTestCase owner) {
PowerManager pm = (PowerManager) owner.getInstrumentation()
.getTargetContext().getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK
| PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP, owner.getClass()
.getSimpleName());
wakeLock.acquire();
return wakeLock;
}
/**
*unlock screen if needed
*/
public static void unlock(Instrumentation instr) {
try {
Context targetContext = instr.getTargetContext();
KeyguardManager keyGuardManager = (KeyguardManager) targetContext
.getSystemService(Context.KEYGUARD_SERVICE);
KeyguardLock mLock = keyGuardManager.newKeyguardLock("");
mLock.disableKeyguard();
} catch (Throwable e) {
Log.e("", "disableKeyguard:", e);
}
}

}

------------------------------------------------------------------------------------------------------------------------------

更多请关注:FlyTester,关注技术的测试者

QQ群:456850134

web站:www.flytester.org

微信扫描二维码关注:

android解锁屏幕实例_第1张图片


你可能感兴趣的:(android自动化)