重新启动无障碍服务崩溃后 [英] Restart Accessibility Service After Crash
问题描述
在我的应用程序有一个服务
和 AccessibilityService
。现在服务
由辅助服务,以接收信息的约束。
In my Application there is a Service
and an AccessibilityService
. Now the Service
is bound by the accessibility Service in order to receive information.
我的服务
定期检查,如果 AccessibilityService
已启用,并且如果它不是,它发送一个通知它具有用户启用它。
My Service
periodically checks if the AccessibilityService
is enabled, and if it is not, it sends a notification to the user that it has to enable it.
启用一次, AccessibilityService
开始工作。首先,它绑定到我的服务
,之后,它开始发送信息。
Once enabled, AccessibilityService
starts to work. First it binds to my Service
, and after, it begins to send information.
问题是如果 AccessibilityService
崩溃。它仍然启用,但没有运行实例。所以,我的服务
发现它启用,但实际上 AccessibilityService
没有运行。
The problem is if AccessibilityService
crashes. It remains enabled but there is no running instance. So my Service
finds it enabled but actually the AccessibilityService
is not running.
如何检查 AccessibilityService
How Check AccessibilityService
public boolean checkAccesibilityService()
{
int accessibilityEnabled = 0;
boolean accessibilityFound = false;
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());
}
if (accessibilityEnabled == 1) {
//Log.v(TAG, "***ACCESSIBILIY IS ENABLED*** -----------------");
String settingValue = Settings.Secure.getString(
mContext.getApplicationContext().getContentResolver(),
Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
if (settingValue != null) {
if(TextUtils.isEmpty(settingValue)) {
Log.i(TAG, "Nessun servizio abilitato!");
return false;
}
TextUtils.SimpleStringSplitter splitter = new TextUtils.SimpleStringSplitter(':');
splitter.setString(settingValue);
while (splitter.hasNext()) {
String accessabilityService = splitter.next();
//Log.v(TAG, "-------------- > accessabilityService :: " + accessabilityService);
if (accessabilityService.equalsIgnoreCase(ACCESSIBILITY_SERVCE)) {
//Log.v(TAG, "Accessibility Service Attivato!");
return true;
}
}
Log.v(TAG, "Accesibility Service non abilitato!");
return false;
}
} else {
Log.v(TAG, "***ACCESSIBILIY IS DISABLED***");
}
return accessibilityFound;
}
Q1:为什么当 AccessibilityService
崩溃做的是Android OS没有重新启动
Q1: Why when the AccessibilityService
crashes does the Android OS not restart it?
Q2:我可以改变我的code,以检查 AccessibilityService
已启用,但也有一个正在运行的实例。
Q2: Can I change my code to check if the AccessibilityService
is enabled but also has a running instance?
推荐答案
我不知道,但我怀疑没有什么可以做这件事。我已经与 AccessibilityService
同样的情形的问题,所以我觉得这是很正常的行为。我能想到的最好的是:
Why when the AccessibilityService
crashes does the Android OS not restart it?
I don't know, but I suspect there isn't much you can do about this. I've had similar problems with AccessibilityService
s, so I think this is normal behaviour. The best I can think of is:
- 仔细写你的
AccessibilityService
code和彻底的测试,以便它不包含会导致崩溃的bug。 - 请不要做你的
AccessibilityService
的任何逻辑,你也可以做到在不同的地方。少工作,你的AccessibilityService
是干什么的,它是简单的,所以不太可能是有错误。 - 请您
AccessibilityService
code容错。 predict错误会发生在服务中,并写入code赶上他们,并从中恢复,使AccessibilityService
继续运行正常,如果可能的。 - 在自己的进程中运行你的
AccessibilityService
所以它不会杀死你,当其他code崩溃。请注意,这可能取决于服务如何与您的code的其余部分进行交互是困难的。
- Carefully write your
AccessibilityService
code and thoroughly test it so that it doesn't contain any bugs that will cause it to crash. - Don't do any logic in your
AccessibilityService
that you could also do in a different place. The less work yourAccessibilityService
is doing, the simpler it is, so the less likely it is to have bugs. - Make your
AccessibilityService
code error-tolerant. Predict errors that will happen inside the service, and write code to catch them and recover from them so that theAccessibilityService
continues running normally if possible. - Run your
AccessibilityService
in its own process so it isn't killed when your other code crashes. Note that this might be difficult depending on how the service interacts with the rest of your code.
是的;有几个常见的技术来做到这一点。见如何检查服务是否运行在Android上? 。
Yes; there are a couple of common techniques to do this. See How to check if a service is running on Android?.
这篇关于重新启动无障碍服务崩溃后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!