一、简介
- 别人的设备与系统,自己无能力修改源码,只能寻求其他手段,在网上看到有人说道“无障碍”,便开始试一试,发现是可行的,比adb好用多了;
二、使用
- 新建类 MyAccessibilityService
public class MyAccessibilityService extends AccessibilityService {
String TAG = "AliAccessibilityService";
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
if (event.getPackageName().equals("com.android.systemui") && event.getClassName().equals("android.app.Dialog") && event.getText().get(0).equals(getAppName(getApplicationContext()))) {
AccessibilityNodeInfo nodeInfo = event.getSource();
for (int i = 0; i < nodeInfo.getChildCount(); i++) {
if (nodeInfo.getChild(i).getClassName().equals("android.widget.CheckBox")) {
Log.d(TAG, "onAccessibilityEvent: " + nodeInfo.getChild(i).getClassName() + ": " + nodeInfo.getChild(i).getText());
nodeInfo.getChild(i).performAction(AccessibilityNodeInfo.ACTION_CLICK);
}
}
for (int i = 0; i < nodeInfo.getChildCount(); i++) {
if (nodeInfo.getChild(i).getClassName().equals("android.widget.ScrollView")) {
for (int j = 0; j < nodeInfo.getChild(i).getChildCount(); j++) {
if (nodeInfo.getChild(i).getChild(j).getClassName().equals("android.widget.Button") && nodeInfo.getChild(i).getChild(j).getText().equals("确定")) {
Log.d(TAG, "onAccessibilityEvent: " + nodeInfo.getChild(i).getChild(j).getClassName() + ": " + nodeInfo.getChild(i).getChild(j).getText());
nodeInfo.getChild(i).getChild(j).performAction(AccessibilityNodeInfo.ACTION_CLICK);
return;
}
}
}
}
}
}
@Override
public void onInterrupt() {
Log.d(TAG, "onInterrupt");
}
public static String getAppName(Context context) {
try {
PackageManager packageManager = context.getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(
context.getPackageName(), 0);
int labelRes = packageInfo.applicationInfo.labelRes;
return context.getResources().getString(labelRes);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
- 声明service,并在res下创建xml,然后再xml下创建accessibility_service文件,具体参数说明可以自行搜索,这里不过多赘述
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ysy200506_1sw">
... ...
<application
... ...
<service
android:name=".MyAccessibilityService"
android:label="USB读取自动授权"
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/accessibility_service" />
</service>
</application>
</manifest>
<accessibility-service
xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeWindowStateChanged"
android:accessibilityFeedbackType="feedbackGeneric"
android:canRetrieveWindowContent="true"
android:description="@string/app_name"
android:notificationTimeout="100"/>
初始设置
- 完成代码编写后,需要手动开启该应用的无障碍服务,可以代码跳转设置,也可以直接出厂设置好
注意事项
- 程序一旦崩溃,无障碍服务便会失效,需要重启系统才能恢复
- 程序一旦重新安装或升级安装,无障碍也会失效,需要重新手动开启