What exactly does Stopself () do? How can I stop the service?

I am new to android and don't know much about java ...

I have an Activity that creates an ALARM MANAGER and fires alarmManager.setRepeating() , pointing to a Service.

This service in onCreate sets up a partial wakelock to execute all code execution (the application is designed to interact with the user, so the phone is always in standby mode).

All this work is perfect.

Sometimes a service does not have to execute all the code, but exit it before execution. Therefore, I used StopSelf , but I have some doubts:

  • onDestroy The function in Service has code to remove wakelock. Does this StopSelf() ?

  • In any case, if I do not specify any function to end the service, after the code is executed, when does it end?

So, in my case, is it better to replace onDestroy() instead of StopSelf() ? a lot thanx!

+4
source share
3 answers

You must call stopSelf () to stop the service. After you name it, the Android Framework will call the onDestroy() method automatically.

In fact, these onXXX () methods (the "on" prefix imply that these methods are callbacks for the system) should be called by the system, not by the developers. Read more in the SDK.

+9
source

stopSelf() is called before Thread completes.
Thread ends when stopSelf() called.

Service is still destroyed at the stopSelf() call point.

+2
source

To answer your other question, the service is terminated only when stopSelf () or Context.stopService () is called.

Link: Go to Service Lifecycle at: http://developer.android.com/reference/android/app/Service.html

0
source

Source: https://habr.com/ru/post/1384054/


All Articles