I collect some information inside a Spinner and this information can change
for example something may disappear or something new may come
I want the application to store this information every 100 seconds in a database
is it possible to do something like this?
I'm thinking of creating a database and within this database storing this information
but I'm not sure about this "every 100 seconds" part, how would I do this?
I'm thinking that maybe I will have to create a process
that will be running in parallel, this process will have a time counter and when 100 seconds have passed, the function that will store the information in the database will be called
But I'm not sure if this is possible. I mean, creating such a counter that will run in parallel with the other application.
maybe there is something easier
thanks in advance
On Second Thought:
This is probably not the way to go(AlarmManager). A much better way would be to bind a listener to the spinner. That way you will only react and save data when there is new data to save.
Can you provide some details on the Spinner you are using and perhaps we can work out the event binding.
For the storage, use SQlite: http://developer.android.com/reference/android/database/sqlite/package-summary.html
For the 100 second interval, use the AlarmManager:
This class provides access to the system alarm services. These allow
you to schedule your application to be run at some point in the
future. When an alarm goes off, the Intent that had been registered
for it is broadcast by the system, automatically starting the target
application if it is not already running. Registered alarms are
retained while the device is asleep (and can optionally wake the
device up if they go off during that time), but will be cleared if it
is turned off and rebooted.
http://developer.android.com/reference/android/app/AlarmManager.html
Take a look at this answer for a code sample and further discussion: Android: How to use AlarmManager
I have some same requirement and done this in My application using CountDownTimer
and Created one Custom Class
extending CountDownTimer
and in that when Finish
, I just did perform my data loading and initialized the same object agian to run after using Start
public class MyCounter extends CountDownTimer {
public MyCounter(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
MCObject.cancel();
MCObject = new MyCounter(_intMCCounter, 1000);
MCObject.start();
new tempAysnc().execute(paramList); // code to get data or store data write your code to insert data into database
}
@Override
public void onTick(long millisUntilFinished) {
}
}
In your Activity onCreate() write this code to initiate it first time.
_intMCCounter = 60000 * 5; //set interval to every 5 minutes
MCObject = new MyCounter(_intMCCounter, 1000);
MCObject.start();
and in onDestroy of your Activity write this code to cancel the Timer.
MCObject.cancel();
I think better way is to create a Listener which will check for the data change. If it founds any change of data then it make a function call which will store data in database.
You can use SQLite
I don't see any reason why this couldn't be done. (Only problem is maybe you run out of storage space after a while if you are planning to insert new data constantly)
Check ScheduledThreadPoolExecutor
http://developer.android.com/reference/java/util/concurrent/ScheduledThreadPoolExecutor.html
Android natively supports SQLite
so you can use it as your database.