Continuously Running Service verses periodically Starting Service in Android - android

I wish to do data polling(notifications) from service after a particular time interval(might be around 2-5mins). So I have written a service which will fetch data from the server. So what is the best way from performance point of view from below options.
Starting service every time when polling is to be done using Alarm manager
Or Starting service at Boot Completed using Alarm manager and keeping another Alarm Manager inside service which executes a task every particular time interval.
Thanks all..

I think the first option is better. Start IntentService with alarm manager. IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate.
See: http://developer.android.com/reference/android/app/IntentService.html

Related

How to make android service run every day once

I have a service that runs well but when phone goes to deep sleep , the service stop working .
I want to make use of some android class that makes the service to run everyday
at specific time ,??
i have tried wakelock but it drains the battery very fast for 24/24 cpu on
Any HELP ??
In case this becomes a bigger discussion then our comments. I'll list my answer here.
So your goal is to schedule a job with the alarm manager. Just make yourself a broadcast receiver class and register it on your app's startup. Then you will get code to run on each received notification.
If you need your service to run one time use an IntentService for efficiency instead of regular service.
Also, if you need to make sure it starts automatically then you should register for receiving of phone boot so that you can start your scheduled job again.
The link example:
Schedule a TimerTask at specific time once per day in a service in android
Better solution: you can use any of them alarm manager/jobscheduler/GCMNetworkManager
Create a alarm with looping every 24 hr interval.
That's all you need.
PS: Service will drain your battery.

how to keep an Intent service running

I have two examples of Intentservice. One is the Download example in the commonsware book. the other is at http://www.vogella.com/articles/AndroidServices/article.html#servicecommunication_handler.
Both of these examples show the service executing a finite task and they both apparently destroy themselves by running to the end of the scope of the onHandleIntent event.
The service I am writing has to have events and listen for things. One is a LocationListener listening for GPS movement. Another makes Posts to a REST service and listens for replys. I want it to run until a time has elapsed or until it was told to quit by the activity that started it.
How do I keep it running? Where, for instance, do I put my implementation of LocationListener?
Thanks, Gary
How do I keep it running?
You don't. IntentService is designed to do a piece of work (or perhaps a few off a queue, if commands happen to come in rapidly), then shut down.
The service I am writing has to have events and listen for things.
Then you should not be using an IntentService. Use a regular Service, with your own background thread(s) as needed.
To keep a service running, your service need to return START_STICKY in the service method onStartCommand().
With this, the service will be running even if you exit form your activity.
Note:
The Android still kills services after some time (30 mintus to 1 hour) if they are not foreground services. Use startForeground(notification) to make it foreground.
good luck
You can achieve this in either of two ways,
AlarmManager
TimerTask
AlarmManager is android's in-buite class that allows you to execute certain action on particular time peroid.
TimerTask does same thing as AlarmManager, you can repeat certain action of your code again and again.
However AlarmManager is ligher in the execution so i suggest you to go with AlarmManager class.
Create an AlarmManager that fetches the GPS Co-ordinates and post them to server on regular interval basis.
Have a look at to this AlarmManager Example.

Difference between service and using alarm manager in android

In android a service is used to run in background. But the same can also be accomplished using alarm manager. The alarm receiver can do the same things that you would do in a service. So what is the need of service in android?
Well Both are totally different thigns.
AlarmManager Class is used to perform certain Events on after specific Time Intervalr or it can be on a regular Time Interval. An Events can be execution of Service too. AlarmManager allow you to schedule your application to be run at some point in the future.
While in case of Service it is a background process which doesnt have/requier a UI. A Service is not a saperate process or Service is not a thread. A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use
Actually these are two very different things. Alarm Manager can be used to do some tasks periodically using service. But Service can be used also e.g. to move some heavy work out of the UI thread(download data from rest server) or in case of foreground services, to do some work continously (mp3 player).
You can get some explanation on how to use service (with some advices considering AlarmManager) on Styling Android.
Generally,a service is used to process logic when you don't need a UI anymore. For example the service I want to use checks a social networking site for updates every 15 minutes . The service has the logic of processing the update. But who will start the service every 15 minutes? That is where AlarmManager is used. It will periodically start my service every 15 minutes once, so that the service could execute its logic and stop itself once the job is done and my application doesn't keep draining the battery.

Timer Task VS Alarm Manager usage in Android Service

I need to fetch news/event updates from the server at regular intervals like for every 20mins in my Android App. AFAIK Intent Service and Broadcast Receiver combination will be better than using Service As I am not going to communicate with the running Service. In order to fetch events at regular intervals I know 2 options
1) Using Timer Task ScheduleAtFixedRate, I am going to start IntentService, which will fetch events once & broadcast if any updates and destroy itself. After Given Interval, IntentService will be again fired by TimerTask
2) Simply starting Intent Service at the start of app and within Intent Service onHandleIntent method Starting a TimerTask ScheduleAtFixedRate. If this is preferred way, How and when I cancel Timer Task and When the Intent Service is going to get Destroyed.
or I have to use Alarm Manager. Note I need these updates as long as I am using the App, also I need updates for every 20 to 30 mins not for every 1 or 3 mins.
Any body please suggest me, Thanks in Advance.
Go for AlarmManager. I have tried TimerTask before, it does not work properly in some devices and get killed after some time.

android services

I am creating an app which needs to retrieve a textfile from a webserver.
I have the manual retrieving done, just that I am thinking of adding a service to make the service check for the textfile like every 5 minutes and then send a notification to the user when a textfile is detected.
Is it ok to use a service here or do I have to use other methods?
Because the service will be kept running.
The best way of doing this is setting a repeating alarm using AlarmManager, a PendingIntent and a BroadcastReceiver as well as a Service. That way you don't have the service running all the time.
So the AlarmManager fires off the PendingIntent which is then picked up by the BroadcastReceiver which then starts your Service to perform the task in the background using a Thread or AsyncTask etc. Also look at IntentService which runs a Looper to process an Intent and then shuts itself down.
This is a great use of a service. Just make sure to allow the user to enable or disable service component. Also, try to shut it down when its not necessary (when the user cannot react to it).

Categories

Resources