日常设备信息操作,自定义设备信息判断
<p><span style="font-weight: bold;">自用分享,日常设备信息判断。</span></p><p>个人建议:一些固定的设备信息获取后最好是存储于文件,再进行后续读取操作,减少内存的损耗,正常脚本执行很少频繁的更换设备。</p><p>代码可自行修改测试:</p><blockquote><pre style="font-size: 12pt; font-family: "JetBrains Mono", monospace; color: rgb(188, 190, 196);"><span style="font-size: 12pt; color: rgb(207, 142, 109);">from </span><span style="font-size: 12pt;">ascript.android.system </span><span style="font-size: 12pt; color: rgb(207, 142, 109);">import </span><span style="font-size: 12pt;">Device</span><br></pre><pre style="font-size: 12pt; font-family: "JetBrains Mono", monospace; color: rgb(188, 190, 196);"><br><br><span style="color: rgb(207, 142, 109);">class </span>CompareDevice:<br> <span style="color: rgb(207, 142, 109);">def </span><span style="color: rgb(178, 0, 178);">__init__</span>(<span style="color: rgb(148, 85, 141);">self</span>):<br> <span style="color: rgb(148, 85, 141);">self</span>.device = Device()<br> <span style="color: rgb(148, 85, 141);">self</span>.id = <span style="color: rgb(148, 85, 141);">self</span>.device.id()<br> <span style="color: rgb(148, 85, 141);">self</span>.display = <span style="color: rgb(148, 85, 141);">self</span>.device.display()<br> <span style="color: rgb(148, 85, 141);">self</span>.name = <span style="color: rgb(148, 85, 141);">self</span>.device.name()<br> <span style="color: rgb(148, 85, 141);">self</span>.version = <span style="color: rgb(148, 85, 141);">self</span>.device.version()<br> <span style="color: rgb(148, 85, 141);">self</span>.model = <span style="color: rgb(148, 85, 141);">self</span>.device.model()<br> <span style="color: rgb(148, 85, 141);">self</span>.sdk = <span style="color: rgb(148, 85, 141);">self</span>.device.sdk()<br> <span style="color: rgb(148, 85, 141);">self</span>.ip = <span style="color: rgb(148, 85, 141);">self</span>.device.ip()<br> <span style="color: rgb(148, 85, 141);">self</span>.current_appinfo = <span style="color: rgb(148, 85, 141);">self</span>.device.current_appinfo()<br> <span style="color: rgb(148, 85, 141);">self</span>.memory = <span style="color: rgb(148, 85, 141);">self</span>.device.memory()<br> <span style="color: rgb(136, 136, 198);">print</span>(<span style="color: rgb(148, 85, 141);">self</span>.memory)<br> <span style="color: rgb(122, 126, 133);"># print(self.memory, self.id, self.display, self.name, self.version, self.model, self.sdk, self.ip,<br></span><span style="color: rgb(122, 126, 133);"> # self.current_appinfo)<br></span><span style="color: rgb(122, 126, 133);"><br></span><span style="color: rgb(122, 126, 133);"> </span><span style="color: rgb(207, 142, 109);">def </span><span style="color: rgb(86, 168, 245);">memory_remaining_compare</span>(<span style="color: rgb(148, 85, 141);">self</span>, remaining: <span style="color: rgb(136, 136, 198);">int</span>) -> <span style="color: rgb(136, 136, 198);">bool</span>:<br> <span style="color: rgb(95, 130, 107); font-style: italic;">"""<br></span><span style="color: rgb(95, 130, 107); font-style: italic;"> </span><span style="color: rgb(103, 163, 124); font-style: italic;">:param</span><span style="color: rgb(95, 130, 107); font-style: italic;"> remaining: </span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">内存容量,</span><span style="color: rgb(95, 130, 107); font-style: italic;">int</span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">类型,内存容量单位字节<br></span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;"> </span><span style="color: rgb(103, 163, 124); font-style: italic;">:return</span><span style="color: rgb(95, 130, 107); font-style: italic;">: </span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">判断剩余内存是否小于设置的内存容量</span><span style="color: rgb(95, 130, 107); font-style: italic;"><br></span><span style="color: rgb(95, 130, 107); font-style: italic;"> """<br></span><span style="color: rgb(95, 130, 107); font-style: italic;"> </span><span style="color: rgb(207, 142, 109);">if </span>remaining > <span style="color: rgb(148, 85, 141);">self</span>.memory[<span style="color: rgb(42, 172, 184);">0</span>]:<br> <span style="color: rgb(207, 142, 109);">return True<br></span><span style="color: rgb(207, 142, 109);"> return False<br></span><span style="color: rgb(207, 142, 109);"><br></span><span style="color: rgb(207, 142, 109);"> def </span><span style="color: rgb(86, 168, 245);">memory_used_compare</span>(<span style="color: rgb(148, 85, 141);">self</span>, used: <span style="color: rgb(136, 136, 198);">int</span>) -> <span style="color: rgb(136, 136, 198);">bool</span>:<br> <span style="color: rgb(95, 130, 107); font-style: italic;">"""<br></span><span style="color: rgb(95, 130, 107); font-style: italic;"> </span><span style="color: rgb(103, 163, 124); font-style: italic;">:param</span><span style="color: rgb(95, 130, 107); font-style: italic;"> used: </span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">内存容量,</span><span style="color: rgb(95, 130, 107); font-style: italic;">int</span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">类型,内存容量单位字节<br></span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;"> </span><span style="color: rgb(103, 163, 124); font-style: italic;">:return</span><span style="color: rgb(95, 130, 107); font-style: italic;">: </span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">判断已用内存是否小于设置的内存容量</span><span style="color: rgb(95, 130, 107); font-style: italic;"><br></span><span style="color: rgb(95, 130, 107); font-style: italic;"> """<br></span><span style="color: rgb(95, 130, 107); font-style: italic;"> </span><span style="color: rgb(207, 142, 109);">if </span>used > <span style="color: rgb(148, 85, 141);">self</span>.memory[<span style="color: rgb(42, 172, 184);">1</span>]:<br> <span style="color: rgb(207, 142, 109);">return True<br></span><span style="color: rgb(207, 142, 109);"> return False<br></span><span style="color: rgb(207, 142, 109);"><br></span><span style="color: rgb(207, 142, 109);"> def </span><span style="color: rgb(86, 168, 245);">memory_all_compare</span>(<span style="color: rgb(148, 85, 141);">self</span>, in_all: <span style="color: rgb(136, 136, 198);">int</span>) -> <span style="color: rgb(136, 136, 198);">bool</span>:<br> <span style="color: rgb(95, 130, 107); font-style: italic;">"""<br></span><span style="color: rgb(95, 130, 107); font-style: italic;"> </span><span style="color: rgb(103, 163, 124); font-style: italic;">:param</span><span style="color: rgb(95, 130, 107); font-style: italic;"> in_all: </span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">内存容量,</span><span style="color: rgb(95, 130, 107); font-style: italic;">int</span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">类型,内存容量单位字节<br></span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;"> </span><span style="color: rgb(103, 163, 124); font-style: italic;">:return</span><span style="color: rgb(95, 130, 107); font-style: italic;">: </span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">判断总共内存是否小于设置的内存容量</span><span style="color: rgb(95, 130, 107); font-style: italic;"><br></span><span style="color: rgb(95, 130, 107); font-style: italic;"> """<br></span><span style="color: rgb(95, 130, 107); font-style: italic;"> </span><span style="color: rgb(207, 142, 109);">if </span>in_all > <span style="color: rgb(148, 85, 141);">self</span>.memory[<span style="color: rgb(42, 172, 184);">2</span>]:<br> <span style="color: rgb(207, 142, 109);">return True<br></span><span style="color: rgb(207, 142, 109);"> return False<br></span><span style="color: rgb(207, 142, 109);"><br></span><span style="color: rgb(207, 142, 109);"> def </span><span style="color: rgb(86, 168, 245);">app_name_compare</span>(<span style="color: rgb(148, 85, 141);">self</span>, name: <span style="color: rgb(136, 136, 198);">str</span>) -> <span style="color: rgb(136, 136, 198);">bool</span>:<br> <span style="color: rgb(95, 130, 107); font-style: italic;">"""<br></span><span style="color: rgb(95, 130, 107); font-style: italic;"> </span><span style="color: rgb(103, 163, 124); font-style: italic;">:param</span><span style="color: rgb(95, 130, 107); font-style: italic;"> name: app</span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">名称,</span><span style="color: rgb(95, 130, 107); font-style: italic;">str</span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">类型<br></span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;"> </span><span style="color: rgb(103, 163, 124); font-style: italic;">:return</span><span style="color: rgb(95, 130, 107); font-style: italic;">: </span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">判断</span><span style="color: rgb(95, 130, 107); font-style: italic;">app</span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">名称是否相同</span><span style="color: rgb(95, 130, 107); font-style: italic;"><br></span><span style="color: rgb(95, 130, 107); font-style: italic;"> """<br></span><span style="color: rgb(95, 130, 107); font-style: italic;"> </span><span style="color: rgb(207, 142, 109);">if </span><span style="color: rgb(148, 85, 141);">self</span>.current_appinfo.name == name:<br> <span style="color: rgb(207, 142, 109);">return True<br></span><span style="color: rgb(207, 142, 109);"> return False<br></span><span style="color: rgb(207, 142, 109);"><br></span><span style="color: rgb(207, 142, 109);"> def </span><span style="color: rgb(86, 168, 245);">app_packageName_compare</span>(<span style="color: rgb(148, 85, 141);">self</span>, packagename: <span style="color: rgb(136, 136, 198);">str</span>) -> <span style="color: rgb(136, 136, 198);">bool</span>:<br> <span style="color: rgb(95, 130, 107); font-style: italic;">"""<br></span><span style="color: rgb(95, 130, 107); font-style: italic;"> </span><span style="color: rgb(103, 163, 124); font-style: italic;">:param</span><span style="color: rgb(95, 130, 107); font-style: italic;"> packagename: app</span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">包名,</span><span style="color: rgb(95, 130, 107); font-style: italic;">str</span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">类型<br></span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;"> </span><span style="color: rgb(103, 163, 124); font-style: italic;">:return</span><span style="color: rgb(95, 130, 107); font-style: italic;">: </span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">判断</span><span style="color: rgb(95, 130, 107); font-style: italic;">app</span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">包名是否相同</span><span style="color: rgb(95, 130, 107); font-style: italic;"><br></span><span style="color: rgb(95, 130, 107); font-style: italic;"> """<br></span><span style="color: rgb(95, 130, 107); font-style: italic;"> </span><span style="color: rgb(207, 142, 109);">if </span><span style="color: rgb(148, 85, 141);">self</span>.current_appinfo.packageName == packagename:<br> <span style="color: rgb(207, 142, 109);">return True<br></span><span style="color: rgb(207, 142, 109);"> return False<br></span><span style="color: rgb(207, 142, 109);"><br></span><span style="color: rgb(207, 142, 109);"> def </span><span style="color: rgb(86, 168, 245);">app_activity_compare</span>(<span style="color: rgb(148, 85, 141);">self</span>, activity: <span style="color: rgb(136, 136, 198);">str</span>) -> <span style="color: rgb(136, 136, 198);">bool</span>:<br> <span style="color: rgb(95, 130, 107); font-style: italic;">"""<br></span><span style="color: rgb(95, 130, 107); font-style: italic;"> </span><span style="color: rgb(103, 163, 124); font-style: italic;">:param</span><span style="color: rgb(95, 130, 107); font-style: italic;"> activity: app</span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">的</span><span style="color: rgb(95, 130, 107); font-style: italic;">activity<br></span><span style="color: rgb(95, 130, 107); font-style: italic;"> </span><span style="color: rgb(103, 163, 124); font-style: italic;">:return</span><span style="color: rgb(95, 130, 107); font-style: italic;">: </span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">判断</span><span style="color: rgb(95, 130, 107); font-style: italic;">app</span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">的</span><span style="color: rgb(95, 130, 107); font-style: italic;">activity</span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">是否相同</span><span style="color: rgb(95, 130, 107); font-style: italic;"><br></span><span style="color: rgb(95, 130, 107); font-style: italic;"> """<br></span><span style="color: rgb(95, 130, 107); font-style: italic;"> </span><span style="color: rgb(207, 142, 109);">if </span><span style="color: rgb(148, 85, 141);">self</span>.current_appinfo.activity == activity:<br> <span style="color: rgb(207, 142, 109);">return True<br></span><span style="color: rgb(207, 142, 109);"> return False<br></span><span style="color: rgb(207, 142, 109);"><br></span><span style="color: rgb(207, 142, 109);"> def </span><span style="color: rgb(86, 168, 245);">id_compare</span>(<span style="color: rgb(148, 85, 141);">self</span>, id_: <span style="color: rgb(136, 136, 198);">str</span>) -> <span style="color: rgb(136, 136, 198);">bool</span>:<br> <span style="color: rgb(95, 130, 107); font-style: italic;">"""<br></span><span style="color: rgb(95, 130, 107); font-style: italic;"> </span><span style="color: rgb(103, 163, 124); font-style: italic;">:param</span><span style="color: rgb(95, 130, 107); font-style: italic;"> id_: </span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">判断的</span><span style="color: rgb(95, 130, 107); font-style: italic;">id</span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">,</span><span style="color: rgb(95, 130, 107); font-style: italic;">str</span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">类型<br></span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;"> </span><span style="color: rgb(103, 163, 124); font-style: italic;">:return</span><span style="color: rgb(95, 130, 107); font-style: italic;">: </span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">判断</span><span style="color: rgb(95, 130, 107); font-style: italic;">id</span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">是否正确返回</span><span style="color: rgb(95, 130, 107); font-style: italic;">True</span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">,错误返回</span><span style="color: rgb(95, 130, 107); font-style: italic;">False<br></span><span style="color: rgb(95, 130, 107); font-style: italic;"> """<br></span><span style="color: rgb(95, 130, 107); font-style: italic;"> </span><span style="color: rgb(207, 142, 109);">if </span><span style="color: rgb(148, 85, 141);">self</span>.id == id_:<br> <span style="color: rgb(207, 142, 109);">return True<br></span><span style="color: rgb(207, 142, 109);"> return False<br></span><span style="color: rgb(207, 142, 109);"><br></span><span style="color: rgb(207, 142, 109);"> def </span><span style="color: rgb(86, 168, 245);">width_compare</span>(<span style="color: rgb(148, 85, 141);">self</span>, width: <span style="color: rgb(136, 136, 198);">int</span>, mum: <span style="color: rgb(136, 136, 198);">int </span>= <span style="color: rgb(42, 172, 184);">0</span>) -> <span style="color: rgb(136, 136, 198);">bool</span>:<br> <span style="color: rgb(95, 130, 107); font-style: italic;">"""<br></span><span style="color: rgb(95, 130, 107); font-style: italic;"> </span><span style="color: rgb(103, 163, 124); font-style: italic;">:param</span><span style="color: rgb(95, 130, 107); font-style: italic;"> mum: </span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">最小屏幕宽度,</span><span style="color: rgb(95, 130, 107); font-style: italic;">int</span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">类型,默认</span><span style="color: rgb(95, 130, 107); font-style: italic;">0<br></span><span style="color: rgb(95, 130, 107); font-style: italic;"> </span><span style="color: rgb(103, 163, 124); font-style: italic;">:param</span><span style="color: rgb(95, 130, 107); font-style: italic;"> width: </span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">屏幕宽度,</span><span style="color: rgb(95, 130, 107); font-style: italic;">int</span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">类型<br></span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;"> </span><span style="color: rgb(103, 163, 124); font-style: italic;">:return</span><span style="color: rgb(95, 130, 107); font-style: italic;">: </span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">判断</span><span style="color: rgb(95, 130, 107); font-style: italic;">width</span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">值是否在屏幕宽度范围内</span><span style="color: rgb(95, 130, 107); font-style: italic;"><br></span><span style="color: rgb(95, 130, 107); font-style: italic;"> """<br></span><span style="color: rgb(95, 130, 107); font-style: italic;"> </span><span style="color: rgb(207, 142, 109);">if </span>mum < width < <span style="color: rgb(136, 136, 198);">int</span>(<span style="color: rgb(148, 85, 141);">self</span>.display.widthPixels):<br> <span style="color: rgb(207, 142, 109);">return True<br></span><span style="color: rgb(207, 142, 109);"> return False<br></span><span style="color: rgb(207, 142, 109);"><br></span><span style="color: rgb(207, 142, 109);"> def </span><span style="color: rgb(86, 168, 245);">height_compare</span>(<span style="color: rgb(148, 85, 141);">self</span>, height: <span style="color: rgb(136, 136, 198);">int</span>, mum: <span style="color: rgb(136, 136, 198);">int </span>= <span style="color: rgb(42, 172, 184);">0</span>) -> <span style="color: rgb(136, 136, 198);">bool</span>:<br> <span style="color: rgb(95, 130, 107); font-style: italic;">"""<br></span><span style="color: rgb(95, 130, 107); font-style: italic;"> </span><span style="color: rgb(103, 163, 124); font-style: italic;">:param</span><span style="color: rgb(95, 130, 107); font-style: italic;"> mum: </span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">最小屏幕高度,</span><span style="color: rgb(95, 130, 107); font-style: italic;">int</span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">类型,默认</span><span style="color: rgb(95, 130, 107); font-style: italic;">0<br></span><span style="color: rgb(95, 130, 107); font-style: italic;"> </span><span style="color: rgb(103, 163, 124); font-style: italic;">:param</span><span style="color: rgb(95, 130, 107); font-style: italic;"> height: </span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">屏幕高度,</span><span style="color: rgb(95, 130, 107); font-style: italic;">int</span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">类型<br></span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;"> </span><span style="color: rgb(103, 163, 124); font-style: italic;">:return</span><span style="color: rgb(95, 130, 107); font-style: italic;">: </span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">判断</span><span style="color: rgb(95, 130, 107); font-style: italic;">height</span><span style="color: rgb(95, 130, 107); font-style: italic; font-family: 宋体, monospace;">值是否在屏幕宽度范围内</span><span style="color: rgb(95, 130, 107); font-style: italic;"><br></span><span style="color: rgb(95, 130, 107); font-style: italic;"> """<br></span><span style="color: rgb(95, 130, 107); font-style: italic;"> </span><span style="color: rgb(207, 142, 109);">if </span>mum < height < <span style="color: rgb(136, 136, 198);">int</span>(<span style="color: rgb(148, 85, 141);">self</span>.display.heightPixels):<br> <span style="color: rgb(207, 142, 109);">return True<br></span><span style="color: rgb(207, 142, 109);"> return False<br></span><span style="color: rgb(207, 142, 109);"><br></span><span style="color: rgb(207, 142, 109);"><br></span><span style="color: rgb(136, 136, 198);">print</span>(CompareDevice().app_activity_compare(<span style="color: rgb(106, 171, 115);">'com.aojoy.airscript/.activity.MaActivity2'</span>))</pre></blockquote>
收藏(0)
分享
相关标签:
注意:本文归作者所有,未经作者允许,不得转载