2013-06-12 46 views
0

我是android編程的新手。我創建了一個應用程序,如果點擊掃描按鈕,它會顯示訪問點列表。我想每10秒重複一次掃描,並將屏幕上顯示的數據記錄到SD卡/內部存儲器中的.csv文件中以備後用。我該怎麼做?你能給我一個示例代碼嗎?在android中重複執行任務和記錄數據

回答

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"); 
    } 
} 
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 

}