stopSelf() vs stopSelf(int) vs stopService(Intent)

后端 未结 7 1238
鱼传尺愫
鱼传尺愫 2020-12-29 00:56

What\'s the difference in calling
stopSelf() , stopSelf(int) or stopService(new Intent(this,MyServiceClass.class))
inside

7条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-29 01:52

    The core functionality of all 3 methods are same and that's why they are having similar names. There are very small differences among them.

    • public final void stopSelf()

    Class : This belongs to android.app.Service class

    Called From : This method is intended to get called from inside the service only.

    Behavior : Service will get stopped. onDestroy() state called.

    • public final void stopSelf(int startId)

    Class : This belongs to android.app.Service class

    Called From : This method is intended to get called from inside the service only.

    Behavior : There is a method from older version stopSelfResult(int startId). This method is newer version of that method. Service will get stopped only if the most recent time it was started was startId. onDestroy() state called.

    May be you can find this method helpful only in a case where you started 2-3 instance of same service but you want to stop them in a order you received with startId list stored with you. Need to be very careful while using this method.

    • public abstract boolean stopService(Intent service)

    Class : This belongs to android.content.Context class

    Called From : This method is intended to get called from outside the service though you are allowed to call from inside.

    Behavior : If the service is not running, nothing happens. Otherwise it is stopped. Note that calls to startService() are not counted -- this stops the service no matter how many times it was started. onDestroy() state called.

提交回复
热议问题