我想知道如何检测我自己的服务是否已启用。所以我可以检查我的服务是否未启用,然后告诉用户启用它。
问问题
23065 次
5 回答
70
以下是检查您的无障碍服务是否启用的方法。
注意:YOURAccessibilityService
使用您的服务 更改值。
// To check if service is enabled
private boolean isAccessibilitySettingsOn(Context mContext) {
int accessibilityEnabled = 0;
final String service = getPackageName() + "/" + YOURAccessibilityService.class.getCanonicalName();
try {
accessibilityEnabled = Settings.Secure.getInt(
mContext.getApplicationContext().getContentResolver(),
android.provider.Settings.Secure.ACCESSIBILITY_ENABLED);
Log.v(TAG, "accessibilityEnabled = " + accessibilityEnabled);
} catch (Settings.SettingNotFoundException e) {
Log.e(TAG, "Error finding setting, default accessibility to not found: "
+ e.getMessage());
}
TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');
if (accessibilityEnabled == 1) {
Log.v(TAG, "***ACCESSIBILITY IS ENABLED*** -----------------");
String settingValue = Settings.Secure.getString(
mContext.getApplicationContext().getContentResolver(),
Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
if (settingValue != null) {
mStringColonSplitter.setString(settingValue);
while (mStringColonSplitter.hasNext()) {
String accessibilityService = mStringColonSplitter.next();
Log.v(TAG, "-------------- > accessibilityService :: " + accessibilityService + " " + service);
if (accessibilityService.equalsIgnoreCase(service)) {
Log.v(TAG, "We've found the correct setting - accessibility is switched on!");
return true;
}
}
}
} else {
Log.v(TAG, "***ACCESSIBILITY IS DISABLED***");
}
return false;
}
并调用此方法:
if (!isAccessibilitySettingsOn(getApplicationContext())) {
startActivity(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS));
}
如果未启用,这将检查并启动可访问性设置。
于 2013-08-07T05:24:26.707 回答
9
这是Jakub Bláha在 java 中的答案的修改版本。
public boolean isAccessServiceEnabled(Context context, Class accessibilityServiceClass)
{
String prefString = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
return prefString!= null && prefString.contains(context.getPackageName() + "/" + accessibilityServiceClass.getName());
}
于 2019-07-10T12:22:13.257 回答
1
这在某种程度上是一个较小的版本,但它正在工作。
fun isAccessServiceEnabled(context: Context): Boolean {
val prefString =
Settings.Secure.getString(context.contentResolver, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES)
return prefString.contains("${context.packageName}/${context.packageName}.${context.getString(R.string.access_service_name)}")
}
如果有什么遗漏,请随时纠正我。
于 2019-06-14T14:27:10.033 回答
0
Kotlin 版本基于@Pankaj Kumar 的回答
inline fun <reified T> Context.isAccessibilityEnabled(): Boolean {
var enabled = 0
try {
enabled = Settings.Secure.getInt(contentResolver, Settings.Secure.ACCESSIBILITY_ENABLED)
} catch (e: SettingNotFoundException) {
Timber.e(e)
}
if (enabled == 1) {
val name = ComponentName(applicationContext, T::class.java)
val services = Settings.Secure.getString(contentResolver, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES)
return services?.contains(name.flattenToString()) ?: false
}
return false
}
于 2021-03-28T10:07:09.217 回答
0
1:基于 Kotlin 的答案 2:添加了当前包名称检查,因为仅当为当前包启用可访问性服务时,它才会返回 true,最近,如果启用了任何包可访问性服务,则返回 true
private fun checkAccessibilityPermission(): Boolean {
var isAccessibilityEnabled = false
(requireActivity().getSystemService(Context.ACCESSIBILITY_SERVICE) as AccessibilityManager).apply {
installedAccessibilityServiceList.forEach { installedService ->
installedService.resolveInfo.serviceInfo.apply {
if (getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_ALL_MASK).any { it.resolveInfo.serviceInfo.packageName == packageName && it.resolveInfo.serviceInfo.name == name && permission == Manifest.permission.BIND_ACCESSIBILITY_SERVICE && it.resolveInfo.serviceInfo.packageName == requireActivity().packageName })
isAccessibilityEnabled = true
}
}
}
if (isAccessibilityEnabled.not())
startActivity(Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK
})
return isAccessibilityEnabled
}
于 2021-11-01T13:54:42.773 回答