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.
Related
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.
I have some data in my SQLite database table. When my app starts, I want to send that data to the server every 5 minutes.
When the app is closed, it should stop.
What is the best approach for this?
Should I use Service or IntentService?
Should I use AlarmManager, Handler or any other thing?
I'm aware of my application speed. I don't want to make it slow. What is the effective approach?
If you are only transmitting when the app is in foreground, you can do it with a Handler.
You start the handler in onResume() and cancel it in onPause().
And perform the transmision with an AsyncTask or in a separate thread.
If you need to stransmit in background, you can use a service instead and schedule it with the AlarmManager.
And then start the service from the app's Activity.
I am not sure how IntentService would be used for this.
As you just want to run your process while the app is foreground, then TimerTask or Handler with conjunction to Message or Runable is good. It won't bother much your app's performance. If you ask about the better one from these two I'll say it's Handler. Check the details here in this answer:
https://stackoverflow.com/a/3975337/4128371
But if you want a really good performance then I'll suggest to go with AlarmManager. Otherwise Handler is a good option.
Alarm manager is not precise, an alarm schedule for 5 minutes may be that when the device is sleeping it will be fired at twice or triple the time.
If you want accuracy the device does not have to fall in sleep. ( I know that will shorten battery duration)
If you want to prevent the device to go to sleep you need to launch a foreground Service with a non dismissable Notification. That's the Service has to call startForeground()
While the device is awake both Alarmamanager and Handler + Runnable will be accurate ... I prefer the Handler.
Scenario:
Post to server to get any new data in background every 30 seconds for long period i.e. 12 hours. Location data needs to be sent along with this.
Current Implementation;
Service Class;
Location listener with interval of 30 seconds which sets the longitude & latitude values to two local variables
Alarm manager fires pending Intent every 30 seconds to a broadcast receiver.
Broadcast receiver starts an IntentService with location variables in the extras.
The IntentService http posts location and asks for any new data from server.
IntentService send server response back to main service class via broadcast receiver.
Service class starts_sticky to ensure restart by the OS.
I have tried a few different variations;
I've tried using a Handler and runnable to handle the timing mechanism for posting to the server however, the postDelay time went from 2 minutes to 7 minutes when device is asleep.
Also, tried firing IntentService directly from alarm manager but could not change PendingIntent extras with the most up-to-date location variables.
Questions;
Is the current implementation the way to go?
Would going down google's GCM route be much more beneficial?
How can you vigorously test the service class especially with respect to recovering from the OS killing it?
Thanks in advance.
To avoid the OS to kill your service, you must notify the user that your service is an ongoing service, like described in http://developer.android.com/guide/components/services.html#Foreground.
I managed to have my service running every 2 min at background using AlarmManager and WakeLock, like described in this answer. Even when the device is sleep, it runs every 2 minutes. Just set the Alarm to repeat like
alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+5000, 60000 * ALARM_PERIOD_IN_MINUTES, alarmPendingIntent);
instead of setting it to a time.
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.
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