0

如何将数据更新到服务器?我已经使用了下面的代码,但它在 10 分钟后没有执行。

ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
        executor.scheduleWithFixedDelay(new Runnable(){

            public void run() {

//update data to server

            }

    }, 0, 600, TimeUnit.SECONDS);
4

2 回答 2

2

您必须使用自己的线程。这是使用 AsyncTask 的解决方案....

所有代码都放在您的 Activity 类中。

public void toCallAsynchronous() {
                    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 {

        MyAsyncTask task = new MyAsyncTask();
        task.execute(txtSearchField.getText().toString());


                                }
                            });
                        }
                    };
    timer.schedule(doAsynchronousTask, 0, 2000); // execute in every 2 second

}

// 异步任务类

private class MyAsyncTask extends AsyncTask<String, Object, List<ModelObject>> {

               @Override
               protected List< ModelObject > doInBackground(String... params) {

               // Call web service
               return null;

                }

                @Override
                protected void onPostExecute(List< ModelObject > result) {
                        super.onPostExecute(rezultat);
                        // Update UI
                }            
}
于 2013-01-30T15:37:57.553 回答
0

试试这个

 private static final ScheduledExecutorService worker = Executors
       .newSingleThreadScheduledExecutor();
 worker.schedule(new Runnable(){
        public void run() {
 //update data to server
        }, 600, TimeUnit.SECONDS);
于 2013-01-30T15:09:48.020 回答