【问题标题】:Manually switch notificationListenerService on and off手动打开和关闭 notificationListenerService
【发布时间】:2015-02-05 16:33:29
【问题描述】:
您将如何使用按钮手动打开和关闭 notificationListenerService?
在安全->通知中允许应用程序的那一刻服务启动,但是我不确定如何防止这种情况并改为手动执行。
我知道有诸如 startService() 之类的函数,但不确定如何针对服务实现。
谢谢
【问题讨论】:
标签:
java
android
android-intent
service
android-service
【解决方案1】:
您可以使用PackageManager.setComponentEnabledSetting() 以编程方式启用或禁用服务:
PackageManager packageManager = getPackageManager();
ComponentName notificationListenerService = new ComponentName(this,
YourNotificationListenerService.class);
// The new state of the service - either enabled or disabled
int newState = enableService
? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
: PackageManager.COMPONENT_ENABLED_STATE_DISABLE;
packageManager.setComponentEnabledSetting(
notificationListenerService,
newState,
PackageManager.DONT_KILL_APP);