I am doing an application on Android that gets locations from a webservice and puts it on a map. The app should do it every 15 seconds. What is the best way to do this?The communicaton with the webservice is made using REST architecture and the webservice returns a JSON. Should i use a Service or AsyncTask? thanks
You'll for sure see different opinions around here regarding this topic, but I've been using ExecutorServices for a while and they work really nice. Don't forget to stop it whenever the activity pauses.
Remainder: if the application should be an always running thing (for example a GPS navigation, or some type of location based social gaming) then you might want to use a Service that will also use an ExecutorService and don't forget to put a Notification letting users know that it's still running and that they can cancel if they wish to.
Related
i am currently trying to get code running as a background service.
what this code does is:
send request to server with current location of the user
receive response
parse response
save into model (singleton)
and this is set to happen in a 30 sec interval again and again.
now if my app stays in the background for too long, it will get disposed by the device and that code will not be executed anymore. what would be the right kind of background service for this usecase?
one of my main concernes is that i save my data in a singleton. but if my app is disposed this singleton will probably not exist anymore.
intent service doesnt make sense imho because it runs a one time tasks and has to be restarted from an app that might already be disposed at that point.
using the alarm manager would mean that i will have to save everything out of the app (sqllite for example) and then retrieve that data when the activity is started again which sounds rather complicated.
can someone please help me out here?
thanks in advance!
You sir needs the service of GCM
https://developers.google.com/cloud-messaging/
Thats exactly what you need for your desire ;)
But it's not less complicated as sticking to background services.
Also you can do a Hack: having two services watching your service to keep on running and itself...I swear when the User doesn't stop your app manually in the menu the System won't be able to stop them itself. Foolproof.
This is my first android app and so I am a little confused on what is the best method to accomplish this.
My app is going to get the GPS coordinated using the location services and save them in an online MySQL database of a PHP based website. Now I already have the code to retrieve GPS coordinates working and I am working on passing it to the online database. I am thinking about using AsyncTask to connect to the online database by making a POST request. Since the app will be constantly giving new coordinates, I was planning on executing the AsynTask in the onLocationChanged() method of my activity so whenever the coordinates are updates, the new updated coordinates are also updated in my online database.
Now my question is that is it okay to execute my AsyncTask in the onLocationChanged() method or is there a better way? Should I create a service to do the saving and start the service inside onLocationChanced method which will run the service everytime new coordinates are updated.
Another question I had was if the user opts out of the app, i am assuming my app will stop sending its coordinates to my online database if I implement it using AsyncTask in my activity. Should I create a service if I want it to keep sending the coordinates unless the user wants to stop the app and he will open it and maybe press a button to kill that service.
Any help would be much appreciated. Thanks
Go with the background service if you want your app to keep sending the location even if the user has navigated away from your application, if that's not necessary then an async task should be sufficient.
Note: if your update interval is short, say multiple times per minute for example .. consider batching multiple updates then sending them at once to save power and network bandwidth
Ok guys, I did ask before on how to create a way to run a code always in my app. It's similar to how say iMobeter or something like that check your hp, if it's less than max it'll add to it every 5 mins.
Some people told me to use service which sounded good. Problem is, I need a service that is always running in the background. Plus, I could not create a service that can access the database. I cant use the cursor there as in it does not support the method.
I would appreciate pointing me to a good example or a better way to do it.
Problem is, I need a service that is always running in the background.
That is not possible. Users can stop your service whenever they want, and Android will stop your service if it lives too long.
Plus, I could not create a service that can access the database. I cant use the cursor there as in it does not support the method.
Yes, it does. Use SQLiteOpenHelper, the same way you would with an activity, content provider, etc.
I have an app that checks GPS position and interacts with four websites. I am thinking of using ASync tasks within a Service. The result from each website does not depend on any other website.
As finding the location and interacting with each website will take a different amount of time, does this mean I should have five services? Or should it all be combined into one service?
You need to call the other 4 services after you get a GPS fix. You can create new instances of each AsyncTask running in parallel. I don't see the need for using a service. I use one httpClient object stored in an application class for all web requests.
You don't need additional services. The problem here, as I understand, is that each HTTP call might take a long time to finish and that would slow down the whole process and that you will have too many ASynkTasks. Then, Google has already solved the problem for you.
Instead of using ASyncTasks you can use Volley and send all your requests to websites in parallel. That way, Volley will handle your requests in different threads and keep everything tidy. As a bonus it is A LOT faster than ASyncTask.
I creating a small application that will basically use a background server to send data over HTTP. I dont know how to create services. Secondly there will be a couple of activities in my application. I want the activities to display a Context Menu when data becomes available. How can i do both. I have search for a while but the code i keep getting dose not seem to run on 1.6 api. How can i create the service and how can my activities listen to updates so that when a update is available they display a message.
NOTE: I do not need help on the HTTP part and the server part only creating the service and my activities listening to updates.
Kind Regards,
Give the Service docs a very good, thorough read.