如何在 android 中每 30 秒执行一次查询?

how to perform a query every 30 seconds in android?

我有一个查询,我想每 30 秒执行一次并将其记录到 Logcat。我是由处理程序完成的,但没有得到回复。

这是我的代码:

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();

如果有人能帮助我,我将不胜感激。

创建计时器的最佳方式是使用计时器 class

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