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() # 進入消息循環

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章