Alarm manager with asynctask in android - android

i have a application where i have to send gps(location) updates every 10 minutes to a server.So i am using alarm manager.I have read that async task is the best way for communicating with a server.is there any other way for sending the location updates?or is this a good method?

Related

Android app that shows notification when server sending data

What service should I use to check every few seconds when the app is closed, if there is any data incoming from the server?
I tried Alarm Manager and schedule multiple alarms that schedule each other every few seconds. But the alarm manager is not very accurate. Then I checked out JobScheduler , but it's good for specific conditions. Is there any service that is good for this need?

Android: Send location data in background periodically (every 30 seconds) and ask server for new data

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.

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.

Continuously Running Service verses periodically Starting Service in 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

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.

Categories

Resources