Android - Get the current Activity script

start

As an Android developer, I think everyone has a big pain point when tracking bugs and debugging: The test took the mobile phone and said, there is a problem on this page, you are confused, this ghost knows which page it is, just think about it , adb command to sendadb shell ...

step

After a long time, you will find:
1. Open Terminal
2. Enter adb shell dumpsys activity | grep mFoc
3. Copy the result
4. Open Android studothe search class

Basically these 4 steps. In fact, the pain point is this long command, and it is not easy to remember. If you accidentally type it wrong, please check carefully where it is wrong.

analyze

For the basically fixed 4 steps, you can consider using a script tool for automatic acquisition and copying to the clipboard. At present, the mainstream script tool languages python​​are standard in Mac and Linux, and the installation of Windows is also super simple;

下面的脚本文件主要是简化了输入的难度,使用草鸡简单,直接执行命令:`python current_activity.py`,如果执行正常,当前页面的java类名直接赋值到了剪切板,可以直接粘贴使用;

use

python current_activity.py

source code

# coding:utf-8
# !/usr/bin/python
__author__ = "Egan"

"""
Mac 环境:
本脚本是通过 adb 获取当前的 Activity
"""
# FileName:current_activity.py

import commands
import subprocess


# 将数据copy到剪切板
def set_clipboard_data(data):
    p = subprocess.Popen(['pbcopy'], stdin=subprocess.PIPE)
    p.stdin.write(data)
    p.stdin.close()
    p.communicate()
    print('当前页面的名字已复制到剪切板')


if __name__ == "__main__":
    # 执行 adb 获取当前连接的虚拟机或者真机的 可见的页面.
    result = commands.getstatusoutput("adb shell dumpsys activity | grep 'mFoc'")
    # print(result)
    if result[0] == 0:
        # 对命令执行结果进行解析
        split_result = result[1].split("\n")[0].split("/")
        packages = split_result[0].split(" ")

        # 获取包名以及当前页面的名字
        current_activity_path = packages[len(packages) - 1] + split_result[1].split(" ")[0]
        print current_activity_path
        current_activity_path_list = current_activity_path.split('.')

        activity = current_activity_path_list[len(current_activity_path_list) - 1]
        # print activity
        # 对数据进行处理
        activity = activity.strip().replace('\r\n', ' ').replace('\n', ' ')
        # 复制到剪切板
        set_clipboard_data(bytes(activity))
    else:
        print("错误信息:"+result[1])
        print("根据错误信息,请检查:\n1、是否配置了 ADB 环境!!!\n2、是否有手机、模拟器连接!!!")

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324728624&siteId=291194637