为了安全起见,手机需要设置密码。当我的应用程序执行某些工作时,它将使用已知的密码,密码或其他方式解锁屏幕。所有电话都已扎根。
我的实现通过三个步骤引用了this post:
如果屏幕关闭以将设备从睡眠模式唤醒
adb shell input keyevent KEYCODE_POWER
向上滑动以获取PIN屏幕
adb shell input swipe 800 400 400 400
输入PIN码,然后输入以解锁
adb shell input text 0000 && adb shell input keyevent KEYCODE_ENTER
以上三个步骤已在我的程序中使用Java代码实现。
public static void lightScreenAndUnclock(Context context,String pin) {
boolean isScreenOn;
String[] cmds = {"input keyevent KEYCODE_WAKEUP",
"input keyevent KEYCODE_POWER",
"input text "+ pin};
//check screen on or off
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if (pm == null) return;
if (Build.VERSION.SDK_INT < 20) {
isScreenOn = pm.isScreenOn();
} else {
isScreenOn = pm.isInteractive();
}
//light on screen
if (!isScreenOn) {
if (Build.VERSION.SDK_INT >= 20) {
ShellUtils.execCommand(cmds[0], true);
} else ShellUtils.execCommand(cmds[1], true);
}
KeyguardManager mKeyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
//check if there is screen password
boolean flag = mKeyguardManager.isKeyguardLocked();
if (!flag) return;
//need to unlock screen password,so swipe up to obtain the pin screen
//before that, obtain screen width and height to calculate the swipe coordinates
Resources resources = context.getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
int width = dm.widthPixels;
int height = dm.heightPixels;
//the coordinates to swipe
int[] coordinates = new int[]{width / 2, height / 15 * 14, width / 2, height / 3 };
String cmdOne = "input touchscreen swipe " + coordinates[0] + " " + coordinates[1] + " " + coordinates[2] + " " + coordinates[3];
//swipe to obtain pin screen
ShellUtils.execCommand(cmdOne, true);
//input the password
ShellUtils.execCommand(cmds[2], true);
}
我遇到的问题是第二步,向上滑动以获取PIN屏幕。由于向上滑动某些电话进入大头针屏幕,因此有些向右滑动,甚至有些向右滑动然后向上滑动以显示解锁屏幕。我无法一一解决。
我认为可能有一些方法可以直接显示解锁屏幕,而无需滑动或使用植根电话,我可以运行一些代码以输入密码而不显示固定屏幕。但是我已经阅读了this this和关于它的一切。我找不到办法。我的minSdk
是19,而targetSdk
是28。有人可以帮我吗?