程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Learning Python is really useful, see how it controls your phone

編輯:Python

“PythonProgramming can do almost anything,只要你敢想,敢嘗試!”,Check it out todayPythonHow to code to control your android phone.Specifically, it replaces your hand,Implement automatic touches and some actions,實現自動化操作!Mainly used on Android phonesAndroid調試橋(Android Debug Bridge),它是一個非常有用的工具!in this quick guide to this article,我將向你展示如何使用Python代碼與ADB交互,and how to create2個快速腳本.

ADB(Android Debug Bridge)是一個命令行工具(CLI),可用於控制Android設備並與之通信.Many operations can be performed:例如安裝應用程序,調試應用程序,查找隱藏的功能並使用外殼程序直接與設備連接.要啟用ADB,Your device must first unlock developer options and enable itUSB調試——要解鎖開發人員選項,Go to device settings,然後向下滾動到“關於”部分,找到設備上當前軟件的內部版本號.Click the build number7次,將啟用“開發人員選項”.然後,您可以轉到設置中的“開發人員選項”面板,然後從那裡啟用USB調試.現在,基本大功告成,The only thing you need to do is use your phoneUSBcable to the computer.

     好,Today's journey is like this: 

  • Describes installation requirements
  • Get you started initially、帶你飛 
  • Introduces the basics of scripting 
  • 創建自拍計時器 
  • 創建定義搜索器

1. 安裝准備工作

The first of the two things that needs to be installed is on the computerADB工具.它會自動與Android Studio捆綁在一起,因此,如果已經安裝了Android Studio那就不用擔心.否則,You can go to the official documentation,And there should be instructions on how to install it at the top of the page.

安裝ADB工具後,需要獲取python庫,我們將使用該庫與ADBInterface with our mobile phone.安裝命令如下:

pip install pure-python-adb #安裝pure-python-adb庫.
復制代碼

The following is not required,但很有必要:To make it easier for us to develop scripts,可以安裝一個名為scrcpy的開源程序,該程序允許我們使用鼠標和鍵盤在我們的計算機上顯示和控制我們的android手機設備.要安裝它,可以轉到Githubrepository and download for your operating system(Windows,macOS或Linux)的正確版本.如果使用的是Windows,則將zip文件解壓縮到一個目錄中,然後將此目錄添加到您的路徑中.這樣一來,我們只需在終端窗口中鍵入scrcpy即可從系統上的任何位置訪問該程序.

2.Take you to get started and take you to fly

現在已經安裝了所有依賴項,可以啟動ADB並連接設備.首先,使用USB電纜將設備連接到PC,如果啟用了USB調試,則會彈出一條消息,詢問PC是否可以控制設備,只需回答是.然後在你的PC上,打開一個終端窗口,並通過鍵入adb start-server來啟動ADB服務器.這應該打印出以下消息:

如果還安裝了scrcpy,then just type in the terminalscrcpy即可啟動.但是,Only after adding it to the path,for this method to work,否則,This can be done by changing the terminal directory to installscrcpy的目錄並鍵入scrcpy.exe來打開可執行文件.希望一切順利,你應該能夠在PC上看到您的設備,並能夠使用鼠標和鍵盤對其進行控制.

現在,我們可以創建一個新的python文件,and check if the connected device can be found using the above library:

上面這裡,我們導入AdbClient類並使用它創建一個客戶端對象.然後,我們可以獲得連接的設備的列表.最後,我們從列表中獲得第一台設備(如果僅連接了一個設備,則通常是唯一的設備).

3.編寫腳本的基礎

The main way we want to connect with the device is to useshell,通過這種方式,我們可以發送命令以模擬特定位置的觸摸或從A滑動到B.要模擬屏幕觸摸(輕擊),我們首先需要工作了解屏幕坐標的工作方式.為了幫助解決這些問題,我們可以在開發人員選項中激活指針位置設置.激活後,無論您在屏幕上的何處觸摸,都可以看到該點的坐標顯示在頂部.The phone screen coordinate system works as follows:

It is in the upper left corner of the displayx和y坐標點(0,0),右下角的坐標是x和y的最大可能值.現在我們知道了坐標系的工作原理,我們需要檢查一下可以運行的不同命令.我在下面列出了命令列表以及如何使用它們,以供快速參考(英文不懂?Check it out)

4. 創建自拍計時器

哦,Now we roughly know what to do,讓我們開始吧,做一個簡單的例子.I'll show you how to create a quick selfie timer.首先,我們需要導入我們的庫並創建一個connect函數以連接到我們的設備:

你可以看到connect函數與前面的如何連接到設備的示例相同,除了這裡我們返回設備和客戶端對象以供以後使用.

in the main code above,我們可以調用connect函數來檢索設備和客戶端對象.從那裡我們可以打開相機應用程序,等待5秒鐘並拍照.真的就是這麼簡單!正如我之前說過的,This just replicates what you would normally do,因此,If you do it manually first and write down the steps,則思考如何做事情是最好的.

believe and learn from mePythonThe children's shoes can understand the above code?

5. 創建定義搜索器

現在,We do something more complicated:Ask the browser to find the definition of a specific word,並截圖以將其保存在我們的計算機上.

The basic flow of the program to be written is as follows:

1.打開浏覽器

2.單擊搜索欄

3.輸入搜索查詢

4.等待幾秒鐘

5.截圖並保存

但是,在開始之前,You need to find the coordinates of the search bar in the default browser,They can be easily found using the method I suggested earlier.對我來說,他們是(440,200).

首先,We have to import the same library as before,使用相同的connect方法.

在我們的主要函數中,我們可以調用connect函數,and for the search barx和y坐標分配一個變量.注意這是一個字符串,rather than a list or tuple,This way we can easily incorporate the coordinates into oursshell命令中.我們還可以從用戶那裡獲取輸入信息,以查看他們想要獲取哪個單詞的定義:

我們會將查詢添加到完整的句子中,然後對其進行搜索,這樣我們就可以始終獲取定義.之後,我們可以打開浏覽器,然後將搜索查詢輸入到搜索欄中,如下所示:

在這裡,我們使用eventID 66模擬輸入鍵的按下以執行搜索.如果需要,可以根據需要更改等待時間.最後,We use on device objectsscreencap方法獲取屏幕截圖,並將其另存為.png文件:

在這裡,We have to write byte mode("wb")打開文件,因為screencap方法返回表示圖像的字節.如果一切都按計劃進行,則應該有一個快速腳本來搜索特定單詞.Works like this on my phone:

希望你今天學到了一些新知識,在我對此進行研究之前,我其實也不知道,But the cool thing is:With it you can basically do anything you would normally do!沒錯,而且還可以做更多的事,Because it just simulates your own touches and movements on the phone!

以上就是本次分享的所有內容,如果你覺得文章還不錯,歡迎關注公眾號:Python編程學習圈,每日干貨分享,內容覆蓋Python電子書、教程、數據庫編程、Django,爬蟲,雲計算等等.或是前往編程學習網,了解更多編程技術知識.


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved