How to stop my service?

前端 未结 3 1553
挽巷
挽巷 2021-01-15 22:40

Please also see Edit 2 (below)

I have extended NotificationListenerService and implemented several of its methods including the onDestroy()

相关标签:
3条回答
  • 2021-01-15 22: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;
    }
    
    0 讨论(0)
  • Place command

    stopSelf();
    

    to

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

    Make some variable for checking if service should be stopped.

    0 讨论(0)
  • 2021-01-15 23:15

    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.

    0 讨论(0)
提交回复
热议问题