前提准备: 1.http://www.android-studio.org/ 下载手机android sdk manage,根据adb在电脑上的环境变量 2.手机 (我的是荣耀10 android 8.1.0 屏幕大小2280 1080) 3.查看robot类使用 ,查看手机 adb 简单语法
步骤:1.手机打开开发者模式,连到电脑
2.电脑调到cmd模式,输入adb services 查看是否有手机接入(两个目的:1.检查adb可以看出是否好的2.可以看到手机的驱是否可以)
3.在idea输入一下代码(参数有你手机实际来调)
package org.pgj;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.*;
/**
* @author Administrator
* @date 2018/12/18 1:39
*/
@SpringBootApplication
public class App {
private volatile static int second1 = 500;
private static Robot robot;
private static Thread thread = new Thread(() -> {
startADB();
});
static {
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
}
public enum State {
YSE, NO
}
public static void main(String... args){
drawFrame();
}
public static void drawFrame() {
// 创建 JFrame 实例
JFrame frame = new JFrame("Example");
// Setting the width and height of frame
frame.setSize(350, 150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel);
frame.setVisible(true);
}
private static void placeComponents(JPanel panel) {
panel.setLayout(null);
JLabel userLabel1 = new JLabel("刷新次数:");
userLabel1.setBounds(10, 20, 80, 25);
panel.add(userLabel1);
/*
* 创建文本域用于用户输入
*/
final JTextField userText1 = new JTextField(20);
userText1.setBounds(100, 20, 165, 25);
userText1.setText("500");
panel.add(userText1);
// 创建运行按钮
JButton loginButton = new JButton("开始");
loginButton.setBounds(150, 60, 80, 25);
panel.add(loginButton);
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
second1= Integer.valueOf(userText1.getText());
if(!thread.isAlive()){
thread.start();
}
}
});
}
public static void startADB() {
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
robot.setAutoDelay(100);
pressCMDEdit();
pressADBEdit();
for (int i = 0; i < second1; i++) {
pressFirstTap();
pressSwipe();
robot.delay(100);
pressFlassh();
}
}
/*
* 单击
* */
private static void pressMeta(int key) {
robot.keyPress(key);
robot.keyRelease(key);
}
private static void pressString(int... keys) {
for (int i = 0; i < keys.length; i++) {
if (keys[i] == 0) {
continue;
}
pressMeta(keys[i]);
}
pressMeta(KeyEvent.VK_ENTER);
robot.delay(50);
}
private static int[] convertStrsToInts(String strs) {
int[] targe = new int[100];
Class<KeyEvent> keyEventClass = KeyEvent.class;
Field[] fields = keyEventClass.getDeclaredFields();
Map<String, Integer> fieldMap = getFieldMap(fields);
for (int i = 0; i < strs.length(); i++) {
String targeName = String.valueOf(strs.charAt(i));
if (targeName.equals(" ")) {
targe[i] = KeyEvent.VK_SPACE;
continue;
}
Integer targeInteger = fieldMap.get(targeName);
if (targeInteger == null) {
System.out.println(targeName);
continue;
}
targe[i] = targeInteger.intValue();
}
return targe;
}
public static Map<String, Integer> getFieldMap(Field[] fields) {
Map<String, Integer> map = new HashMap<>();
for (Field field : fields) {
field.setAccessible(true);
if (field.getType() == int.class && Modifier.isStatic(field.getModifiers())) {
String fieldName = field.getName();
int i = fieldName.lastIndexOf("_");
if (i == 0 || i < 0) {
continue;
}
;
String targeName = fieldName.substring(i + 1, fieldName.length()).toLowerCase();
try {
int keyValue = field.getInt(KeyEvent.class);
map.put(targeName, keyValue);
} catch (IllegalAccessException e) {
continue;
}
}
}
return map;
}
/*
* 快捷键
* */
private static void pressTwoMeta(int... keys) {
for (int i = 0; i < keys.length; i++) {
if (keys[i] == 0) {
continue;
}
robot.keyPress(keys[i]);
}
for (int i = 0; i < keys.length; i++) {
if (keys[i] == 0) {
continue;
}
robot.keyRelease(keys[i]);
}
robot.delay(50);
}
/*
* 进入cmd编辑页面
* */
private static void pressCMDEdit(){
pressTwoMeta(KeyEvent.VK_WINDOWS, KeyEvent.VK_R);
pressString(convertStrsToInts("cmd"));
}
/*
* 进入ADB编辑页面
* */
private static void pressADBEdit() {
pressString(convertStrsToInts("adb shell"));
}
/*下滑
* */
private static void pressSwipe() {
for (int i = 0; i < 13; i++) {
pressString(convertStrsToInts("input swipe 400 1000 400 100 1000 "));
pressSometimeView();
if (i == 1) {
pressString(convertStrsToInts("input tap 400 800"));
}
}
robot.delay(100);
pressString(convertStrsToInts("input tap 50 200"));
}
private static void pressFlassh() {
pressString(convertStrsToInts("input swipe 400 400 400 1000 150"));
}
/*
* 点击第一行广告
* */
private static void pressFirstTap() {
pressString (convertStrsToInts("input tap 400 400"));
}
/*处理偶尔出现的彩蛋
* */
private static void pressSometimeView(){
///
}
}
唉,随便写写玩玩,沾点外挂的边!后续努力