I am designing an Android application that needs to receive constantly changing information (so the update interval must be very small).
Right now I am reading this information by consuming a REST WebService, but I am wondering if this is the best approach, regarding the battery drainage. This application would have to refresh the data being shown very often (specifically GPS data read from another system).
I have been reading about Google Cloud Messaging but I didn't find any comparisons between the two approaches.. Theoretically both approaches fit my needs since the data changes so frequently that it doesn't really matter whether it is updated by request or on a timer..
So my question is: Should I stick with WebServices or shift to GCM? Is there another battery-efficient solution?
Thanks in advance!
GCM utilises a connection that is already there - using this over your current polling methods is definitely recommended and will be much friendlier with the battery.
Think about it, in your case GCM will send you a notification to let you know you need to refresh data so instead of just calling refresh you have: receive the notification, process it and then call refresh. So if you need to refresh so often, like just say each minute and it's constant just skip the notification.
I would set a recurring alarm, get the intent in a BroadcastReceiver and trigger an IntentService for the refresh
Related
My app requires real time updating of information, i.e updates around every 20 seconds, and the size of the data can vary (can be >4KB).
What would be the ideal method of polling the server for data or even somehow pushing the data to the client instead? I'm guessing one of these methods https://developer.android.com/topic/performance/scheduling.html would be suitable but not sure which one is suitable for my needs. Are there other more battery/data efficient ways of doing this.
The app only needs to receive information when it is open and in the foreground.
Thanks in advance
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 :)
I'm currently making an app in Android that is checking an API which returns two things. Some text and a colour.
However I want this to be checked for updates every 15 minutes in the background and check every 5 seconds when the app is open. When running in the background it should give a notification if the status is changed.
Now I have checked numerous stackoverflow q&a's and forums, docs etc.. But I can't seem to find a good baseline for what I need. So many documentation that contradicts eachother.. I think that I need an Alarm Manager or a Service... but what do you guys suggest for my problem? The app may not harm the battery too much.
What I really would like to have is that the application doesn't have to "poll" the server every 15 minutes but that the application gets interrupted like.. "hey, there is a new status update". I can't imagine that messaging apps are constantly polling a server for updates? I haven't found much information about that topic... Any help is appreciated. Not asking for code but directions to get where I want to go.
Many thanks
If you're looking to poll the server every X seconds/minutes, AlarmManager(android guide, tutorial) is exactly what you need. However, as you point out this is probably not the best way to go about things. While the app is open you may want to look in to passing messages between the device and server via an open Web Socket. Once your app is closed you could, instead of the app polling the server, have the server push a notification, via GCM or some such, to the app when an update is available.
If you are doing both the server side project and the mobile application, You can use Any messaging service rather than polling for the server, Because there has to be a pusher implementation from the server side to push the status to the MS.
For now GMS is free, I hope it will remain the same :). Otherwise, You can use AlarmManager and IntentService to achieve your goal.
For one of the screens in my android application, I need to listen to server indeterminately - ie; I have few fields in the screen whose values change continuously so long the screen is kept open. The values to be updated will be provided by the server continuously. I understand that normal http connection would not be a solution here. Also, I do not wish to make continuous http requests owing to performance reasons. What is the best way out in order to accomplish this.Is GCM Cloud Connection Server a good solution for my requirement. Or are there better solutions? Please advise.
Any help is much appreciated.
I think there are a two options. If you don't own the server yourself I would start a service to run in the background and bind to it. The service would poll the server at some time interval depending upon how often you want the values to update. The activity would then receive periodic updates and update the views. Given that the information that you're updating is really not all that large, updates every 30s to a minute wouldn't take a toll on performance at all since all of the work would be done in an asynchronous task.
Using an AlarmManager to accomplish this.
If you own the server then you could implement the GCM model, and only send updates when data changes. This is assuming that every user of the app would get the same set of updates of course.
Introduction to GCM
Keeping screen on could be battery consuming. If you own the server the changes can be pushed to the app using the GCM service.
As far as I understand, GCM bundles push messages from several server trying to push the messages together and hence is an optimised way to communicate.
Alternatively, you can bring up a server which can keep polling the original server and push the changes to the app through GCM.
I am building an app that shows user's friends' real-time locations on a map.
Each user updates his currnet location on the server (Parse.com) every defined interval (time and distance).
When a user opens the FindFriends activity, he'll get a map with markers on friends locations.
The friends locations data can be retrieved in two methods:
When activity is running, I am calling an AsyncTask that will download locations data from the server in a while loop (inside doInBackground), and after each time I call publishProgress() to handle the changes on the map.
I wrote a cloud code that is called every time a user updates his location on the server, which sends push notification to the relevant users (the user cant see the notification). When the FindFriends activity is running, a broadcast reciever is getting those pushes and update locations data on the map.
My question is: Which method is better for my needs or maybe I should do it differently? Of course I would like to keep battery consumption to minimum but not at expense of getting almost real time data.
I don't really think that this is an appropriate question, since you really should just make a prototype using each implementation, and see which is more performant in your particular use case. However, I can say that I definitely prefer the cloud solution. I've used GCM to implement a messaging client, and from my testing the updates are definitely really quick (very little noticeable lag time, when testing two clients side by side). So, I don't think that implementing it with GCM would negatively impact your user experience, and it does have the plus side of eliminating pointless server polling when, potentially, no updates could be present - which saves on battery.
If you want real time data then push notification GCM is not a good idea because, there is no guarantee that all users will get push notification in time. Sometime you will encounter delays in push notification.
Under given circumstances AsyncTask or Service can be a good way to perform required task, if they are handled properly. You should have control on AsyncTask/Service so that you can stop it any time you want.