1. 程式人生 > >電腦通過 adb 控制安卓手機自動加微信

電腦通過 adb 控制安卓手機自動加微信

最近做了個需求,從電腦上通過資料線控制手機打電話和自動新增微信好友。查詢一番,很快有了方向,那就是通過 adb工具,即Android Debug Bridge(安卓除錯橋) tools。它就是一個命令列視窗,用於通過電腦端與模擬器或者真實裝置(也就是手機)互動。

好了,知道用什麼就好整了,下面簡單介紹下過程

1.下載adb工具
2.手機配置

首先當然得是安卓機了(不過貌似也有ios版本的仿寫adb,沒有試過,沒試過暫且不管),需要將手機的的開啟開發者選項,然後開啟usb除錯,允許usb控制以及模擬點選之類的設定,這個隨手機型號的不同而不同。
這裡寫圖片描述
連上資料線插到電腦,一般都會自動安裝驅動了,如果你中招了,安裝失敗的話,去下個豌豆莢之類的手機助手即可,電腦可以識別到手機就ok了,下面就可以測試adb了

3.測試adb命令

從cmd中進到下載好的adb工具資料夾內,就可以開始敲命令了,看著自己的手機在動是不是很神奇。上面給的命令連線目錄做的很好了,贊一個。
可以檢視連線裝置,查手機訊號,電量,解析度等等引數,可以開啟應用,模擬點選,滑動,輸入等

//電源鍵
adb shell input keyevent 26
//選單鍵
adb shell input keyevent 82
//home鍵
adb shell input keyevent 3
//返回鍵
adb shell input keyevent 4
//開啟微信
adb shell am start -n com.tencent.mm/.ui.LauncherUI
//模擬點選 100 300是座標
adb shell input tap 100 300 //模擬滑動 300 1000 300 500 分別表示起始點x座標 起始點y座標 結束點x座標 結束點y座標。 adb shell input swipe 300 1000 300 500 //模擬輸入 hello adb shell input text hello //撥打電話 10086 adb shell am start -a android.intent.action.CALL tel:10086 //編輯簡訊hello(不支援中文,不過好像有方法做到,貌似需要在手機安裝特殊的輸入法,暫不搞) adb shell am start -a android.intent.action.SENDTO -d sms:10086
--es sms_body hello adb shell input keyevent 22 //焦點去到傳送按鍵 adb shell input keyevent 66 //回車,就是按下發送鍵

有了這些模擬操作基本就可以自動化一個操作了
我實現的主要是微信自動加好友,如果只是用於自己的手機玩玩的話,可以用固定的座標點去實現,這個就非常簡單了(但碰到那些會變化的介面,那麼你固定的座標就偏移了)。那麼只好像別的辦法了,有沒有方法去獲取頁面各個控制元件的座標呢?
答案當然是有了(O(∩_∩)O哈哈~)安卓裡面有個 uiautomator 的東西。

4. Uiautomator 獲取頁面元素座標

Uiautomator是谷歌推出的,用於UI自動化測試的工具,也就是普通的手工測試,點選每個控制元件元素看看輸出的結果是否符合預期。(只支援 Android 4.3 以上系統,貌似現在沒有低於這個版本的手機了吧)
獲取當前手機頁面各個元素資訊方法如下:

  • 抓取當前頁面元素佈局資訊儲存到手機自動位置
adb shell uiautomator dump /data/local/tmp/uidump.xml
  • 將手機裡的xml檔案匯出到電腦指定位置,預設時儲存到adb.exe目錄下
adb pull /data/local/tmp/uidump.xml
  • 解析xml 得到目的控制元件的座標

這個得去找個xml解析類咯,我用的是 Rapidxml,用過的都說好。
解析微信的介面的話,得寫個遞迴函式去遍歷所有的元素,然後找出元素中 content-desc 和 text 不為空的控制元件,比如微信的新增朋友介面
這裡寫圖片描述

以下就是解析上個那個介面後,刪除 content-desc 和 text 都為空的元素,將幾個關鍵key打印出來的log,重點就是 bounds 這個key值了,它的值就是這個控制元件在螢幕上所佔矩形區域。有了這個座標,你想點哪裡還不能點了?

< text: , resource-id: , class: android.widget.FrameLayout, content-desc: 當前所在頁面,新增朋友, bounds: [0,0][1080,2030] >
< text: , resource-id: com.tencent.mm:id/hy, class: android.widget.ImageView, content-desc: 返回, bounds: [0,66][132,198] >
< text: 新增朋友, resource-id: android:id/text1, class: android.widget.TextView, content-desc: , bounds: [167,98][367,165] >
< text: , resource-id: android:id/title, class: android.widget.TextView, content-desc: 分隔欄, bounds: [0,198][1080,253] >
< text: 微訊號/QQ號/手機號, resource-id: com.tencent.mm:id/can, class: android.widget.TextView, content-desc: , bounds: [39,286][1041,372] >
< text: 我的微訊號:xi_a_o_qiang, resource-id: android:id/title, class: android.widget.TextView, content-desc: , bounds: [268,403][740,489] >
< text: , resource-id: com.tencent.mm:id/cbl, class: android.widget.ImageView, content-desc: 我的二維碼, bounds: [740,394][811,498] >
< text: , resource-id: android:id/title, class: android.widget.TextView, content-desc: 分隔欄, bounds: [0,498][1080,553] >
< text: 雷達加朋友, resource-id: android:id/title, class: android.widget.TextView, content-desc: , bounds: [171,580][391,645] >
< text: 新增身邊的朋友, resource-id: android:id/summary, class: android.widget.TextView, content-desc: , bounds: [171,645][402,690] >
< text: 面對面建群, resource-id: android:id/title, class: android.widget.TextView, content-desc: , bounds: [171,756][391,821] >
< text: 與身邊的朋友進入同一個群聊, resource-id: android:id/summary, class: android.widget.TextView, content-desc: , bounds: [171,821][600,866] >
< text: 掃一掃, resource-id: android:id/title, class: android.widget.TextView, content-desc: , bounds: [171,932][303,997] >
< text: 掃描二維碼名片, resource-id: android:id/summary, class: android.widget.TextView, content-desc: , bounds: [171,997][402,1042] >
< text: 手機聯絡人, resource-id: android:id/title, class: android.widget.TextView, content-desc: , bounds: [171,1108][391,1173] >
< text: 新增或邀請通訊錄中的朋友, resource-id: android:id/summary, class: android.widget.TextView, content-desc: , bounds: [171,1173][567,1218] >
< text: 公眾號, resource-id: android:id/title, class: android.widget.TextView, content-desc: , bounds: [171,1284][303,1349] >
< text: 獲取更多資訊和服務, resource-id: android:id/summary, class: android.widget.TextView, content-desc: , bounds: [171,1349][468,1394] >

至此,大功告成。

值得注意的是生成xml的時間大約是 2-4秒,一定要給足夠了,否則還沒生成,你就去匯出肯定是失敗的。
如果就模擬個幾步每次都去生成,匯出,解析xml的話太費時了,但,我們可以這樣
首次加微信好友時,每次都這麼來一遍,費時就費時吧。
然後將每個步驟的座標都記錄下了儲存到本地ini,下次再來的時候直接讀座標即可。
什麼?你說換個手機勒,座標又對不上了。
–每個手機的型號都存一次不就行了,^_^,像下面這樣:
這裡寫圖片描述

5.效果圖

最後,也是最重要的,沒圖沒JB啊,好吧上圖
這裡寫圖片描述

6.demo原始碼下載