当前位置:网站首页>The corresponding permissions required to automatically open the app in the setting interface through accessibility service

The corresponding permissions required to automatically open the app in the setting interface through accessibility service

2022-04-23 18:41:00 tanleicsdn

Recently, I saw a project that needs to allow access to applications, for example 、 And screen recording and other permissions When the mobile phone is running, click to jump to the barrier free interface and click to open the barrier free service , Then turn to the base note. , Then the phone automatically turns on step by step App Authority required , It looks very cow and intelligent sub son . Learned about it and found that through AccessibilityService This class can fulfill the above requirements . However, the setting interface of each mobile phone is different , Therefore, we need to make different models to judge the appropriate models . My job is in Huawei Honor 9i(Android 9.0 System ) To complete the function of automatically setting permissions .

About AccessibilityService Class, I won't explain too much , Direct roll up code

1. In the list file application Add :

    <service
        android:name="com.hxf.childapp.AutoDebugService"
        android:enabled="true"
        android:exported="true"
        android:label=" Your bag name "
        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/autodebug"/>
    </service>

2. Configure the package name and behavior to be monitored , That is to say @xml/autodebug file : stay xml Create in folder autodebug file .

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:accessibilityEventTypes="typeAllMask|typeWindowStateChanged|typeWindowContentChanged"
    android:accessibilityFeedbackType="feedbackGeneric"
    android:accessibilityFlags="flagReportViewIds"
    android:canPerformGestures="true"
    android:notificationTimeout="100"
    android:canRetrieveWindowContent="true"
    android:packageNames="com.android.settings" />

Here's to say android:packageNames The value in is the application that needs to be monitored .

3. establish AutoDebugService Class inherits from AccessibilityService class :

public class AutoDebugService extends AccessibilityService {
	
}

4. Rewriting methods :

@Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
    
    }

The core method that needs to be rewritten All events are monitored from this method , For example, the interface changes , Click on , Long press and other events .

5. Judge whether to listen through events :

if (AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED == event.getEventType() || AccessibilityEvent.TYPE_VIEW_CLICKED == event.getEventType()) {

}

Here I filter through interface changes and click events , The logic is written in this callback method ,

Here's the whole code , For reference only , The need for mobile phones is different .

public class AutoDebugService extends AccessibilityService {
    int index = 0;

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        if (AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED == event.getEventType() || AccessibilityEvent.TYPE_VIEW_CLICKED == event.getEventType()) {
            if (step == 0) {
                try {
                    Thread.sleep(700);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED == event.getEventType()) {
                    event("com.android.settings:id/dashboard_container", " account ");
                    Log.e("lee", step + "*******");
                }
            } else if (step == 1) {
                try {
                    Thread.sleep(700);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED == event.getEventType()) {
                    Log.e("lee", step + "*******");
                    back();
                }
            } else if (step == 2) {
                if (AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED == event.getEventType()) {
                    back();
                }
            } else if (step == 3) {
                Log.e("lee", step + "*******");
                try {
                    Thread.sleep(700);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED == event.getEventType()) {
                    event("com.android.settings:id/dashboard_container", " Security and privacy ");
                }
            } else if (step == 4) {
                Log.e("lee", step + "*******");
                try {
                    Thread.sleep(700);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                back();
            } else if (step == 5) {
                try {
                    Thread.sleep(700);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                back();
            } else if (step == 6) {
                Log.e("lee", step + "*******");
                try {
                    Thread.sleep(700);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                event("com.android.settings:id/dashboard_container", " Desktop and wallpaper ");
            } else if (step == 7) {
                Log.e("lee", step + "*******");
                try {
                    Thread.sleep(700);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                event("com.android.settings:id/list", " Desktop style ");
            } else if (step == 8) {
                Log.e("lee", step + "*******");
                try {
                    Thread.sleep(700);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                event("com.android.settings:id/normal_l_ui_sector", " Standard style ");
            } else if (step == 9) {
                Log.e("lee", step + "*******");
                try {
                    Thread.sleep(700);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                back();
            } else if (step == 10) {
                Log.e("lee", step + "*******");
                try {
                    Thread.sleep(700);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                back();
            } else if (step == 11) {
                try {
                    Thread.sleep(700);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                back();
            } else if (step == 12) {
                Log.e("lee", step + "*******");
                try {
                    Thread.sleep(700);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                event("com.android.settings:id/dashboard_container", " application ");
            } else if (step == 13) {
                Log.e("lee", step + "*******");
                try {
                    Thread.sleep(700);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                event("com.android.settings:id/list", " Application separation ");
            } else if (step == 14) {
                Log.e("lee", step + "*******");
                try {
                    Thread.sleep(700);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                check("com.android.settings:id/clone_app_switche");
            } else if (step == 15) {
                Log.e("lee", step + "*******");
                try {
                    Thread.sleep(700);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                back();
            } else if (step == 16) {
                Log.e("lee", step + "*******");
                try {
                    Thread.sleep(700);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                back();
            } else if (step == 17) {
                try {
                    Thread.sleep(700);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                back();
            } else if (step == 18) {
                Log.e("lee", step + "*******");
                try {
                    Thread.sleep(700);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                event("com.android.settings:id/dashboard_container", " application ");
            } else if (step == 19) {
                Log.e("lee", step + "*******");
                try {
                    Thread.sleep(700);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                event("com.android.settings:id/list", " Application startup management ");
            } else if (step == 20) {
                Log.e("lee", step + "*******");
                try {
                    Thread.sleep(700);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                check("com.huawei.systemmanager:id/app_control_switch");
            } else if (step == 21) {
                Log.e("lee", step + "*******");
                try {
                    Thread.sleep(700);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                back();
            } else if (step == 22) {
                Log.e("lee", step + "*******");
                try {
                    Thread.sleep(700);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                back();
            } else if (step == 23) {
                try {
                    Thread.sleep(700);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                back();
            } else if (step == 24) {
                Log.e("lee", step + "*******");
                try {
                    Thread.sleep(700);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                event("com.android.settings:id/dashboard_container", " System ");
            } else if (step == 25) {
                Log.e("lee", step + "*******");
                try {
                    Thread.sleep(700);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                event("com.android.settings:id/list", " Developer options ");
            } else if (step == 26) {
                Log.e("lee", step + "*******");
                try {
                    Thread.sleep(700);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                event("com.android.settings:id/list", "USB  debugging ");
            } else if (step == 27) {
                Log.e("lee", step + "*******");
                try {
                    Thread.sleep(700);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                back();
            } else if (step == 28) {
                Log.e("lee", step + "*******");
                try {
                    Thread.sleep(700);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                back();
            } else if (step == 29) {
                Log.e("lee", step + "*******");
                try {
                    Thread.sleep(700);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                back();
            }
        }
    }

    /**
     *  adopt Id obtain AccessibilityNodeInfo
     *
     * @param id
     * @return
     */
    private List<AccessibilityNodeInfo> findNodesById(String id) {
        AccessibilityNodeInfo root = getRootInActiveWindow();
        if (root != null) {
            return root.findAccessibilityNodeInfosByViewId(id);
        }
        return null;
    }

    /**
     *  adopt Text obtain AccessibilityNodeInfo
     *
     * @param text
     * @return
     */
    private List<AccessibilityNodeInfo> findNodesByText(String text) {
        AccessibilityNodeInfo root = getRootInActiveWindow();
        if (root != null) {
            return root.findAccessibilityNodeInfosByText(text);
        }
        return null;
    }

    /**
     *  Handling events 
     *
     * @param id
     * @param text
     */
    private void event(String id, String text) {
        List<AccessibilityNodeInfo> byIdList = findNodesById(id);
        List<AccessibilityNodeInfo> byTextList = findNodesByText(text);
        if (byTextList != null && byTextList.size() > 0) {
            AccessibilityNodeInfo info = byTextList.get(0);
            if (!info.isChecked()) {
                info.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
                step++;
            } else {
                byTextList.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
                step++;
            }
        } else {
            if (byIdList != null && byIdList.size() > 0) {
                byIdList.get(0).performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
            }
        }
    }

    private void check(String id) {
        List<AccessibilityNodeInfo> byIdList = findNodesById(id);
        if (byIdList != null && byIdList.size() > 0) {
            for (int i = 0; i < byIdList.size(); i++) {
                if (!byIdList.get(i).isChecked()) {
                    byIdList.get(i).performAction(AccessibilityNodeInfo.ACTION_CLICK);
                }
            }
        }
        step++;
    }


    /**
     *  Return to the previous level 
     */
    private void back() {
        performGlobalAction(GLOBAL_ACTION_BACK);
        step++;
    }
}

Method called , First determine whether the accessibility service is turned on :
If it is opened, jump to the setting interface for step-by-step setting

if (isAccessibilitySettingsOn(AutoDebugService.class.getName())) {// Accessibility is turned on 
                Intent intent = new Intent(Settings.ACTION_SETTINGS);
                startActivity(intent);
            }

If you don't open it, open it :

 Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
startActivityForResult(intent, REQUEST_ACCESSIBLE_PERMISSION);

The above completes the function of automatically setting permissions . If you don't know the of the view id have access to sdk Self contained DDMS Tool view .

版权声明
本文为[tanleicsdn]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210604297429.html