问题
Background
Suppose you have an app that has an accessibility service, and the user has enabled it after you've used the correct intent :
startActivity(new Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS));
You can also check if the user has enabled it or not, using this code.
The problem
Sometimes, even if the user has enabled it, because it calls many times to onAccessibilityEvent, waking the app, it would be nice to be able to disable it temporarily.
What I've tried
I tried to disable the service as a component, but it works only after a few seconds for some reason:
private boolean isComponentEnabled() {
final int componentEnabledSetting = getPackageManager().getComponentEnabledSetting(new ComponentName(this, ....class));
return componentEnabledSetting == PackageManager.COMPONENT_ENABLED_STATE_ENABLED || componentEnabledSetting == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
}
public void setEnabled(boolean enabled) {
ComponentName component = new ComponentName(this, ....class);
getPackageManager().setComponentEnabledSetting(component,
enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
}
Also, re-enabling the service doesn't do anything except changing its state to be enabled, meaning no calls to "onAccessibilityEvent" even though it's enabled.
The question
Is it possible to enable&disable this service programmatically, to avoid un-needed calls to "onAccessibilityEvent", and avoid performance issues from it?
来源:https://stackoverflow.com/questions/41785005/is-it-possible-to-enable-disable-an-accessibility-service-after-the-user-has-ena