前言
使用无障碍服务需要是系统应用才能有权限,否则无权限无效,直接上代码。
1、创建继承系统的AccessibilityService自定义服务
public class AccessibilityService extends android.accessibilityservice.AccessibilityService {
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
}
@Override
public void onInterrupt() {
}
}
2、清单文件中需要加入以下权限和service定义
<!-- 无障碍权限 -->
<uses-permission android:name="android.permission.BIND_ACCESSIBILITY_SERVICE" />
<!-- 无障碍定义清单 -->
<service
android:name="com.rishun.systemsetting.service.AccessibilityService"
android:enabled="true"
android:exported="true"
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_services" />
</service>
3、在资源目录下的xml目录中创建accessibility_services.xml文件,可以根据
accessibilityEventTypes:响应的事件类型(单击、长按、滑动、通知等)accessibilityFeedbackType:回显给用户的方式(例如:配置TTS引擎,实现发音)accessibilityFlags:很关键,你的应用程序需要获取哪些信息
description:描述(会在开启辅助功能页面看到这段文字)
notificationTimeout:响应时间间隔100
packageNames:需要辅助的app包名,不写表示所有app
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFeedbackType="feedbackGeneric"
android:accessibilityFlags="flagRequestFilterKeyEvents"
android:canRetrieveWindowContent="true"
android:canRequestFilterKeyEvents="true"
android:description="@string/app_name"
tools:ignore="MissingDefaultResource" />
4、最后步可以在继承Application类中调用,这个方法是打开无障碍服务,这样可以在自己实现的AccessibilityService类中监听系统按键事件、或者其他应用界面信息和状态等
/**
* 开启无障碍服务
*/
public void openAccessibilityService(Context context){
//是否已开启无障碍服务
boolean isStartAccessibilityServiceEnable = false;
AccessibilityManager accessibilityManager = (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
if (accessibilityManager != null) {
List<AccessibilityServiceInfo> accessibilityServices = accessibilityManager.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_ALL_MASK);
for (AccessibilityServiceInfo info : accessibilityServices) {
if (info.getId().contains(context.getPackageName())) {
isStartAccessibilityServiceEnable = true;
Log.i("AccessibilityService", "已开启无障碍服务.");
break;
}
}
}
if (!isStartAccessibilityServiceEnable) {
String enabledServices = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
ComponentName selfComponentName = new ComponentName(context.getPackageName(), AccessibilityService.class.getCanonicalName());
String flattenToStr = selfComponentName.flattenToString();
if (enabledServices == null || !enabledServices.contains(flattenToStr)) {
enabledServices += ":" + flattenToStr;
}
//开启服务
isStartAccessibilityServiceEnable = Settings.Secure.putString(context.getContentResolver(),Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, enabledServices);
Settings.Secure.putInt(context.getContentResolver(),Settings.Secure.ACCESSIBILITY_ENABLED, 1);
Log.i("AccessibilityService", "开启无障碍服务,enable:"+isStartAccessibilityServiceEnable);
}
}