android解锁屏幕实例
16lz
2021-01-25
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); } }
}
在进行自动化测试中,经常会遇到,黑屏跟需要解锁的情况,如果这种情况不处理,自动化测试无法开始执行,下面是唤醒屏幕跟解锁的方法,代码如下:
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
微信扫描二维码关注:
更多相关文章
- Fragment生命周期
- Android(安卓)之Activity切换动画效果
- Android如何获取屏幕的分辨
- android TabHost简单使用
- android 让ImageView的图片全屏填充
- android 获取 USB 拔插广播消息
- Android如何获取屏幕的分辨率
- 转:android的像素单位dip,dp,px,in,mm,pt,sp
- Android(安卓)draw Rect 坐标图示