[Quick use] Solve the USB permission pop-up window-barrier-free

1. Introduction

  • Other people's equipment and systems have no ability to modify the source code and can only seek other means. I saw someone on the Internet saying "no barriers", so I started to give it a try and found that it is feasible and much easier to use than adb;

Two, use

  • New class 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;
    }
}
  • Declare service, and create xml under res, and then create accessibility_service file under xml. You can search for specific parameter descriptions, but I won’t repeat them here.
<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"/>

default setting

  • After the code is written, you need to manually turn on the accessibility service of the application, which can be set by code jump, or directly set at the factory

Precautions

  • Once the program crashes, the accessibility service will fail, and the system needs to be restarted to recover
  • Once the program is reinstalled or upgraded, the accessibility will also be invalid, and you need to restart it manually

Guess you like

Origin blog.csdn.net/qq_36881363/article/details/107755932