Skip to content Skip to sidebar Skip to footer

Android: Is It Possible To Create A Database And Save Some Information In It Every 100 Seconds

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 infor

Solution 1:

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

Solution 2:

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

publicclassMyCounterextendsCountDownTimer {

        publicMyCounter(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @OverridepublicvoidonFinish() {
            MCObject.cancel();
            MCObject = newMyCounter(_intMCCounter, 1000);
            MCObject.start();
            newtempAysnc().execute(paramList); // code to get data or store data write your code to insert data into database

        }

        @OverridepublicvoidonTick(long millisUntilFinished) {

        }

    }

In your Activity onCreate() write this code to initiate it first time.

_intMCCounter =60000*5; //setintervaltoevery5 minutes
MCObject =new MyCounter(_intMCCounter, 1000);
MCObject.start();

and in onDestroy of your Activity write this code to cancel the Timer.

MCObject.cancel();

Solution 3:

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

Solution 4:

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 ScheduledThreadPoolExecutorhttp://developer.android.com/reference/java/util/concurrent/ScheduledThreadPoolExecutor.html

Android natively supports SQLite so you can use it as your database.

Post a Comment for "Android: Is It Possible To Create A Database And Save Some Information In It Every 100 Seconds"