解锁屏幕
在uiautomator2.0中有两个方法:
- isScreenOn:只能判断屏幕是亮着的还是关着的,屏幕锁屏时 isScreenOn 还是会返回 true;
- wakeUp:只能把屏幕唤醒点亮并不能解锁屏幕;
以上两个方法并不能实现解锁屏幕功能,想要实现解锁屏幕还是需要借助辅助APP,Appium和Airtest框架都是通过自带的辅助APP实现解锁屏幕功能的。
定义解锁Activity
package com.android.jarvis import android.app.Activity import android.app.KeyguardManager import android.os.Bundle import androidx.core.view.accessibility.AccessibilityEventCompat class IdentityActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) ensureVisibility() } private fun ensureVisibility() { val window = window window.addFlags(2097152) window.addFlags(AccessibilityEventCompat.TYPE_WINDOWS_CHANGED) unlock() val params = window.attributes params.screenBrightness = 0.2f window.attributes = params } private fun unlock() { (getSystemService("keyguard") as KeyguardManager).newKeyguardLock("InputService/Unlock") .disableKeyguard() } }
在AndroidManifest.xml中注册Activity
<activity android:name=".IdentityActivity" android:exported="true" android:finishOnTaskLaunch="true" android:stateNotNeeded="true" android:screenOrientation="portrait" android:excludeFromRecents="true" android:noHistory="true"> <intent-filter> <action android:name="com.android.jarvis.action.ACTION_IDENTIFY" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
在AndroidManifest.xml中申请权限
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
执行下面的命令即可实现解锁屏幕:
adb shell am start -a com.android.jarvis.action.ACTION_IDENTIFY