ADB操作手机的一个界面小工具(python实现)

  我们经常使用adb命令操作手机,因此我突然想到做一个界面把这些命令用界面点击的形式操作,这样可以简化我们平时敲命令的时间,工具的功能尚不完善,先总结一下思路。首先先把工具的界面展示一下:

  首先讲一下工具的环境配置:

          此工具采用python3.7实现,因为python的tkinter只支持显示gif图,所以这里采用ffmpeg将屏幕截图转换成gif并显示出来,因此需要ffmpeg环境(python和ffmpeg以及adb环境安装不在此篇文章的讨论范围之内,请自行百度)

          使用之前请先使用adb命令连接设备:

                   1.adb connect ip或者使用usb线连接手机

                    2.adb root 

                    3.如果使用无线adb调试的话,还需要再一次连接ip ,adb connect ip,因为adb root之后机器会断连adb。

          此工具主要实现了设备界面状态,亮灭屏,上下滑,截屏,查看mac、分辨率、屏幕密度以及wifi密码(需要root权限)的功能。此外还获取了设备安装的所有程序列表,以及设备sdcard目录列表。这两个列表本来想要实现一些功能,暂未完成。其中sdcard目录列表想做的思路如下:做一个可以在电脑上通过adb命令实现复制,粘贴,新建文件夹,删除文件/文件夹,安装apk,打开文件等功能,这些是待实现的,不过有了前面的功能铺垫,后续的也简单,只是时间问题而已。下面将已实现的功能代码总结出来:

# coding=utf-8
import tkinter as tk
import tkinter.messagebox #这个是消息框,对话框的关键
import tkinter.constants
import os
import threading

global deviceStatus
global showStatusInfo
global bm1
global statusPic
showStatusInfo = False

secondLine = 40
thirdLine = 80

def runCmd(str):
    p = os.popen(str)
    return p.read()

def screenShot():
    os.system("adb shell screencap -p /sdcard/screen.jpg")
    os.system("adb pull /sdcard/screen.jpg")
    return

def updatePic():
    screenShot()
    os.system("del screen.gif")
    os.system("ffmpeg -i screen.jpg -s 240x425 screen.gif")
    global bm1
    bm1 = tk.PhotoImage(file='screen.gif')
    global statusPic
    statusPic.configure(imag=bm1)
    return

def getDeviceStatus():
    status = runCmd("adb devices").strip()
    print(status)
    if status=="List of devices attached":
        status="当前无设备连接"
    else:
        status = status.replace("List of devices attached","").strip()
        t1 = threading.Thread(target=updatePic)
        t1.setDaemon(True)
        t1.start()
    deviceStatus.set(status)
    global showStatusInfo
    if showStatusInfo==True:
        tkinter.messagebox.showinfo("提示","设备状态已更新")
    showStatusInfo = True
    return status

def screenOn():
    os.system("adb shell input keyevent 224")
    return

def screenOff():
    os.system("adb shell input keyevent 223")
    return

def unlockScreen():
    os.system("adb shell input swipe 500 600 500 50")
    return

def downSlide():
    os.system("adb shell input swipe 500 50 500 600")
    return

def showMac():
    p = runCmd("adb shell cat /sys/class/net/wlan0/address")
    tk.messagebox.showinfo("提示","Android设备MAC地址为"+p)
    return

def getDensity():
    p = runCmd("adb shell wm density")
    tk.messagebox.showinfo("提示","屏幕密度为"+p)
    return

def getScreenSize():
    p = runCmd("adb shell wm size")
    tk.messagebox.showinfo("提示","android设备屏幕分辩率"+p)
    return

def getWifi():
    p = runCmd("adb shell cat /data/misc/wifi/*.conf")
    list = p.split("network=")
    i=1

    ########################测试代码#################################
    # s=0
    # while s<150:
    #     s+=1
    #     list.append(p.split("network=")[1])
    ########################测试代码#################################

    result = ""
    while i<len(list):
        ssid = list[i].split("ssid=")[1].split("psk=")[0].strip()
        psk = list[i].split("psk=")[1].split("key_mgmt=")[0].strip()
        result =result+ "wifi名称:"+ssid+" wifi密码:"+psk+"  --- "
        print(result)
        i+=1
    tk.messagebox.showinfo("所有wifi名称和密码",result)
    return

def getCurrentActivity():
    p = runCmd("adb shell dumpsys window | findstr mCurrentFocus")
    tk.messagebox.showinfo("当前activity",p)
    return

def getAllPkg():
    p = runCmd("adb shell pm list package")
   # tk.messagebox.showinfo("所有应用",p)
    list = p.split("\n\n")
    print(list)
    for s in list:
        packageStr = s.split(":")
        if len(packageStr)>1:
            s = s.split(":")[1]
            listb.insert(tkinter.constants.END,s)
    return p

def getAllFile():
    p = runCmd("adb shell ls /mnt/sdcard/")
    list = p.split("\n\n")
    print(list)
    for s in list:
        listFile.insert(tkinter.constants.END,s)
    return

root = tk.Tk() # 初始化Tk()
root.title("ADB界面工具V1.0")    # 设置窗口标题
root.geometry("1100x650")    # 设置窗口大小 注意:是x 不是*
root.resizable(width=False, height=False) # 设置窗口是否可以变化长/宽,False不可变,True可变,默认为True
deviceStatus = tk.StringVar()

getDeviceStatus()
#显示当前设备状态的文本框
statusText = tk.Label(root, textvariable=deviceStatus, fg="blue",bd=2,width=50,font = 'Helvetica -16')
statusText.place(x=150, y=10, anchor='nw')

#更新当前设备状态的按钮
updateBtn = tk.Button(root, text="更新状态",bd=2,width=10,font = 'Helvetica -16',command=getDeviceStatus)
updateBtn.place(x=500, y=5, anchor='nw')

screenOnBtn = tk.Button(root, text="亮屏",bd=2,width=5,font = 'Helvetica -16',command=screenOn)
screenOnBtn.place(x=0, y=secondLine, anchor='nw')

screenOffBtn = tk.Button(root, text="灭屏",bd=2,width=5,font = 'Helvetica -16',command=screenOff)
screenOffBtn.place(x=70, y=secondLine, anchor='nw')

unlockScreenBtn = tk.Button(root, text="上滑/解锁",bd=2,width=10,font = 'Helvetica -16',command=unlockScreen)
unlockScreenBtn.place(x=140, y=secondLine, anchor='nw')

unlockScreenBtn = tk.Button(root, text="下滑",bd=2,width=5,font = 'Helvetica -16',command=downSlide)
unlockScreenBtn.place(x=260, y=secondLine, anchor='nw')

screenShotBtn = tk.Button(root, text="截屏保存",bd=2,width=10,font = 'Helvetica -16',command=screenShot)
screenShotBtn.place(x=330, y=secondLine, anchor='nw')

showMacBtn = tk.Button(root, text="查看mac地址",bd=2,width=10,font = 'Helvetica -16',command=showMac)
showMacBtn.place(x=450, y=secondLine, anchor='nw')

showMacBtn = tk.Button(root, text="查看分辨率",bd=2,width=10,font = 'Helvetica -16',command=getScreenSize)
showMacBtn.place(x=570, y=secondLine, anchor='nw')

getDensityBtn = tk.Button(root, text="查看屏幕密度",bd=2,width=10,font = 'Helvetica -16',command=getDensity)
getDensityBtn.place(x=700, y=secondLine, anchor='nw')

getDensityBtn = tk.Button(root, text="查看连接过的wifi和密码(root)",bd=2,width=25,font = 'Helvetica -16',command=getWifi)
getDensityBtn.place(x=0, y=thirdLine, anchor='nw')

getCurrentActivityBtn = tk.Button(root, text="当前activity",bd=2,width=10,font = 'Helvetica -16',command=getCurrentActivity)
getCurrentActivityBtn.place(x=260, y=thirdLine, anchor='nw')



listb  = tk.Listbox(root,width=50,height=12)
listb.place(x=1, y=120, anchor='nw')

listFile  = tk.Listbox(root,width=50,height=12)
listFile.place(x=460, y=120, anchor='nw')

getAllFile()

#listb.bind("<<ListboxSelect>>",mouseCallBack)
global bm1
bm1 = tk.PhotoImage(file='screen.gif')
global statusPic
statusPic = tk.Label(root, image=bm1,width=250)
statusPic.place(x=830, y=10, anchor='nw')

getAllPkg()

root.mainloop() # 进入消息循环

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

景兄弟1366

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值