背景
我的应用程序拥有一个AccessibilityService
。Android似乎一直保持 AccessibilityService
运行,但我只需要在某些时候运行该服务。(具体来说,如果用户打开我的应用并关闭它,则不再需要使用 AccessibilityService
)。
如果我的 AccessibilityService
没有必要运行时仍在运行,则会浪费设备的RAM和CPU,我希望尽可能避免这样对我的用户造成不必要的麻烦。
问题
如何以编程方式启动和停止我的 AccessibilityService
?
请注意,我不是在询问如何在Android的辅助功能设置中以编程方式启用或禁用 AccessibilityService
。这只是关于在已启用服务时如何启动和停止服务的问题。
我尝试过什么(停止 AccessibilityService
)
Disabling the
AccessibilityService
component using the following:PackageManager.setComponentEnabledSetting( accessibilityServiceName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0 );
This killed the entire application, including the
AccessibilityService
. Also, after rebooting the phone, theAccessibilityService
was no longer visible in the Android accessibility settings. (It seems that the component needs to be re-enabled followed by restarting the phone in order to get it to show up again.)Disabling the
AccessibilityService
component using the following:PackageManager.setComponentEnabledSetting( accessibilityServiceName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP );
This didn't stop the
AccessibilityService
.Stopping the
AccessibilityService
usingContext.stopService()
andService.stopSelf()
. These didn't stop the service, presumably because the OS is bound to it.Killing the
AccessibilityService
's process usingProcess.killProcess()
. However, the service was soon after restarted and reconnected, presumably because the OS is bound to it.Unregistering for accessibility events using
AccessibilityService.setServiceInfo(new AccessibilityServiceInfo())
. This stops the service from getting accessibility events, but the service still runs in the background.
相关问题
-
这个问题仅涉及如何停止服务。我想在停止后能够重新启动该服务。