Code to enable accessibility services

Code to enable accessibility services

The defined AccessibilityService is disabled by default and needs to be manually enabled or automatically enabled on the accessibility interface.
There are currently two ways to automatically start the service. It is necessary to modify the source code or configure the application as a system application.

1. Modify
frameworks\base\packages\SettingsProvider\src\com\android\providers\settings\DatabaseHelper.java in the source code
and add the following code in the loadSecureSettings function:
loadSetting(stmt, Settings.Secure.ACCESSIBILITY_ENABLED, 1);
loadSetting(stmt , Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,"your defined service");
The format of "your defined service": com.125la.main/com.125la.service.MyAccessibilityService

2. System application and compile in the system
Declare permission requirements in AndroidManiferst.xml

<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />

Add at the APP startup entry:

Settings.Secure.putString(getContentResolver(),Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,"你定义的服务");
Settings.Secure.putInt(getContentResolver(),Settings.Secure.ACCESSIBILITY_ENABLED, 1);

The format to fill in the "service you define": pkgname/classname

1 means on, 0 means off

Turn off accessibility code

Settings.Secure.putString(getContentResolver(),Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,"");
Settings.Secure.putString(getContentResolver(),Settings.Secure.ACCESSIBILITY_ENABLED, 0);

3. Summary:
I have tried two methods, and the second method has some minor problems, such as the inability to monitor after burning the system for the first time and after restoring the factory settings, but it is possible in other cases.

Refer to the article
android to view self-starting services, AccessibilityService self-starting
codes to enable accessibility services-system applications

Guess you like

Origin blog.csdn.net/fromVillageCoolBoy/article/details/119890070
Recommended