How to stop my service?

前提是你 提交于 2019-12-01 12:35:53

Try overrides onStartCommand() in your Service with START_NOT_STICKY flag:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    return START_NOT_STICKY;
}

Create a new service that does all of the work for you, instead of doing the work in this service that extends NotificationListenerService. All you do then is use the startService() method once you want to do some work and presumably you will send some extras with that intent containing the posted notification. It is then very easy to stop that service (that does the work for you and doesn't extend NotificationListenerService) using the stopSelf() method. Stopping the NotificationListenerService itself using the stopSelf() method doesn't work.

Place command

stopSelf();

to

public int onStartCommand(Intent intent, int flags, int startId) {

Make some variable for checking if service should be stopped.

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