当前位置:网站首页>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
边栏推荐
- Golang 语言实现TCP UDP通信
- 数据库上机实验四(数据完整性与存储过程)
- ctfshow-web362(SSTI)
- ESP32 LVGL8. 1 - anim animation (anim 16)
- CISSP certified daily knowledge points (April 18, 2022)
- ctfshow-web361(SSTI)
- Daily CISSP certification common mistakes (April 11, 2022)
- CISSP certified daily knowledge points (April 15, 2022)
- Promote QT default control to custom control
- 昇腾 AI 开发者创享日全国巡回首站在西安成功举行
猜你喜欢
ctfshow-web361(SSTI)
STM32: LCD显示
Iptables - L executes slowly
ESP32 LVGL8. 1. Detailed migration tutorial of m5stack + lvgl + IDF (27)
Setting up keil environment of GD single chip microcomputer
Use Chenxi bookkeeping book to analyze the balance of revenue and expenditure of each account in a certain period of time
解决:cnpm : 无法加载文件 ...\cnpm.ps1,因为在此系统上禁止运行脚本
Machine learning practice - naive Bayes
ctfshow-web362(SSTI)
Druid SQL和Security在美团点评的实践
随机推荐
The connection of imx6 network port is unstable after power on
机器学习实战 -朴素贝叶斯
If condition judgment in shell language
listener.log
Seata处理分布式事务
C language simulates entering and leaving the stack, first in first out, first in first out, shared memory
Quantexa CDI(场景决策智能)Syneo平台介绍
Iptables - L executes slowly
ctfshow-web362(SSTI)
Use stm32cube MX / stm32cube ide to generate FatFs code and operate SPI flash
ctfshow-web361(SSTI)
Ionic 从创建到打包指令集顺序
程序员如何快速开发高质量的代码?
ESP32 LVGL8. 1 - label (style 14)
Promote QT default control to custom control
ESP32 LVGL8. 1 - textarea text area (textarea 26)
ESP32 LVGL8. 1 - calendar (calendar 25)
ESP32 LVGL8. 1 - msgbox message box (msgbox 28)
Chondroitin sulfate in vitreous
函数递归以及趣味问题的解决