每5秒向Web服务发送一次请求

Rıd*_*van 7 android

我想听sql server数据库知道android中是否有数据更改所以我想每5秒向web服务发送一次请求以了解新的数据值.我该怎么做?你能举个例子吗?

My *_*God 17

你可以用AsyncTask做到,

public void callAsynchronousTask() {
    final Handler handler = new Handler();
    Timer timer = new Timer();
    TimerTask doAsynchronousTask = new TimerTask() {       
        @Override
        public void run() {
            handler.post(new Runnable() {
                public void run() {       
                    try {
                        PerformBackgroundTask performBackgroundTask = new PerformBackgroundTask();
                        // PerformBackgroundTask this class is the class that extends AsynchTask 
                        performBackgroundTask.execute();
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                    }
                }
            });
        }
    };
    timer.schedule(doAsynchronousTask, 0, 50000); //execute in every 50000 ms
}
Run Code Online (Sandbox Code Playgroud)

更多:如何在固定的时间间隔后重复执行异步任务