Android工作室每5秒更新一次textview

时间:2016-10-02 04:53:35

标签: android

我的应用程序需要来自数据库的实时数据,并且我将其发布在TextView上,并且我无法在数据库更新时更新TextView。我尝试使用Timer,但它仍然相同。

这是我的代码,

public void startTimer() {
    //set a new Timer
    timer = new Timer();

    //initialize the TimerTask's job
    initializeTimerTask();

    timer.schedule(timerTask, 0, 5000);
}

private void stopTimerTask() {
    //stop the timer, if it's not already null
    if (timer != null) {
        timer.cancel();
        timer = null;
    }
}

public void initializeTimerTask() {
    timerTask = new TimerTask() {
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    final AcceptCars Cars = (AcceptCars) getIntent().getSerializableExtra("cars");
                    renterLat.setText(Cars.renterLat);
                    renterLng.setText(Cars.renterLng);
                    Log.d(TAG,renterLat.getText().toString());
                    Log.d(TAG,renterLng.getText().toString());
                }
            });
        }
    };
}

这是我得到Cars.renterLatCars.renterLng

的地方
public class AcceptCars implements Serializable {

    @SerializedName("renterLat")
    public String renterLat;

    @SerializedName("renterLng")
    public String renterLng;

}

3 个答案:

答案 0 :(得分:0)

这是你应该遵循的逻辑。我使用的是Handler而不是Timer。在run方法中,您需要调用Web服务并从db获取更新的值。使用runOnUiThreadThread更新UI的值。

请参阅下面的代码,

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Handler taskHandler = new Handler();
    taskHandler.postDelayed(myTask, 0);
}

private Runnable myTask = new Runnable(){
        public void run() {

            queryDb();

            // repeat the task
            taskHandler.postDelayed(this, 1000);
        }
};

private void queryDb(){
    new Thread(new Runnable() {
        @Override
        public void run() {

            // call you webservice
            String data = callWebservice();

            // parse the data in to AcceptCars pojo class
            AcceptCars Cars = parseData(data);

            //update the UI
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    renterLat.setText(Cars.renterLat);
                    renterLng.setText(Cars.renterLng);
                }
            });
        }
    }).start();
}

答案 1 :(得分:0)

TimerTask很难与IMO打交道。您应该使用Handler并在一段时间后致电postDelayed来做某事。

或者,您可以尝试我写的这个计时器类:

import android.os.Handler;

public class Timer {
    private Handler handler;
    private boolean paused;

    private int interval;

    private Runnable task = new Runnable () {
        @Override
        public void run() {
            if (!paused) {
                runnable.run ();
                Timer.this.handler.postDelayed (this, interval);
            }
        }
    };

    private Runnable runnable;

    public int getInterval() {
        return interval;
    }

    public void setInterval(int interval) {
        this.interval = interval;
    }

    public void startTimer () {
        paused = false;
        handler.postDelayed (task, interval);
    }

    public void stopTimer () {
        paused = true;
    }

    public Timer (Runnable runnable, int interval, boolean started) {
        handler = new Handler ();
        this.runnable = runnable;
        this.interval = interval;
        if (started)
            startTimer ();
    }
}

使用起来非常简单。

你可以像这样使用它:

Timer timer = new Timer(new Runnable() {
    public void run() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                final AcceptCars Cars = (AcceptCars) getIntent().getSerializableExtra("cars");
                renterLat.setText(Cars.renterLat);
                renterLng.setText(Cars.renterLng);
                Log.d(TAG,renterLat.getText().toString());
                Log.d(TAG,renterLng.getText().toString());
            }
        }
    }
 }, 5000, true);

答案 2 :(得分:0)

你甚至可以使用倒数计时器。 这是链接https://developer.android.com/reference/android/os/CountDownTimer.html