因为要做自动化测试,需要对已发布的包进行回归手测,这个时候需要手动安装APK,但是红米会弹出继续安装的按钮,手点一次比较烦,想自动点"继续安装"按钮!
感谢先行者们的分享
本文参考:
http://blog.csdn.net/itfootball/article/details/21953763
https://testerhome.com/topics/3800
http://developer.android.com/reference/android/accessibilityservice/AccessibilityService.html
0. 干货区
如果你想知道一下什么是AccessibilityService,可自行搜索学习或看官方介绍 http://developer.android.com/reference/android/accessibilityservice/AccessibilityService.html
开启方法:
普通手机: 设置 -> 无障碍/辅助功能 -> 服务 -> AutoInstall -> 开启 -> 确定
某些手机:设置 -> 其它高级设置 -> 辅助功能 -> 服务 -> AutoInstall -> 开启 -> 确定
注意:
开启自动安装不仅适用于adb install,也适用于主动点击apk来启动安装。所以有安全风险,建议仅在测试机器上安装
git地址 https://github.com/guhan121/autoinstall.git
1. 代码区
不需要Activity,仅需要一个继承AccessibilityService的服务,在服务里兼听onAccessibilityEvent,当出现安装界面的时候,自动去点击。在安装完成后,到辅助功能里开启即可。
AutoInstallService.java
package com.mrqyoung.autoinstall; import android.accessibilityservice.AccessibilityService; import android.util.Log; import android.view.accessibility.AccessibilityEvent; import android.view.accessibility.AccessibilityNodeInfo; import java.util.List; public class AutoInstallService extends AccessibilityService { private static final String TAG = "AutoInstallService"; private static String PACKAGE_INSTALLER = "com.miui.securitycenter"; public AutoInstallService() { } @Override public void onAccessibilityEvent(AccessibilityEvent event) { //log(event.toString()); if (event.getSource() == null) { log("<null> event source"); return; } int eventType = event.getEventType(); if (eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { log("event Perform: " + event.getPackageName()); if( event.getPackageName().equals(PACKAGE_INSTALLER)) { boolean r = performInstallation(event); log("Action Perform: " + r); } } } @Override public void onInterrupt() { log("AutoInstallServiceInterrupted"); } private void log(String s) { Log.d(TAG, s); } private boolean performInstallation(AccessibilityEvent event) { List<AccessibilityNodeInfo> nodeInfoList; String[] labels = new String[]{"确定", "继续安装", "下一步", "完成"}; for (String label : labels) { nodeInfoList = event.getSource().findAccessibilityNodeInfosByText(label); if (nodeInfoList != null && !nodeInfoList.isEmpty()) { boolean performed = performClick(nodeInfoList); if (performed) return true; } } return false; } private boolean performClick(List<AccessibilityNodeInfo> nodeInfoList) { for (AccessibilityNodeInfo node : nodeInfoList) { if (node.isClickable() && node.isEnabled()) { return node.performAction(AccessibilityNodeInfo.ACTION_CLICK); } } return false; } }
AndroidManifest里面要声明权限,除了上面从代码里面可以过滤,通过meta-data的xml里也可直接配置过滤
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mrqyoung.autoinstall" > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@android:style/Theme.Black" > <service android:name=".AutoInstallService" android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"> <intent-filter> <action android:name="android.accessibilityservice.AccessibilityService" /> </intent-filter> <meta-data android:name="android.accessibilityservice" android:resource="@xml/accessibilityservice" /> </service> </application> </manifest>
在AndroidManifest里面引用的meta-data文件,样例。
@xml/accessibilityservice.xml
<?xml version="1.0" encoding="utf-8"?> <accessibility-service android:accessibilityEventTypes="typeWindowStateChanged" android:description="@string/description" android:accessibilityFeedbackType="feedbackVisual" android:notificationTimeout="1000" android:accessibilityFlags="flagDefault" android:canRetrieveWindowContent="true" xmlns:android="http://schemas.android.com/apk/res/android" />
<!-- 第3行等同于过滤 AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED -->
<!--
android:packageNames="com.android.packageinstaller" 无这个参数主要是开始也不知道那个包在管理安装
-->
gradle build 编译,使用的是2.2版本的gradle
adb uninstall com.mrqyoung.autoinstall 卸载这个应用adb install app-debug.apk 将这个应用直接从PC机安装到手机上,当然第一次你需要手动点"继续安装"