当前位置:网站首页>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
边栏推荐
- Daily CISSP certification common mistakes (April 18, 2022)
- Seata handles distributed transactions
- SQL database syntax learning notes
- Teach you to quickly rename folder names in a few simple steps
- Daily CISSP certification common mistakes (April 13, 2022)
- Ionic 从创建到打包指令集顺序
- Const keyword, variable and function are decorated with const
- 使用 bitnami/postgresql-repmgr 镜像快速设置 PostgreSQL HA
- STM32: LCD显示
- Serial port debugging tools cutecom and minicom
猜你喜欢
ctfshow-web361(SSTI)
Use bitnami / PostgreSQL repmgr image to quickly set up PostgreSQL ha
Kettle paoding jieniu Chapter 17 text file output
机器学习理论之(7):核函数 Kernels —— 一种帮助 SVM 实现非线性化决策边界的方式
ctfshow-web362(SSTI)
Practice of Druid SQL and security in meituan review
logstash 7. There is a time problem in X. the difference between @ timestamp and local time is 8 hours
Machine learning theory (7): kernel function kernels -- a way to help SVM realize nonlinear decision boundary
With the use of qchart, the final UI interface can be realized. The control of qweight can be added and promoted to a user-defined class. Only the class needs to be promoted to realize the coordinate
QT add external font ttf
随机推荐
Daily network security certification test questions (April 12, 2022)
Use of regular expressions in QT
Daily CISSP certification common mistakes (April 15, 2022)
22年字节跳动飞书人力套件三面面经
QT tablewidget insert qcombobox drop-down box
使用晨曦记账本,分析某个时间段每个账户收支结余
Cutting permission of logrotate file
listener. log
Multifunctional toolbox wechat applet source code
Use stm32cube MX / stm32cube ide to generate FatFs code and operate SPI flash
22 year flying Book manpower Kit
About the operation of unit file reading (I)
机器学习理论之(8):模型集成 Ensemble Learning
解决:cnpm : 无法加载文件 ...\cnpm.ps1,因为在此系统上禁止运行脚本
ESP32 LVGL8. 1 - anim animation (anim 16)
Implementation of TCP UDP communication with golang language
[popular science] CRC verification (I) what is CRC verification?
QT add external font ttf
数据库上机实验四(数据完整性与存储过程)
STM32: LCD显示