Service with AsyncTask performing a request every X time - android

I want to be reassured that I'm doing this the best practice way :
I have a list which order is changing on the server, therefore, I want every 20-30 sec. to perform a request to see if there were any changes in the order. So, I've created a Service which is bound to the Activity with the ListView, and the service every 20-30 sec. performs a request with an AsyncTask.
I chose to perform it with a Service because I want the list to be updated constantly, even when the application is in the background, And the AsyncTask is because I don't want it to be performed on the main thread.
Is this the right way to do it?
Thank you in advance,
Dan.

It will work, but your app will do a lot of unnecessary work. This will affect battery life. Besides that, according to documentation, if you use device radio, it stays full-powered at least for 20 seconds, which is also no good for battery. You have the following options:
Use Google Cloud Messaging. It will allow you to perform update only when this is really necessary.
If you don't want or cannot use GCM, follow this guide to optimize network access. Start with increasing the update interval (to 4 minutes at least).

Related

Background accumulated tasks executed periodically

I am devolping an Android App that need to execute an accumulation of internet tasks periodically (like a web bot). Those tasks need to get stored at a specific time so I thought about using Alarm Manager and a
embeded Database. Due to it, the app could be active much more time, although those save tasks do not need web connection. Later I will throw another Alarm Manager to execute all the tasks queued and do web stuff.
Otherwise I am not sure if it is better to use a foreground service. The app will be working all the day saving the tasks (each 5 or 15 min) but only running task queue with internet each 30mins.
I feel capable of developing both systems but I would like to know which one is better in terms of performance, of battery consumption.
Thank you very much.
Only a recommendation try to find and use a nice library to do it, every Android Update change something about foreground/background servicess.
one is
TimedDog
and for more
enter link description here

Foreground or Background service to track user's location?

I'm developing a location aware app. This app will start tracking users when they are in their workday. These are the requirements:
The service should run on a regular basis (every 30 or 45 min).
It won't matter if the service does not trigger at the same basis everytime.
The service needs to upload data to our firestore db.
I doesn't need to be triggered on specific conditions (data is on, phone is charging, etc...)
I need this to run even if the user restarts his phone.
We may need to track the distance traveled by the user. (This is not a requirement per se, but it may be a feature someday)
I know there are a lot of ways to achieve this, but I have not decided which is the option that best fits my scenario. This is what I've thought so far:
Foreground service combined with BroadcastReciever in case the phone is rebooted
Background service using the new jetpack's Workmanager.
Which will the best solution? Do you think there is a better solution than those?
Thanks!
Was thinking create a GPS location tracker so when they are in work premise as well as outside it kinda shows.
Then consider adding the number 5 of the above. Like you said there could be mire awesome solutions than these so lets wait for options.

Data exchange between Activities, Asynctasks and Services

Regarding the problematic stated below I have come to a point where I need to make a decision on whether to:
Start a Service once that has an AlarmManager inside which then starts the query every 10 minutes. This Service will only be stopped if the user sets an "Onn-Off" Switch to "Off".
Use an AlarmManager to start an IntentService every 10 Minutes. This Service will then only be started when needed and closed afterwards
Which of these ways is better when it comes to:
- Ability to exchange data received by the Service (Or Intenservice) with other activities/services
- Battery usage
- Overall "good coding habits" ?
Thanks!
Original Question:
I am a pretty new Android Developer and have come across a situation that I do not know how to solve. I have already spent several days searching for a solution but could not find one.
While trying to develop my first app idea I have started playing around with receiving and parsing data from the internet. What I have achieved so far is generating a query that receives JSON data via an API and parses this JSON. All of which is done inside an AsyncTask. The received data is then shown on the screen.
However, for the purpose of my app idea, I need this to be done in the background. What I have thought of is:
Starting a Service that pretty much has the same logic as my Asynctask. Managed by an AlarmManager, this service then requests, receives and parses the data in a specific time interval.
Now the tricky part begins:
The data that I receive (let's say every 10 minutes) shall be used to change an alarm clock. So, as a simple example, let's say the user can set his alarm clock to 08:00 in the morning. The application then checks the current temperature every 10 minutes and changes the alarm clock time to 07:45 if the temperature is below 0° celcius because the user has to wake up earlier to clear the ice off his car.
Also, when "waking up" the application, the current (or rather the latest received) tempereture shall be shown in the UI.
What would be the best way to achieve this? I am having some issues regarding passing/receiving data from AsyncTasks/Services to/from Activities.
My first approach would be to start a single service from the MainActivity, passing some data to the Service (like the initial time the alarm shall start and the current location of the user). The Service then has two seperate AlarmManagers. One of which is set to perform the actual alarm (waking up the user in the morning) and the other manages the time interval of getting the data from the internet.
My questions:
- Does my train of thought make any sense at all so far?
- What is the best way to pass and receive data to/from a service? My best guess would be to use intents to pass and a broadcastreceiver to receive data from the service. would this make sense in this specific situation?
I fear that it is not welcomed to post questions without putting in any effort of your own before. Although I did not add any actual source code, I hope you can see that I have dealt with these questions for quite a while now but could not really start coding before I know the structure of the application.
Thanks in advance
Use AlarmManager to start an IntentService as often as necessary (in your example, it should be sufficient to start checking the temperature about two hours before the user plans to get up and maybe again after one hour and finally half an hour before the normal wakeup time. More often only in case of extreme weather conditions.
It's not necessary to check the temperature exactly at 03:33 a.m. so use
setInexactRepeating(), this will be easier on the battery.
See also Scheduling Repeating Alarms
Write the results to SharedPreferences and have one IntentService check 15 minutes before normal wakeup time if the user should get up right then. Cancel the normal wakeup alarm in this case. Communicating via SharedPreferences (think of a mailbox) and local (!) Broadcasts is a good idea - cheap and secure :)

Best Way to Frequently Poll a Server from an Android App

I have an Android app which needs to poll a web server at 2 second intervals.
So I would like to know what the best way to do this would be. My first thought was to use an AlarmManager but I believe this is no good for anything more frequent than about 5 minute intervals. I have also considered using a service but I am concerned this will drain the battery. Are there any options I haven't considered? What is the best way to poll a server very frequently without killing the battery?
I also know that GCM is the ideal way to sync with a server but unfortunately it is not an option at this time.
Edit: ok, it seems from your comments that it is as I feared and there's no good solution for this. I will probably implement it in a service and press for a push mechanism instead. Thanks for your help.
The solution I have come to is gaining agreement to redesign so that the server pushes to the app only when the data has changed (it won't change that often, realistically).
In the meantime I have made some small changes to the service so that it cancels all requests when the app isn't in the foreground and reduced the poll time to 5 seconds (better than nothing, right).

How to loop or to repeat periodically task in Android?

I am quite new to Android, and I am really wondering about is how to loop or to repeat periodically a task.
In my program, I have UpdateLoc() that sends my gps location to my databse, but I want it to update periodically (whether it be 3 min or 3 hours) without using too much battery and CPU.
The problem is that I have no idea where to start... Is there a simple way to do this?
You might want to look here:
http://developer.android.com/reference/android/location/LocationListener.html
http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates(java.lang.String, long, float, android.location.LocationListener)
This will allow you to receive updates only after a particular amount of time has past.
I believe you have two options in this case:
Service - This will stay running in the background but use more battery.
AlarmManager - You can schedule a task to run in the future to briefly perform an update operation and should use less battery.

Categories

Resources