0

I have a simple question. I'm trying to insert current date into database every 1 second, but when I try to do this, the date is always the same, and I want to have a current date. Next problem is, when I try to close database with button "Stop", I'm getting this exception:

01-27 13:57:51.063: E/AndroidRuntime(12964): FATAL EXCEPTION: Thread-144
01-27 13:57:51.063: E/AndroidRuntime(12964): java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/pl.pawelfrydrych.flyingball/databases/baza.db
01-27 13:57:51.063: E/AndroidRuntime(12964):    at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
01-27 13:57:51.063: E/AndroidRuntime(12964):    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1437)
01-27 13:57:51.063: E/AndroidRuntime(12964):    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
01-27 13:57:51.063: E/AndroidRuntime(12964):    at pl.pawelfrydrych.flyingball.MainActivity.insertData(MainActivity.java:146)
01-27 13:57:51.063: E/AndroidRuntime(12964):    at pl.pawelfrydrych.flyingball.MainActivity$2.run(MainActivity.java:248)

insertData method:

 public void insertData() throws InterruptedException {
     ContentValues values = new ContentValues();
     myDBAdapter = new Database(this).open();

     while(true) {
         Date date = new Date();
         long millis = System.currentTimeMillis();

         values.put(Database.LEFT_POSITION, xPosition);
         values.put(Database.RIGHT_POSITION, yPosition);
         values.put(Database.GPS, GPSposition);
         values.put(Database.TIME, date.toString());

         if(myDBAdapter.db != null) {
             myDBAdapter.db.insert("baza", null, values);
             Thread.sleep(1000 - millis % 1000);
         } else {
             Log.d(Database.DB_NAME,"db is null");
         }
     }
}

MainActivity onClick method:

public void onClick(View v) {
    switch(v.getId()){
        case R.id.bStart:
            Thread watek = new Thread(){
                public void run(){
                    try {
                        Looper.prepare();
                        insertData();
                    } catch (InterruptedException e) {
                        Log.v("Thread", "Problem with inserting insertData method");
                        e.printStackTrace();
                    }
                }   
            };

            watek.start();
            break;

        case R.id.bStop:    
            myDBAdapter.close();
            break;  
    }   
}
Melquiades
  • 8,496
  • 1
  • 31
  • 46
Algeroth
  • 785
  • 3
  • 12
  • 29

2 Answers2

0

I think u can use TimerTask for repeating something with a specific interval. u can use scheduleTask() with a interval of 1 sec as u needed. as code for this

TimerTask task = new TimerTask() {
        public void run() {

        }
    };
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(task, firstTime, period);

If u don't want to use Timertask u can use 1 alternative as well. or can follow the link for similar. Android - Controlling a task with Timer and TimerTask?

 private Runnable onEverySecond=new Runnable() {
public void run() {
    // do real work here

    if (!isPaused) {
        someLikelyWidget.postDelayed(onEverySecond, 1000);
    }
}

};

Community
  • 1
  • 1
Manmohan Badaya
  • 2,326
  • 1
  • 22
  • 35
0

1) try to use the sqlite syntax:

values.put(Database.TIME, "NOW()");

2) on the button 'stop' you should exit the while-cicle, for example: make a global flag

boolean stop_posting = false;

on start button, clear this flag

case R.id.bStart:
 stop_posting = false;
 /*...*/

on stop button, don't close the DB, but set the flag

case R.id.bStop:
 stop_posting = true;
 break;

in the cicle, check this flag and return when it is set

while(true){
 if(stop_posting) {
  myDBAdapter.close();
  return;
  }
/*...*/}
kirill
  • 366
  • 1
  • 8
  • values.put(Database.TIME, "NOW()"); not works, it added in column Time, string value " NOW() " but not date ;) – Algeroth Jan 27 '14 at 19:34
  • OK, then instead of `Thread.sleep(1000 - millis % 1000);` use `Thread.sleep(1000);`, because now your code sleeps for unpredictable number of milliseconds when you want to sleep for 1000 milliseconds each iteration – kirill Jan 28 '14 at 05:47