{"id":17361,"date":"2023-12-18T11:16:35","date_gmt":"2023-12-18T03:16:35","guid":{"rendered":"https:\/\/www.zhidianwl.com\/zhidian\/?p=17361"},"modified":"2023-12-18T11:16:35","modified_gmt":"2023-12-18T03:16:35","slug":"python%e5%b0%81%e8%a3%85%e5%ae%89%e5%8d%93%e6%89%8b%e6%9c%baapp%e6%93%8d%e4%bd%9c%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/www.zhidianwl.com\/zhidian\/2023\/17361\/","title":{"rendered":"python\u5c01\u88c5\u5b89\u5353\u624b\u673aapp\u64cd\u4f5c\u6307\u5357"},"content":{"rendered":"

Python\u4e0e\u5b89\u5353\u624b\u673a\u7684\u8fde\u63a5\u5e38\u5e38\u4f7f\u7528Android Debug Bridge(ADB)\u65b9\u5f0f\uff0c\u53d1\u6325ADB\u7684\u8c03\u8bd5\u529f\u80fd\uff0c\u901a\u8fc7Python\u5bf9\u5b89\u5353\u8fdb\u884c\u64cd\u4f5c\u5c01\u88c5\u7684\u65b9\u5f0f\u79f0\u4e3a\u201cPython\u5b89\u5353\u81ea\u52a8\u5316\u201d\uff0c\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u4e00\u4e2a\u76ee\u6807\u662f\u201c\u83b7\u53d6\u5b89\u5353\u7cfb\u7edf\u4e2dAPP\u540d\u4e3a\u2018\u5fae\u4fe1\u2019\u7684\u6240\u6709Item\u201d\uff0c\u8981\u5b8c\u6210\u8fd9\u4e2a\u76ee\u6807\u9996\u5148\u8981\u4e86\u89e3\u5b89\u5353\u7684\u5e03\u5c40\u548c\u5bf9\u8c61, \u5b89\u5353UI\u5e03\u5c40\u4f7f\u7528\u7684\u662fXML\u63cf\u8ff0\u6587\u4ef6, \u4e00\u822c\u5b58\u653e\u5728 `\/res\/layout` \u548c `\/res\/transition` \u76ee\u5f55\u4e0b\u3002<\/p>\n

\u5b89\u5353UI\u4e2d\u7684\u7ec4\u4ef6\uff0c\u6bd4\u5982Button\u3001TextView\u3001EditText\u7b49\u7b49\u5f62\u6210\u4e00\u4e2a\u53ef\u89c1\u7a97\u53e3\uff0c \u6bcf\u4e2a\u7a97\u53e3\u90fd\u6709ID\u6807\u8bc6\uff0c\u7528\u6237\u5c31\u662f\u901a\u8fc7\u8fd9\u4e2aID\u6765\u64cd\u4f5c\u5b89\u5353\u7cfb\u7edf\u4e2d\u7684\u7ec4\u4ef6\u3002\u5176\u4e2d\uff0c\u6bcf\u4e00\u4e2acomponent\u90fd\u7531\u4e0d\u540c\u7684class\u4ee3\u8868\uff0c\u5e76\u5e94\u7ee7\u627f\u81f3`View`\u57fa\u7c7b\uff0c\u6bd4\u5982TextView\u7684\u7c7b\u540d\u4e3a`android.widget.TextView`\uff0cButton\u7684\u7c7b\u540d\u4e3a`android.widget.Button`\u3002<\/p>\n

Python\u5c01\u88c5\u5b89\u5353\u624b<\/p>\n

<\/figure>\n<\/p>\n

\u673aapp\u4e3b\u8981\u6b65\u9aa4\u5982\u4e0b:<\/p>\n

1. \u5b89\u88c5adb\uff0c \u786e\u8ba4\u624b\u673a\u8fde\u5b89\u5353app\u5f00\u53d1\u5de5\u5177<\/a>\u63a5<\/p>\n

2. \u786e\u8ba4\u624b\u673a\u4e2d\u5df2\u7ecf\u5b89\u88c5\u597dadb shell\u5305\u542b\u7684\u5404\u79cd\u5de5\u5177<\/p>\n

3. \u5b89\u88c5python\u5e76\u5b89\u88c5adb-python\u5305<\/p>\n

4. \u719f\u6089adb\u547d\u4ee4\uff0c\u901a\u8fc7Python\u6267\u884cadb\u547d\u4ee4\u5b9e\u73b0\u64cd\u4f5c<\/p>\n

\u57fa\u4e8e\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u53ef\u4ee5\u7f16\u5199Python\u4ee3\u7801\u6765\u64cd\u4f5c\u5b89\u5353\u624b\u673a\uff0c\u4f8b\u5982\uff1a<\/p>\n

“`python<\/p>\n

from adb.client import Client as AdbClient<\/p>\n

client = AdbClient(host=”127.0.0.1″, port=5037)<\/p>\n

device = client.device(“emulator-5554”)<\/p>\n

results = device.shell(“dumpsys activity | grep mFocusedActivity”)<\/p>\n

“`<\/p>\n

\u5176\u4e2d\uff0c`adb.client`\u63d0\u4f9b\u4e86\u8bbf\u95eeAndroid\u8bbe\u5907\u7684Python\u5e93\uff0c`AdbClient`\u8d1f\u8d23\u8fde\u63a5Android\u8bbe\u5907\uff0c`device`\u8d1f\u8d23\u4ee3\u8868\u8bbe\u5907\uff0c`shell()`\u65b9\u6cd5\u6267\u884cAn\u5b89\u5353app<\/a>droid\u8bbe\u5907\u7684\u547d\u4ee4\uff0c`grep mFocusedActivity`\u7528\u4e8e\u8fc7\u6ee4\u5230\u5f53\u524d\u83b7\u5f97\u7126\u70b9\u7684Activity\u4fe1\u606f\u3002<\/p>\n

\u603b\u4e4b\uff0cPython\u5c01\u88c5\u5b89\u5353\u624b\u673aapp\u662f\u4e00\u9879\u8f83\u4e3a\u5e38\u7528\u7684\u6280\u80fd\uff0c\u638c\u63e1\u4e4b\u540e\u53ef\u4ee5\u4e3a\u81ea\u5df1\u5f00\u53d1\u548c\u8c03\u8bd5\u5b89\u5353\u8f6f\u4ef6\u63d0\u4f9b\u4fbf\u5229\uff0c\u4ee5\u53ca\u4e3a\u7528\u6237\u63d0\u4f9b\u66f4\u52a0\u9ad8\u6548\u7075\u6d3b\u7684\u4f7f\u7528\u4f53\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

Python\u4e0e\u5b89\u5353\u624b\u673a\u7684\u8fde\u63a5\u5e38\u5e38\u4f7f\u7528Android Debug Bridge(ADB)\u65b9\u5f0f\uff0c\u53d1\u6325ADB\u7684\u8c03\u8bd5\u529f\u80fd\uff0c\u901a\u8fc7Python\u5bf9\u5b89\u5353\u8fdb\u884c\u64cd\u4f5c\u5c01\u88c5\u7684\u65b9\u5f0f\u79f0\u4e3a\u201cPython\u5b89\u5353\u81ea\u52a8\u5316\u201d\uff0c\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u4e00<\/p>\n","protected":false},"author":14,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[13071,23613,77,1638,331],"class_list":{"0":"post-17361","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-zuoapp","7":"tag-app","9":"tag-77","10":"tag-1638","11":"tag-331"},"_links":{"self":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/17361","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/comments?post=17361"}],"version-history":[{"count":1,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/17361\/revisions"}],"predecessor-version":[{"id":17387,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/17361\/revisions\/17387"}],"wp:attachment":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/media?parent=17361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/categories?post=17361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/tags?post=17361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}