Service accessing a database - android

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.

Related

what kind of background-service to use for background request and saving

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.

Get data from webservice and put on map

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.

Android: can't decide how to manage background task

I have a task which I need to run in the background in my Android app. It reads data over the network and populates a database. It can take several minutes to run.
Once it's started, it needs to complete successfully without interruption. (Otherwise I'll end up with a broken half-populated database.) I realise I can never guarantee it will always complete, but I want to make it as hard as possible for the system to kill off this task. For safety I guess I will have it populate a temporary database, and then only swap out the old database for the new one on successful completion of the import.
It's a modal operation; it does not make sense for the user to be interacting with the app while the import is in progress.
My first attempt is using an ASyncTask with a Progress dialog to achieve the modality, but this obviously breaks the "don't interrupt" requirement. I could work around the screen-rotation issue with ASyncTasks, but I don't think that goes far enough.
At the moment I'm not sure if this should be an ASyncTask, a Service, an IntentService, some combination of these, or something else entirely. Can you help me decide?
I'd run it as a service and additionally I'd also have a clean SQLite DB on my server populated with the data the clients are going to retrieve so I can generate a kind of signature. Have the clients check for the correct signature of the DB. If the signature is not matching the servers signature then reinitialize the database filling process.
This is just an idea tho. I have no idea whether it'd be possible with what you are trying to do or not.
You are better off with services in that case. The Android runtime will leave it alone working as long as enough memory is available. In the case it kills the service, you can save the state in a bundle, and the system will restart the process as soon as possible, so you can resume the process, if possible for your solution:
Android Fundamentals, Service Section
Then it is easy to communicate with the service, like showing the progress/ notifications etc, using a handle registry like proposes by Mark Bredy in his Android Service Prototype

Advice on structuring a service, activity, thread

can someone please help me?
I would like to write a program which uses a service to periodically update a text view on an activity.
I do this by having ActivityA with a 2 buttons to start/stop my service. In the service I run a timer which triggers every second. From here I need to have this launch and update a text view on ActivityB which at present is just a counter value.
I'm sure there are likely better ways to do this, such as using only one activity, maybe using a thread but the main design consideration is to have the service running even if my activity is destoyed (the counter value would instead go trigger some alarm or file write instead of a text view update).
Sorry for rambling. I find the android developer resources offer too many solutions!
Thanks
Ben
In the service I run a timer which triggers every second.
Why? Most Android devices run on batteries. Batteries are never big enough. What value are you giving the user to justify your expenditure of CPU and RAM (and, hence, battery life)?
From here I need to have this launch and update a text view on ActivityB which at present is just a counter value.
Where is "here"?
I'm sure there are likely better ways to do this, such as using only one activity
I would think so.
maybe using a thread
Probably not.
but the main design consideration is to have the service running even if my activity is destoyed
This is significantly more complicated than you are perhaps thinking.
(the counter value would instead go trigger some alarm or file write instead of a text view update).
If your goal is to do something at a particular time, use AlarmManager.
I suspect that there is a better approach for whatever it is that you are trying to do than the path you are presently headed down. Unfortunately, since I do not know what it is that you are trying to do, I have limited ability to provide more specific advice.
I think what you want to do is at best done with an simple AsyncTask. If you use the onProgressUpdate method you can increase the value in the textview at every time you reach a certain point during your background work. It is also able to cancel the background work etc. There is no need for the full Service, Thread work.

Which Android service path to choose for implementing a large upload?

I am trying to implement a RESTful API in which I have to upload files that are relatively large for a mobile platform. The upload could take anywhere from 30 seconds to 5 minutes.
I would like my application to be completely usable while the upload takes place. I've been doing some research and I've come across a few methods from which I can't decide which is the correct solution to my problem.
These are the two things I have come across.
Using an IntentService -- handles the worker thread so I don't have to.
Using my own type of Service-- still need to implement an AsyncTask to handle the large process.
Implement an AsyncTask in a singleton file that would allow me to do all the work there but without using the service class.
My question is which method is the best -- if one isn't listed but is a more apt solution to my problem then I would appreciate suggestions.
After researching all these methods I am still also confused on one thing. Lets say I upload a 2MB files, but I want to know when it is done. For example, lets say I have a service that uploads an image and returns and imageID -- I need to be able to know when that service returns an imageID -- process that and then move on to the next upload until the rest are finished.
As always, thanks in advance.
EDIT: I forgot to specify that while uploading I want the app to be usable-- that means that I can switch activities and still have the same service run.
I used IntentService. It extends service so it basically has all the functions of the normal service, expect that it handles the threading for me so I don't have to worry about that. Its working very well at the moment.

Categories

Resources