Android Service Performance Tip

Hi there!

While working on an Android project, I had modified a Service in a way that it didn’t stop itself anymore. This made me feel really embarrassed, when I found out that I had broken a critical rules, which I found in a very nice Android Best Practices presentation that I came across ( I confirmed that my Service was still running by visiting the running services page on the Android settings (Press Menu -> Manage Apps -> Running tab). There I noticed that a whole process was being kept awake because of this Service that was left running forever!

Indeed, the best use of a Service is made by starting it and ensuring that it will call selfStop(), when it reaches a certain state. This ensures that the service will keep running only for the period that it has real work to do (this might not stand in every Service use case, but still think a lot before concluding to let a Service run forever!). If a Service needs to start at different points in time to do some work, you can use the AlarmManager to wake the Service app, or register a BroadcastReceiver to instantiate the Service to do the required work.

So, probably there is always a better way to use a Service than let it run forever. In this way you respect the users of your application, who should really appreciate that you don’t drain their battery!

Happy coding!


About sermojohn

Professional Software Engineer
This entry was posted in Android and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s