【快速使用】解决USB权限弹窗——无障碍

一、简介

  • 别人的设备与系统,自己无能力修改源码,只能寻求其他手段,在网上看到有人说道“无障碍”,便开始试一试,发现是可行的,比adb好用多了;

二、使用

  • 新建类 MyAccessibilityService
public class MyAccessibilityService extends AccessibilityService {
    
    
    String TAG = "AliAccessibilityService";

    /**
     * event: EventType: TYPE_WINDOW_STATE_CHANGED;
     * EventTime: 382223;
     * PackageName: com.android.systemui;
     * MovementGranularity: 0;
     * Action: 0;
     * ContentChangeTypes: [];
     * WindowChangeTypes: [] [
     * ClassName: android.app.Dialog;
     * Text: [YSY200506-1SW, 要允许YSY200506-1SW访问USB Serial Converter吗?, 连接USB Serial Converter后一律打开YSY200506-1SW, 取消, 确定];
     * ContentDescription: null;
     * ItemCount: -1;
     * CurrentItemIndex: -1;
     * Enabled: true;
     * Password: false;
     * Checked: false;
     * FullScreen: false;
     * Scrollable: false;
     * BeforeText: null;
     * FromIndex: -1;
     * ToIndex: -1;
     * ScrollX: -1;
     * ScrollY: -1;
     * MaxScrollX: -1;
     * MaxScrollY: -1;
     * AddedCount: -1;
     * RemovedCount: -1;
     * ParcelableData: null ];
     * recordCount: 0
     *
     * @param event AccessibilityEvent
     */

    //接收到系统发送AccessibilityEvent时的回调
    @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"/>

初始设置

  • 完成代码编写后,需要手动开启该应用的无障碍服务,可以代码跳转设置,也可以直接出厂设置好

注意事项

  • 程序一旦崩溃,无障碍服务便会失效,需要重启系统才能恢复
  • 程序一旦重新安装或升级安装,无障碍也会失效,需要重新手动开启

猜你喜欢

转载自blog.csdn.net/qq_36881363/article/details/107755932