2015-08-14 277 views
-1

我有一個查詢,我想每30秒執行一次並將其記錄到Logcat。我是通過處理程序完成的,我沒有得到迴應。如何在android中每30秒執行一次查詢?

這裏是我的代碼:

runnable = new Runnable() { 

     @Override 
     public void run() { 
      Cursor cursor = null; 
      int i; 
      try { 
       String sql = "SELECT * FROM info"; 
       cursor = database.rawQuery(sql, null); 
      } catch (SQLException sqle) { 
       throw sqle; 
      } 

      if (cursor != null && cursor.getCount() != 0) { 
       cursor.moveToFirst(); 

       do { 
        i = cursor.getCount(); 

       } while (cursor.moveToNext()); 
       cursor.close(); 

       if (i > count) { 
        count = i; 
        Log.i("COUNT", "" + count); 
       } 
      } 
      handler.postDelayed(runnable, 30000); 
     } 
    }; 
    runnable.run(); 

如果有人能幫助我,我將不勝感激。

回答

3

創建一個定時器將是使用Timer類的最佳途徑

Timer _Request_Trip_Timer = new Timer(); 
    _Request_Trip_Timer.scheduleAtFixedRate(new TimerTask() { 
     @Override 
     public void run() { 

      //your code here 



     } 
    }, 5, 1000);// First time start after 5 mili second and repead after 1 second 
相關問題