Python控制Android手机 python编程控制Android手机操作技巧示例

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Python控制Android手机 python编程控制Android手机操作技巧示例

初遇我ㄖ寸の热情呢?   2021-10-08 我要评论
想了解python编程控制Android手机操作技巧示例的相关内容吗,初遇我ㄖ寸の热情呢?在本文为您仔细讲解Python控制Android手机的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Python编程,Python控制Android手机,下面大家一起来学习吧。

在这里插入图片描述

几天前我在考虑使用 python 从 whatsapp 发送消息。和你们一样,我开始潜伏在互联网上寻找一些解决方案并找到了关于twilio. 一开始,是一个不错的解决方案,但它不是免费的,我必须购买一个 twilio 电话号码。此外,我无法在互联网上找到任何可用的 whatsapp API。所以我放弃了使用 twilio 和任何其他 whatsapp API 的想法。在想了很多之,打开 android studio,我连接了我的手机,然后开始了这个过程。当应用程序构建时,我想到了使用手机本身自动发送 whatsapp 消息的想法。我搜索了一些差不多的东西,发现了一些很有可能解决我的问题的东西。我找到了一个命令行工具,adb它可以帮助人们在不接触手机的情况下控制手机。

你应该拥有的东西

  • 对 Python 的基本理解
  • 一些空闲时间阅读此博客

安装

首先,转到此链接并在您的系统中下载 adb。
解压文件夹并将 adb 放入环境变量中。下面是在环境变量中添加 adb 的完整过程,

在这里插入图片描述

在您的手机中启用 USB 调试,并使用 USB 电缆将您的手机与 PC 连接。

通过打开 cmd 并键入,检查连接是否正确adb devices。您将在连接的设备列表中看到一个设备。

如果您可以看到您的设备,那么您可以打开任何代码编辑器。我正在使用 Visual Studio 代码。

开始

让我们首先导入一些我们需要的依赖项。您可以使用pip.

import cv2
import subprocess

我们将需要子进程通过命令行调用 adb 并获取输出,我们需要 cv2 进行一些图像处理,以便 python 能够点击屏幕或任何其他任务。
现在让我们在下面创建一个名为 adb 的基本函数,

def adb(command):
    proc = subprocess.Popen(command.split(' '), stdout=subprocess.PIPE, shell=True)
    (out, _) = proc.communicate()
    return out.decode('utf-8')

上面的函数基本上是通过子进程调用 adb 并检索我们将需要的输出。

轻敲

现在让我们编写代码,其中 python 将单击移动设备的屏幕。所以我们将创建一个名为 tap 的函数,它会点击屏幕上的特定位置。

def tap(tap_x, tap_y):
    adb("adb shell input tap {} {}".format(tap_x, tap_y))
tap(100,100)

这将单击距 x 100 像素和距 y 100 像素。现在您一定在想,为每个命令硬编码坐标是非常困难的,并且当设备改变时它不会工作,这就是为什么在本博客的下一节中我们将使用图像处理来检测坐标自动地。

截图

def take_screenshot(final):
    adb(f"adb exec-out screencap -p > ./images/{final}.png")

代码很简单。我们制作了一个功能,可以保存手机内部图像目录的屏幕截图。在函数中,我们可以传递图像文件的名称。

高级点击

现在,我们将使用目标图像来自动检测坐标,而不是传递坐标。为了更好地理解这一点,让我们举个例子,我有这个屏幕 ,我想打开我们中间的应用程序,然后将使用一个称为. 通过这个过程,我们将截取屏幕截图 > 使用模板匹配计算我们中间图标的坐标 > 点击那里

在这里插入图片描述

TemplateMatching

ef image_position(small_image, big_image):
    img_rgb = cv2.imread(big_image)
    img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
    template = cv2.imread(small_image, 0)
    height, width = template.shape[::]
    res = cv2.matchTemplate(img_gray, template, cv2.TM_SQDIFF)
    _, _, top_left, _ = cv2.minMaxLoc(res)
    bottom_right = (top_left[0] + width, top_left[1] + height)
    return (top_left[0]+bottom_right[0])//2, (top_left[1]+bottom_right[1])//2

screen="screen"
take_screenshot(screen)
x, y  = image_position("images/among_us_icon.png", f"images/{screen}")
click(x,y)
# WOWWW Python successfully opened among us app.

有了上面的代码,即使你在手机屏幕上改变了我们游戏的位置,python仍然可以打开游戏。

我们还能做什么?
你可以用 adb 和 python 做更多的事情。让我们谈谈其中的一些。

滑动

def swipe(start_x, start_y, end_x, end_y, duration_ms):
    adb("adb shell input swipe {} {} {} {} {}".format(start_x, start_y, end_x, end_y, duration_ms))

打电话给某人

def call(number):
    adb(f"adb shell am start -a android.intent.action.CALL -d tel:{number}")
call('+91xxxxxxxxxx') # +[CODE][NUMBER]

从手机下载文件到电脑

在这里插入图片描述

def download(path, output_path):
    adb(f"adb pull {path} {output_path}")
从手机中删除文件
def remove(path):
    adb(f"adb shell rm {path}") #/sdcard/...

手机录屏

# name is the video_file name and time is the seconds you want to record
def screen_record(name, time):
    adb(f"adb shell screenrecord /sdcard/{name} --time-limit {time}")
    download(f"/sdcard/{name}",f"./mobile/{name}")
    remove(f"/sdcard/{name}")

打开手机

def switch_phone_on_off():
    adb("adb shell input keyevent 26")

还有更多类似 26 的关键事件。如果您想知道,请访问此链接。

打开网址

def open_url(url):
    adb(f'adb shell am start -a android.intent.action.VIEW -d {url}')
open_url("https://www.google.co.in/")

发送 Whatsapp 消息

好的,所以我觉得这很酷。在获得了所有这些基本的理解之后,我们已经解决了我的主要问题,即发送没有二维码的 whatsapp 消息,没有像 twilio 这样的付费方法。这有点棘手,但它在我的手机上工作。我希望它也适用于你的。

def send_whatsapp_message(phone, message):
    adb(f'adb shell am start -a android.intent.action.VIEW -d "https://api.whatsapp.com/send?phone={phone}"') # Opening whatsapp url
    adb('ping 127.0.0.1 -n 2 > nul') # delay
    adb(f'adb shell input text "{message}"')  # entering message
    adb('adb shell keyevent 22') # Right arrow
    adb('adb shell keyevent 22') # Right arrow
    adb('adb shell input keyevent 22') # Right arrow 
    adb('adb shell input keyevent 22') # Right arrow
    adb('adb shell input keyevent 66') # Enter Key

send_whatsapp_message('+91xxxxxxxxxx', 'blah blah blah')

消息已发送!

在这里插入图片描述

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们