安卓 模拟 按键、点击、输入、滑动屏幕


	public void HomeClick(View view)
	{
		SuProcess.KeyClick(KeyCode.Home);
		
		for (int i = 0; i < 3; i++)
		{
			SuProcess.ScreenSwipe_Right(MainActivity.this);
			SuProcess.ScreenSwipe_Left(MainActivity.this);
		}
		
		// SuProcess.ScreenSwipe(1200, 100, 80, 100);
		// SuProcess.ScreenSwipe(80, 100, 1280, 100);
	}

package sc.tool.shell;

import java.io.DataOutputStream;
import java.io.OutputStream;

import android.content.Context;
import android.os.Handler;
import android.util.DisplayMetrics;

/** 获取su进程执行 */
public class SuProcess
{
	private static long dellayMills = 0;
	private static long AddMills = 0;
	
	/** 添加延时 */
	public static void Delay(long mills)
	{
		AddMills = mills;
		dellayMills += mills;
	}
	
	/** 执行命令 */
	public static void Exec(final String cmd)
	{
		Runnable r = new Runnable()
		{
			long AddMillsT = SuProcess.AddMills;
			
			@Override
			public void run()
			{
				try
				{
					dellayMills -= AddMillsT;
					
					Process process = Runtime.getRuntime().exec("su");	// 获取su进程
					
					// 获取输出流
					OutputStream outputStream = process.getOutputStream();
					DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
					dataOutputStream.writeBytes(cmd);
					dataOutputStream.flush();
					dataOutputStream.close();
					outputStream.close();
				}
				catch (Exception ex)
				{
					ex.printStackTrace();
				}
			}
		};
		
		AddMills = 0;
		if(dellayMills <= 0) dellayMills = 0;
		new Handler().postDelayed(r, dellayMills);
	}
	
	/** 模拟按键点击 */
	public static void KeyClick(int key)
	{
		String cmd = "input keyevent " + key;
		Exec(cmd);
	}
	
	/** 模拟屏幕点击坐标 x,y */
	public static void ScreenClick(int x, int y)
	{
		String cmd = "input tap " + x + " " + y;
		Exec(cmd);
	}
	
	/** 模拟输入信息text */
	public static void InputText(String text)
	{
		String cmd = "input text '" + text + "'";
		Exec(cmd);
	}
	
	/** 模拟滑动屏幕 */
	public static void ScreenSwipe(final int x1, final int y1, final int x2, final int y2)
	{
		Delay(1100);	// 滑动屏幕时执行延时
		
		String cmd = "input swipe" + " " + x1 + " " + y1 + " " + x2 + " " + y2;
		Exec(cmd);
	}
	
	/** 向右滑动屏幕 */
	public static void ScreenSwipe_Right(Context context)
	{
		DisplayMetrics screen = getScreeenSize(context);
		
		int space = screen.widthPixels / 4;
		
		int x1 = space;
		int y1 = screen.heightPixels / 2;
		int x2 = screen.widthPixels - space;
		int y2 = y1;
		
		ScreenSwipe(x1, y1, x2, y2);
	}
	
	/** 向左滑动屏幕 */
	public static void ScreenSwipe_Left(Context context)
	{
		DisplayMetrics screen = getScreeenSize(context);
		
		int space = screen.widthPixels / 4;
		
		int x1 = screen.widthPixels - space;
		int y1 = screen.heightPixels / 2;
		int x2 = space;
		int y2 = y1;
		
		ScreenSwipe(x1, y1, x2, y2);
	}
	
	/** 向上滑动屏幕 */
	public static void ScreenSwipe_Up(Context context)
	{
		DisplayMetrics screen = getScreeenSize(context);
		
		int x1 = screen.widthPixels / 2;
		int y1 = screen.heightPixels - 10;
		int x2 = x1;
		int y2 = 10;
		
		ScreenSwipe(x1, y1, x2, y2);
	}
	
	/** 向下滑动屏幕 */
	public static void ScreenSwipe_Down(Context context)
	{
		DisplayMetrics screen = getScreeenSize(context);
		
		int x1 = screen.widthPixels / 2;
		int y1 = 10;
		int x2 = x1;
		int y2 = screen.heightPixels - 10;
		
		ScreenSwipe(x1, y1, x2, y2);
	}
	
	/** 获取屏幕分辨率 */
	private static DisplayMetrics getScreeenSize(Context context)
	{
		DisplayMetrics dm = context.getResources().getDisplayMetrics();
		
		// int screenWidth = dm.widthPixels; // 屏幕宽度
		// int screenHeight = dm.heightPixels; // 屏幕高度
		
		return dm;
	}
}

package sc.tool.shell;

/** 按键键值 */
public final class KeyCode
{
	public final static int Home = 3;
	public final static int Back = 4;
	
	public final static int Up = 19;
	public final static int Down = 20;
	public final static int Left = 21;
	public final static int Right = 22;
	public final static int Select = 23;
	public final static int Ok = 23;
	
	public final static int VolumeAdd = 24;
	public final static int VolumeSub = 25;
	public final static int Power = 26;
	
	public final static int Menu = 82;
	
//	KEYCODE_MENU 1
//	KEYCODE_SOFT_RIGHT 2
//	KEYCODE_HOME 3
//	KEYCODE_BACK 4
//	KEYCODE_CALL 5
//	KEYCODE_ENDCALL 6
//	KEYCODE_0 7
//	KEYCODE_1 8
//	KEYCODE_2 9
//	KEYCODE_3 10
//	KEYCODE_4 11
//	KEYCODE_5 12
//	KEYCODE_6 13
//	KEYCODE_7 14
//	KEYCODE_8 15
//	KEYCODE_9 16
//	KEYCODE_STAR 17
//	KEYCODE_POUND 18
//	KEYCODE_DPAD_UP 19
//	KEYCODE_DPAD_DOWN 20
//	KEYCODE_DPAD_LEFT 21
//	KEYCODE_DPAD_RIGHT 22
//	KEYCODE_DPAD_CENTER 23
//	KEYCODE_VOLUME_UP 24
//	KEYCODE_VOLUME_DOWN 25
//	KEYCODE_POWER 26
//	KEYCODE_CAMERA 27
//	KEYCODE_CLEAR 28
//	KEYCODE_A 29
//	KEYCODE_B 30
//	KEYCODE_C 31
//	KEYCODE_D 32
//	KEYCODE_E 33
//	KEYCODE_F 34
//	KEYCODE_G 35
//	KEYCODE_H 36
//	KEYCODE_I 37
//	KEYCODE_J 38
//	KEYCODE_K 39
//	KEYCODE_L 40
//	KEYCODE_M 41
//	KEYCODE_N 42
//	KEYCODE_O 43
//	KEYCODE_P 44
//	KEYCODE_Q 45
//	KEYCODE_R 46
//	KEYCODE_S 47
//	KEYCODE_T 48
//	KEYCODE_U 49
//	KEYCODE_V 50
//	KEYCODE_W 51
//	KEYCODE_X 52
//	KEYCODE_Y 53
//	KEYCODE_Z 54
//	KEYCODE_COMMA 55
//	KEYCODE_PERIOD 56
//	KEYCODE_ALT_LEFT 57
//	KEYCODE_ALT_RIGHT 58
//	KEYCODE_SHIFT_LEFT 59
//	KEYCODE_SHIFT_RIGHT 60
//	KEYCODE_TAB 61
//	KEYCODE_SPACE 62
//	KEYCODE_SYM 63
//	KEYCODE_EXPLORER 64
//	KEYCODE_ENVELOPE 65
//	KEYCODE_ENTER 66
//	KEYCODE_DEL 67
//	KEYCODE_GRAVE 68
//	KEYCODE_MINUS 69
//	KEYCODE_EQUALS 70
//	KEYCODE_LEFT_BRACKET 71
//	KEYCODE_RIGHT_BRACKET 72
//	KEYCODE_BACKSLASH 73
//	KEYCODE_SEMICOLON 74
//	KEYCODE_APOSTROPHE 75
//	KEYCODE_SLASH 76
//	KEYCODE_AT 77
//	KEYCODE_NUM 78
//	KEYCODE_HEADSETHOOK 79
//	KEYCODE_FOCUS 80
//	KEYCODE_PLUS 81
//	KEYCODE_MENU 82
//	KEYCODE_NOTIFICATION 83
//	KEYCODE_SEARCH 84
//	TAG_LAST_KEYCODE 85
}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值