Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use a android service?

Tags:

Im wondering what is the point of using a android service to do background work when you need to do a lot of things just to access any public methods or get a large chunk of data from a service such as a larger List object.

why not just use a simple POJO that does stuff in the background for you in a seperate thread if you like and gain access to its public methods without creating interfaces using AIDL, binding to a service etc etc?

seems soo much work needs to be done to access a method from a service class or is that really not the point of a service class in android?

i have developed a android service class that gets 100's of items in a xml structure from a web service who i then parse it into a POJO that is then stored in a List but i am having difficulty finding a way to send this List back to the activity that called this service.

i've read about using parcebales objects but that along with all the intent.putExtra have a size limitations so i may run into problems in the future.

i am thinking of ditching Android services and i have quickly found out why i dont like using them in the first place :(

a simple SomeBackgroundPojo backroundTask = new SomeBackgroundPojo(); backgroundTask.getData();

Seems soooo much easier than dealing with parcelables, serealizable objects, AIDL, binding etcc etc all just to achieve the two lines of code i just typed above :(

like image 556
Jonathan Avatar asked Oct 25 '10 15:10

Jonathan


People also ask

What happens when you start a service in Android?

Whether it will "restart" will depend upon whether the service was still considered to be running from the previous startService() call. If something called stopService() or stopSelf() to stop the service, then a subsequent call to startService() will create a fresh instance of the service.

What is Android system service?

Android services are system component which allows performing a longer-running operation while not interacting with the user. Just like Activities , Service runs on Main Thread and has a life cycle but unlike Activity , Services do not have a UI.

What is the difference between service and IntentService in Android?

Service class uses the application's main thread, while IntentService creates a worker thread and uses that thread to run the service. IntentService creates a queue that passes one intent at a time to onHandleIntent() . Thus, implementing a multi-thread should be made by extending Service class directly.


1 Answers

Nitpick: an object running a threaded background task ain't precisely what's usually meant by a plain old Java object.

If you don't care what happens to the work being done if its requesting Activity or Application is shut down, then by all means avoid services. One of the main points of Services is that they can stay alive when the user navigates away from an Activity: if you're only doing work to fill a UI ListView, by all means use e.g. an AsyncTask, as is discussed in this earlier question.

like image 145
Pontus Gagge Avatar answered Oct 05 '22 14:10

Pontus Gagge