Is it possible to enable/disable an accessibility service after the user has enabled it?

隐身守侯 提交于 2019-12-12 03:34:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!