Android: When to use Service vs Singleton?

I am completely new to Android development.

When is it better to create an Android service and not just use a simple Singleton class?

Take, for example, a data layer that downloads information from the Internet.

Using the service seems too big for some cases, but sometimes I may need access to Context , so I'm a little unsure how to create an application.

+43
android android-service
Aug 25 '10 at 15:37
source share
1 answer

If it is normal for your process to be killed (along with singleton) immediately after the user leaves his activity, then use singleton. If you need it to continue to work for a while after this, use the service. If you want to continue working after the user leaves him, but you can not live with him because the user is now connected to something else where more memory is required, then use singleton.

The solution between the two only comes down to the life cycle of your application. For this purpose, this is a service - ask the platform to change the management of your process. If you need context in singleton mode, just use Context.getApplicationContext () to retrieve the global context for your process.

+42
Aug 25 '10 at 16:45
source share



All Articles