0

我是android编程的新手。我创建了一个应用程序,如果单击扫描按钮,它会显示接入点列表。我想每 10 秒重复一次扫描,并将屏幕上显示的数据记录到 sdcard/内部存储器中的 .csv 文件中以供进一步使用。我该怎么做?你能给我一个示例代码吗?

4

2 回答 2

0

您可以使用计时器

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

}

于 2013-06-12T09:37:27.930 回答
0

您正在寻找 AsyncTask:http: //developer.android.com/reference/android/os/AsyncTask.html

 private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
             // Escape early if cancel() is called
             if (isCancelled()) break;
         }
         return totalSize;
     }

     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     }

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
 }
于 2013-06-12T09:14:22.497 回答