首页 > 编程语言 >C# adb封装类

C# adb封装类

时间:2023-02-03 19:44:55浏览次数:39  
标签:封装 String C# devices internal adb input ADBPATH

为了方便操作apk 实现app的自动化点击 封装了个adb操作了。基本上的操作都有了

using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;

class Emulator {

    static readonly String ADBPATH = "D:/Microvirt/MEmu/"; //adb 所在目录
    static readonly String ROOTPATH = AppDomain.CurrentDomain.BaseDirectory;
    static readonly String SAVEPATH = AppDomain.CurrentDomain.BaseDirectory + "temp";
    static readonly String SNAPEPATH = SAVEPATH + @"\gsnap.png";
    static readonly String TEMPLATE = AppDomain.CurrentDomain.BaseDirectory + "template/";

    internal String devices { get; set; }

    internal Emulator() {
    }

    internal Emulator(String devices) {
        this.devices = devices;
    }
    //获取的所有能连接使用的设备
    internal List<String> GetDevices() {
        List<String> lst = new List<string>();
        String result = Common.Execute(ADBPATH + "adb devices");
        String[] lines = Regex.Split(result, "\r\n");// result.Split('\n');
        foreach (String line in lines) {
            if (line.Contains("\tdevice")) {
                lst.Add(line.Replace("\tdevice", ""));
            }
        }
        return lst;
    }
    //获取当前运行的APP 最顶层的Activity
    internal string GetTopActivity() {
        try {
            MatchCollection matchs;
            IEnumerator enumerator;
            String input = "";
            input = Common.Execute(ADBPATH + "adb -s " + devices + " shell dumpsys activity|findstr mFocusedActivity"); //获取最顶层的 activity
            if (input.IndexOf("HistoryRecord") > 0) {
                matchs = Regex.Matches(input, @"HistoryRecord.*?\}", RegexOptions.Singleline);
            }
            else {
                matchs = Regex.Matches(input, @"ActivityRecord.*?\}", RegexOptions.Singleline);
            }
            int num = 0;
            try {
                enumerator = matchs.GetEnumerator();
                while (enumerator.MoveNext()) {
                    Match current = (Match)enumerator.Current;
                    num++;
                    return current.Value;
                }
            }
            catch { }
            return "";
        }
        catch {
        }
        return "";
    }

    //截屏
    internal void snape() {
         Common.Execute(ADBPATH + "adb -s " + devices + " exec-out screencap -p > " + SNAPEPATH);
    }
    //载屏并保存为指定名称
    internal void snape(String name) {
        String path = SAVEPATH + "/" + name + ".bmp";
        Common.Execute(ADBPATH + "adb -s " + devices + " exec-out screencap -p > " + path);
    }

    //判断是否安装了某个包(APP)
    internal Boolean CheckApp(String package) {
        String Result = Common.Execute(ADBPATH + "adb -s " + devices + " shell pm list packages");
        return Result.IndexOf(package) > 0;
    }

    //安装APP
    internal Boolean install(String package) {
        String Result = Common.Execute(ADBPATH + "adb -s " + devices + " install " + package);
        return Result.IndexOf("Success", StringComparison.CurrentCultureIgnoreCase) > 0;
    }

    //卸载某个应用  //包名 可通过 adb shell pm list packages -s 来查看
    internal void uninstall(String package) {
         Common.Execute(ADBPATH + "adb -s " + devices + " uninstall " + package);
    }

    //启用APP  
    internal void start(String package) {
        Common.Execute(ADBPATH + "adb -s " + devices + " shell am start -n " + package);
    }
    //结果运行中的APP
    internal void stop(String package) {
        Common.Execute(ADBPATH + "adb -s " + devices + " shell am force-stop " + package);
    }

    //单击某区域
    internal void click(Point point) {
        String cmd = ADBPATH + "adb -s " + devices + " shell input tap " + (point.X) + " " + point.Y;
        Common.Execute(cmd);
    }
    //长按某个按
    internal void LongPress(String Key) {
        Common.Execute(ADBPATH + "adb - s " + devices + " input keyevent--longpress " + Key);
    }
    //按住某点滑动
    internal void swipe(Point first, Point second, int time) {
        Common.Execute(ADBPATH + "adb - s " + devices + " input swipe " + first.X + " " + first.Y + " " + second.X + " " + second.Y + " " + time);
    }

    //输入文本信息
    internal void text(String text) {
        Common.Execute(ADBPATH + "adb -s " + devices + " shell input text " + text);
    }
    //发送键值
    internal void key(String key) {
        Common.Execute(ADBPATH + "adb -s " + devices + " shell input keyevent " + key);
    }

    //检测某应用是否运行
    internal bool isrun(String packname) {
        String Result = Common.Execute(ADBPATH + "adb -s " + devices + " shell ps");
        return Result.IndexOf(packname, StringComparison.CurrentCultureIgnoreCase) > 0;
    }
    //滑动解锁
    internal void unlock() {
        StringBuilder cmd = new StringBuilder();
        cmd.AppendLine(ADBPATH + "adb -s " + devices + " shell sendevent /dev/input/event0 3 0 32");
        cmd.AppendLine(ADBPATH + "adb -s " + devices + " shell sendevent /dev/input/event0 3 1 353");
        cmd.AppendLine(ADBPATH + "adb -s " + devices + " shell sendevent /dev/input/event0 1 330 1");
        cmd.AppendLine(ADBPATH + "adb -s " + devices + " shell sendevent /dev/input/event0 0 0 0");
        cmd.AppendLine(ADBPATH + "adb -s " + devices + " shell sendevent /dev/input/event0 3 0 260");
        cmd.AppendLine(ADBPATH + "adb -s " + devices + " shell sendevent /dev/input/event0 0 0 0");
        cmd.AppendLine(ADBPATH + "adb -s " + devices + " shell sendevent /dev/input/event0 1 330 0");
        cmd.AppendLine(ADBPATH + "adb -s " + devices + " shell sendevent /dev/input/event0 0 0 0");
        Common.Execute(cmd.ToString());
    }
}

  

标签:封装,String,C#,devices,internal,adb,input,ADBPATH
From: https://www.cnblogs.com/ningqhai/p/17090287.html

相关文章

  • 神经网络--从0开始搭建全连接网络和CNN网络
    前言:Hello大家好,我是Dream。今天来学习一下如何从0开始搭建全连接网络和CNN网络,并通过实验简单对比一下两种神经网络的不同之处,本文目录较长,可以根据需要自动选取要看的......
  • EMR修改Core节点EBS卷大小
    本文用于修改已创建好的EMR集群Core节点EBS磁盘容量,由于业务场景、成本等多方面考虑,需要周期性的调整Core节点的磁盘容量,但是通过人工调整EBS卷大小非常耗时,本文通过Bootstr......
  • #yyds干货盘点# LeetCode面试题:无重复字符的最长子串
    1.简述:给定一个字符串s,请你找出其中不含有重复字符的 最长子串 的长度。 示例 1:输入:s="abcabcbb"输出:3解释:因为无重复字符的最长子串是"abc",所以其长度为......
  • #yyds干货盘点# LeetCode程序员面试金典: 递归乘法
    题目:递归乘法。写一个递归函数,不使用*运算符,实现两个正整数的相乘。可以使用加号、减号、位移,但要吝啬一些。示例1:输入:A=1,B=10输出:10示例2:输入:A=3,B=4......
  • webrtc RTCPeerConnection前端使用记录
    参考资料​​https://developer.mozilla.org/zh-CN/docs/Web/API/RTCPeerConnection​​​​https://developer.mozilla.org/zh-CN/docs/Web/API/RTCPeerConnection#created......
  • WGCLOUD简介与特点 - 主机列表中的磁盘使用率是什么
    WGCLOUD监测平台中的模块,【主机列表】的磁盘总使用率百分比和【系统页面】的磁盘空间列表,有时候我们看的时候可能不明白,有什么区别,有什么联系这是因为【主机列表】的磁盘总......
  • Java集合 Map 集合 与 操作集合的工具类: Collections 的详细说明
    Java集合Map集合与操作集合的工具类:Collections的详细说明每博一文案别把人生,输给心情师父说:心情不是人生的全部,却能左右人生的全部。你有没有体会到,当你心......
  • Promise 封装 $.ajax
    完整文档 html<inputtype="file"multipleid="filess"><buttononclick="jqueryAjax3()">上传文件ss</button><pid="filess_progress"style="width:0px;height......
  • git推送时(push)shell脚本的行分隔符类型依然是CRLF,没有变成LF
    git的autocrlf设置会在push推的时候把CRLF变成LF,pull拉的时候会把LF变成CRLF(视系统而定),需要注意的一点就是git每次推不会作用于项目全部文件,只作用于检测到变更的文件 ......
  • Jvm--MinorGC过程
    第一次GC:在不断创建对象的过程中,当Eden区域被占满,此时会开始做YoungGC也叫MinorGC1)第一次GC时Survivous中S0区和S1区都为空,将其中一个作为ToSurvivous(用来存储Eden......