Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly do Stopself()? What's the best way to terminate a service running?

i'm a beginner in android and have not much experience on java...

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

This Service in onCreate set a partial wakelock to perform the entire execution of the code (the app is made for non user interaction an so the phone is always in standby).

All of this work perfect.

Sometimes the Service should not perform entire code, but exit before from execution. So i have used StopSelf but i have some doubts:

  • onDestroy function in Service has the code to delete the wakelock. Does StopSelf() call it?

  • Anyway if i don't specify any function to terminate the service, after code execution when is it terminated?

So in my case, is better replace onDestroy() instead of Stopself() ? many thanx!

like image 995
Lork Avatar asked Dec 01 '11 07:12

Lork


2 Answers

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

Actually, these onXXX() methods(the prefix "on" implies that these methods are callbacks for the system) should be called by the system, not the developers. Read the SDK document for more details.

like image 109
Huang Avatar answered Oct 21 '22 15:10

Huang


stopSelf() is called before the Thread finishes.
The Thread runs to completion when stopSelf() is called.

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

like image 39
Mithun Sasidharan Avatar answered Oct 21 '22 16:10

Mithun Sasidharan