package com.example.emu_ddd.tools; import android.content.Context; import android.graphics.Bitmap; public class AdbManager { public static boolean findPicArea(int x, int y, int wid, int het, String md5Tag) { AdbUtil util = AdbUtil.getIns(); return util.findPicArea(x, y, wid, het, md5Tag); } public static boolean findPicArea(int x, int y, int wid, int het, String md5Tag, long time) { AdbUtil util = AdbUtil.getIns(); return util.findPicArea(x, y, wid, het, md5Tag, time); } public static void kill(String packageName) { AdbSuUtil.kill(packageName); } public static void screenshot(String sdfilename) { AdbSuUtil.screenshot(sdfilename); } public static void clickScreen(int x, int y) { AdbSuUtil.clickScreen(x, y); } public static void enterText(String text) { AdbSuUtil.enterText(text); } public static String getImgMd5(Bitmap bp, int x, int y, int wid, int het) { AdbUtil util = AdbUtil.getIns(); return util.getImgMd5(bp, x, y, wid, het); } public static String getImgMd5(Bitmap bp) { AdbUtil util = AdbUtil.getIns(); return util.getImgMd5(bp); } public static void clearData(String packageName) { AdbSuUtil.delData(packageName); } public static void clearDatas(String packageName, String[] folders) { AdbSuUtil.delDatas(packageName, folders); } public static void sendKey(int key) { AdbSuUtil.sendKey(key); } public static String getTopActName(Context context) { AdbUtil util = AdbUtil.getIns(); return util.getTopActName(context); } }
package com.example.emu_ddd.tools; import android.os.Environment; import android.util.Log; import java.io.IOException; import java.io.OutputStream; public class AdbSuUtil { private static String tag = "Emu_suutil"; public static void kill(String packageName) { try { Process process = Runtime.getRuntime().exec("su"); OutputStream out = process.getOutputStream(); String cmd = "am force-stop " + packageName + " \n"; try { out.write(cmd.getBytes()); out.flush(); } catch (IOException e) { e.printStackTrace(); } process.getOutputStream().close(); process = null; } catch (Exception e) { e.printStackTrace(); } } public static void screenshot(String sdfilename) { try { Log.e(tag, "screenshot..."); Process sh = Runtime.getRuntime().exec("su"); OutputStream os = sh.getOutputStream(); os.write(("/system/bin/screencap -p /sdcard/" + sdfilename + "\n") .getBytes()); os.flush(); os.write("exit\n".getBytes()); os.flush(); sh.waitFor(); os.close(); Log.e(tag, "screenshoted!"); } catch (Exception e) { e.printStackTrace(); } } public static void clickScreen(int x, int y) { Log.e(tag, "click " + x + "," + y); String[] cmds = { "sendevent /dev/input/event0 3 0 " + x, "sendevent /dev/input/event0 3 1 " + y, "sendevent /dev/input/event0 1 330 1", "sendevent /dev/input/event0 0 0 0", "sendevent /dev/input/event0 1 330 0", "sendevent /dev/input/event0 0 0 0" }; try { Process sh = Runtime.getRuntime().exec("su"); OutputStream os = sh.getOutputStream(); for (int i = 0; i < cmds.length; i++) { os.write((cmds[i] + "\n").getBytes()); os.flush(); } os.write("exit\n".getBytes()); os.flush(); sh.waitFor(); os.close(); sh = null; } catch (Exception e) { e.printStackTrace(); } } public static void enterText(String text) { Log.e(tag, "enter : " + text); try { Process sh = Runtime.getRuntime().exec("su"); OutputStream os = sh.getOutputStream(); os.write(("input text " + text + "\n").getBytes()); os.flush(); os.write("exit\n".getBytes()); os.flush(); sh.waitFor(); os.close(); sh = null; } catch (Exception e) { e.printStackTrace(); } } public static void sendKey(int key) { Log.e(tag, "sendKey : " + key); try { Process sh = Runtime.getRuntime().exec("su"); OutputStream os = sh.getOutputStream(); os.write(("input keyevent " + key + "\n").getBytes()); os.flush(); os.write("exit\n".getBytes()); os.flush(); sh.waitFor(); os.close(); sh = null; } catch (Exception e) { e.printStackTrace(); } } public static void delDatas(String packageName, String[] folders) { Log.e(tag, "del : " + packageName); String basePath = Environment.getDataDirectory() + "/data/" + packageName + "/"; try { Process sh = Runtime.getRuntime().exec("su"); OutputStream os = sh.getOutputStream(); for (int i = 0; i < folders.length; i++) { os.write(("rm -r " + basePath + folders[i] + "\n").getBytes()); os.flush(); } os.write("exit\n".getBytes()); os.flush(); sh.waitFor(); os.close(); sh = null; } catch (Exception e) { e.printStackTrace(); } } public static void delData(String packageName) { delDatas(packageName, new String[] { "files", "cache", "shared_prefs", "databases" }); } }
package com.example.emu_ddd.tools; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.math.BigInteger; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.List; import android.app.ActivityManager; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Environment; import android.util.Log; public class AdbUtil { private String tag = "emu_zutil"; private static AdbUtil ins = new AdbUtil(); public static AdbUtil getIns() { return ins; } public String getSdpah() { return Environment.getExternalStorageDirectory().getPath(); } public byte[] bitmapToBytes(Bitmap bitmap) { if (bitmap == null) { return null; } ByteArrayOutputStream os = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, os); return os.toByteArray(); } public void quit(boolean result, String des) { if (result) Log.i(this.tag, des + "成功!"); else Log.i(this.tag, des + "失败!"); } public boolean findPicArea(int x, int y, int wid, int het, String md5Tag) { return findPicArea(x, y, wid, het, md5Tag, 20111L); } public boolean findPicArea(int x, int y, int wid, int het, String md5Tag, long time) { try { long tagTime = System.currentTimeMillis(); while (System.currentTimeMillis() - tagTime <= time) { File xy1 = new File(getSdpah() + "/xy1.png"); if (xy1.exists()) { xy1.delete(); } AdbSuUtil.screenshot("xy1.png"); Thread.sleep(2000L); while (!xy1.exists()) { Log.e(this.tag, "截图不存在!!!"); AdbSuUtil.screenshot("xy1.png"); Thread.sleep(5000L); } InputStream in = new FileInputStream(xy1); Bitmap b = BitmapFactory.decodeStream(in); while (b == null) { Thread.sleep(2000L); b = BitmapFactory.decodeStream(in); } Log.i(this.tag, "解析到bitMap!"); String ct = getImgMd5(b, x, y, wid, het); if (ct.equals(md5Tag)) { return true; } Thread.sleep(3000L); } return false; } catch (Exception e) { e.printStackTrace(); } return false; } public static String getMD5String(byte[] bytes) { MessageDigest digest = null; try { digest = MessageDigest.getInstance("MD5"); digest.update(bytes); BigInteger bigInt = new BigInteger(1, digest.digest()); return bigInt.toString(16); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return null; } public String getImgMd5(Bitmap bp, int x, int y, int wid, int het) { try { Bitmap nb = Bitmap.createBitmap(bp, x, y, wid, het); String ct = ""; ct = getMD5String(bitmapToBytes(nb)); return ct; } catch (Exception e) { e.printStackTrace(); } return null; } public String getImgMd5(Bitmap bp) { try { return getMD5String(bitmapToBytes(bp)); } catch (Exception e) { e.printStackTrace(); } return ""; } public String getTopActName(Context context) { try { ActivityManager am = (ActivityManager) context .getSystemService("activity"); List list = am.getRunningTasks(5); return ((ActivityManager.RunningTaskInfo) list.get(0)).topActivity .getClassName().toString(); } catch (Exception e) { e.printStackTrace(); } return ""; } }
说明:
adb shell sendevent /dev/input/event0 3 0 110 //x坐标
adb shell sendevent /dev/input/event0 3 1 70 //y坐标
adb shell sendevent /dev/input/event0 1 330 1 //按下状态,准确的说是有压力值
adb shell sendevent /dev/input/event0 0 0 0 //必要的一行数据
adb shell sendevent /dev/input/event0 1 330 0 //抬起状态,无压力值
adb shell sendevent /dev/input/event0 0 0 0 //必要的一行,相当于终止一段完整数据的标致