I am currently developing Android application with webservice synchronization and everything is working smoothly, but I need to create a background Update service, for data fetching to database. Last couple days, I’ve been searching for information how to accomplish this and I found many solution, which confused me pretty badly.
I am fetching data using asyncTask which fire up the httpClient and the I parse response and save it to database. After that I send an intent to launch an Activity. Simple stuff.
I want a service, which would fetch the data while the application isn’t running. And probably stop the service, when user start the application.
I manage to write a Service class with thread sleep, but this seems like a pretty bad solution.
Another solution I found, is by using pendingIntent and broadcast receivers.
I am not sure if sending broadcast intent is a secure enough. If someone plug his phone into a debugger, he would see every intent broadcast, right? Which means he/she could mess with my application (like send an broadcast intent, which would inform application that new data has been fetch, when in reality, that would not be the case).
I also don’t want to getting my service killed by system.
Is there any simple and effective solution for this? I kinda struggle to find something suited for me needs and I am pretty new to Android programing to write it by myself. I found many solution, but I really don’t know which one is “right” solution, which is the most effective.
You should use AlarmaManager to schedule periodic tasks. No long running service needed. It just executes the schedule pending intent and finishes after all the execution is complete.