Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Please, explain some Android Service Concepts

I'm just approaching Android Services, but i have many doubts. Here there are some questions. Before starting, notice i have read those pages Android Services (official) Bounded Services (official) plus sone Inner classes theory in my language. Please be patient, i'm still a bit confused.

1) First of all, a Services differentiates itself from an AsyncTask mainly because it continues to run also if the app is paused (i.e. the user is watching another app); AsyncTask is stopped in that cases. Is it ok or am i wrong? 2) A Service runs in the same thread of the activity that started it through startService(). To not affect the Activity's performances, i have to create a separate thread for that Service, for example implementing the Runnable interface. Another method is making a service that extends IntentService, which automatically provides a new thread for the service: a new thread is created on any onHandleIntent() call.

Now, let's look at my concrete problem. I need to create a Service that will be used by many Activities: his task will be to connect to the server DB every 60 seconds and check for news. If a news is found, notify there's a new news (if we are on MainActivity) or show the new news's title (if we are in the news reader). How should i code it? I have made a MainActivity that instantiate a NewsService and immediately calls startService(). On the other side, i have the NewsService extends IntentService, that (creates a new thread when onHandleIntent is called?) and looks for new news. Is it a bad idea to use a IntentService? I realized it will be very ugly to start the service calling startService() indefinitely. At the start of this exercize i tought it was a good solution because it automatically creates a new thread and makes Service implementation simple. But now i have some doubts (i can't know if there's a news! How can MainActivity know it? And how to get the title)

This should be done with a normal extends Thread class, that makes an infinite cicle in it's run() method, checking for news every 60 seconds and, if there's a new one, reads the title from remote DB AND update activities buttons/views. Then if the App will be closed by user, the Service will be closed too. But the problem is that if i istantiate such class it's work will be stopped when the MainActivity is paused or stopped, and other Activities (the NewsReader in this case) cannot get any update because the new thread isn't getting news at the moment. So i need a Service.

I hope it's clear. How should i implement a solution in the right way? Please highlight everything wrong in my text, i really need to learn :D

like image 513
mark Avatar asked Apr 01 '14 08:04

mark


1 Answers

You seem to have understood everything correctly.

As to your specific problem, I'd recommend the following:

  • Use AlarmManager to schedule your service. Don't let the Service run when it does not have to.

  • Use a Broadcast Intent for new news. All Activities will have to have an inner BroadcastReceiver that listens for the Broadcast intent of the service and reacts accordingly.

like image 116
FD_ Avatar answered Nov 12 '22 23:11

FD_