Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to control mobile phone APP

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to use Python to manipulate mobile phone APP. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Recently, I often see projects that operate mobile phone APP with Python, such as Douyin, Xianyu and so on. After reading these projects, I find that all of these projects require the deployment of ADB environment. As for what ADB is, many great gods have said that it is written professionally, but I am still in the clouds after waiting for the rookie to read it.

In order to enable people without Android development experience to quickly learn to use Python to operate mobile phone APP, I wrote this article from the aspects of Android SDK deployment, ADB basic applications, Python operating mobile phones, etc., in order to make people have an intuitive understanding of the use of ADB, so that it will be easier to play with these projects mentioned above.

Notice in advance: there may be many ways to implement the various operations covered in this article, and I will only mention one of them, and the approach I have taken is probably not the best one.

1.Android SDK download and deployment

Speaking of Android SDK tools, there are a lot of SDK Tools, SDK, Platform-tools and so on. Here I recommend SDK Tools. If you enter the https://www.androiddevtools.cn/ website, you can see the interface in the following picture and choose your own corresponding system version (zip version) to download.

Unzip the downloaded file, run the SDK Manager.exe file, and enter the Packages installation management interface. For novice friends, I suggest using the default option directly, and then click the Install XX packages button in the lower right corner. The entire download and installation process will probably take dozens of minutes, and the specific time is related to the personal network speed.

The next step is to configure the environment variables. Add the platform-tools and tools folders in the android-sdk directory to the system variables. This step is a regular operation, and I won't demonstrate it any more.

After performing the above steps, even if the ADB environment is configured, all you need to do is to enable the developer options for the phone, enable USB debugging, and connect the phone to the computer with the usb cable.

Let's take the crucial step of testing whether the phone is connected successfully through ADB. Enter the system terminal and enter the adb devices directive. If the result in the following figure appears, it means that the environment has been successfully deployed, and the device number of the connection is preceded by the device.

If the connection test is not successful, such as the device cannot be found or the device is not connected, you can try to shut down and start debugging again, restart the phone, restart the computer, plug and unplug the USB cable, and so on.

Some basic operations of 2.ADB

Although it is a basic operation, there are still a lot of ADB commands, and it is difficult for everyone to digest them for a while. So here I only list a few commonly used instructions to operate the mobile phone APP, all of which are some basic operations.

1)。 Show all package names

Adbshellpmlistpackages

This command lists the names of all app packages installed on the phone. If the suffix parameter-s is added, the names of all system applications are listed (below), while the suffix parameter-3 lists all third-party application names.

2)。 Show active programs

Adbshelldumpsys activity activities

This command can get the name of the running package in the current mobile phone system. For example, when I start the "game center" of Xiaomi phone in my phone, I will get the information in the following figure by running the above command:

The parameter after realActivity in the figure is the currently running Activity name. If you pay a little attention, you will find that this name is more detailed than the package name above. As for the usefulness of this name, we will mention it in a minute.

3)。 Start the application

Name of the application Activity to be launched by adbshellamstart-n

The Activity name used here is the parameter after the realActivity just obtained. When using this directive, be sure to use the Activity name obtained by the previous command to start APP, not the package name obtained by the adb shell pm list packages command. For example, if we want to start Xiaomi's game center, we can use the command:

Adbshellamstart-ncom.xiaomi.gamecenter/.ui.MainTabActivity

If you use the following command, you will not be able to start the application smoothly:

Adbshellamstart-ncom.xiaomi.gamecenter

4)。 Stop the application

Name of the application to be stopped by adb shell am force-stop

This instruction forces the specified application to stop, which is slightly different from the startup of the program. When stopping the application, use the package name obtained by the adb shell pm list packages command. Or take Xiaomi's game center as an example, stop the application and use the following instructions:

Adbshellamforce-stopcom.xiaomi.gamecenter

5)。 Click to specify a location

Adbshellinputtapxy

This command is easy to understand. Simulated clicking on a location on the phone's screen is the corresponding location coordinate. By default in ADB, the coordinates of the upper left corner and the lower right corner of the phone screen are (0J0) and (xmax,ymax) respectively.

One point is emphasized here: to use the analog click function, the phone must be set to allow debugging and modifying permissions or clicking simulation through USB. The following functions such as swiping the phone screen and entering text messages also need to be set up.

6)。 Slide the mobile phone screen

Adbshellinputswipe x_start y_start x_end y_end

The essence of this instruction is to make the point with coordinates (x_start, y_start) move to the coordinate position (x_end, y_end), so as to achieve the effect of screen sliding.

7)。 Enter text information

Characters that need to be entered for adbshellinputtext

There is nothing to say about this instruction, just select the corresponding position, and then enter the corresponding characters.

8)。 Analog mobile phone button

AdbshellinputkeyeventX

In addition to regular keyboard characters, most mobile phones now have some special keys, such as home keys, volume + / -, and confirmation keys that sometimes appear in soft keyboards.

Where X corresponds to the number of special keys, here are a few commonly used examples (there are many other special keys):

3 corresponds to the home key

24 corresponds to volume +

25 corresponds to the volume-

66 corresponds to the confirmation key

9)。 Get element location

Careful friends may now find that some of the commands mentioned above about simulated clicks and simulated input are related to the location of mobile phone elements, so how do we determine the specific coordinate location and element information in practice? Here I propose to use the uiautomatorviewer.bat tool, which will be installed with SDK Tools, in the tools directory.

Take a look at the following picture, after the phone is connected successfully, click the green icon in the upper left corner to get the current page information of the phone, and then you can click on any location or element on the left side of the screen, and the relevant information will be displayed on the right screen.

Whether it feels very similar to using chrome to analyze web pages, with this information, it is not a problem to use the above instructions to operate the phone.

3.Python controls mobile phone App

1)。 Command line control

I talked about some basic operations of ADB operating mobile phone, but the theory is too boring. Now let's do a small experiment to see the effect.

Let's take Xiaomi's game center as an example and use ADB to complete the following tasks in turn:

Switch to the "online games" tab, instruction: adb shell input tap 400263

Select the search bar, instruction: adb shell input tap 776139

Enter "wangzherongyao", instruction: adb shell input text wangzherongyao

Click the confirmation button, instruction: adb shell input keyevent 66

Enter Arena of Valor game page, instruction: adb shell input tap 600500

2). Python script control

From the above demonstration video, using ADB to operate the mobile phone APP is quite successful, but if you enter instructions one by one as just now, it may not be as convenient as directly operating the phone, so we need to use Python scripts to make it batch and automatic.

Python can execute terminal instructions with the help of the os.system () function. Let's look at the following code:

The function of the above code is basically the same as what we demonstrated before, mainly adding an operation to open the game center and simplifying some intermediate steps.

After reading the above, do you have any further understanding of how to use Python to manipulate mobile phone APP? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report