2013-07-19 42 views
-2

我的Android應用程序凍結了幾秒鐘,當我嘗試將我的數據庫文件發送到我的遠程服務器時,有無論如何,我可以將其設置爲使用多線程或其他此類功能的後臺線程?如何添加多線程或其他類似的功能,以Android應用程序?

這是出現問我,如果我要發送

public void showYesNoBox(){ 
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 
     switch (which){ 
     case DialogInterface.BUTTON_POSITIVE: 
      //Yes button clicked 
      SendData(); 
      finish();//go back to the previous Activity 
     overridePendingTransition(R.anim.fadein, R.anim.fadeout); 

      break; 

     case DialogInterface.BUTTON_NEGATIVE: 
      //No button clicked 

      finish();//go back to the previous Activity 
     overridePendingTransition(R.anim.fadein, R.anim.fadeout); 
      break; 
     } 
    } 
}; 

AlertDialog.Builder builder = new AlertDialog.Builder(this, AlertDialog.THEME_HOLO_DARK); 
builder.setMessage("Do you want to send the data now?"); 
builder.setPositiveButton("Yes", dialogClickListener); 
builder.setNegativeButton("No", dialogClickListener); 
builder.show(); 
} 

,並在此對話框中的代碼的代碼在執行時我點擊「是」按鈕

public void SendData() { 

    File path = getDatabasePath(DataBaseHelper.DATABASE_NAME); 
    if (path.exists()) 
     copyDatabase(path); 
} 


/** 
* Copy the database to the sdcard 
* @param file 
*/ 
private void copyDatabase(File file) { 
    SimpleDateFormat dateFormat1 = new SimpleDateFormat("ddMMyy-HHmmss"); 
    String dateString = dateFormat1.format(new Date()); 
    String pathdest = getDir().getAbsolutePath() + Configuration.LOST_FOLDER + "/Database/"; 
    String pathdir = pathdest; 
    File dir = new File(pathdir); 
    if (!dir.exists()) 
     dir.mkdirs(); 

    String namefile = file.getName(); 
    int pos = namefile.lastIndexOf('.'); 
    if (pos != -1) { 
     String ext = namefile.substring(pos + 1); 
     String name = namefile.substring(0, pos - 1); 
     pathdest += name; 
     pathdest += "_" + dateString; 
     pathdest += ext; 
    } else { 
     pathdest += namefile; 
     pathdest += "_" + dateString; 
    } 

    File filedest = new File(pathdest); 
    try { 
     if (filedest.createNewFile()) 
      copyFile(file.getAbsolutePath(), pathdest); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

任何有關這個問題的幫助將不勝感激,因爲雖然問題並沒有阻止用戶能夠發送數據,但它試圖執行該操作時不願意在一個屏幕上停留幾秒鐘。

+0

檢查這個職位上多線程 http://stackoverflow.com/questions/1921514/how-to-run-a-runnable-thread-in-android – WhiskThimble

回答

2

while the problem doesn't prevent the user from being able to send the data it is anoying to be stuck on one screen for a few seconds while it attempts to execute the action.

你可以看到AsyncTask。它用於相同的目的。當您想要執行長時間運行的進程(包括服務器到服務器,執行數據庫內容等)時,用戶不需要停留在UI上。這可以使用AsyncTask在後臺完成。那裏有很多很好的教程。只是谷歌它。結帳Android Background Processing with Threads, Handlers and AsyncTask - TutorialWhat arguments are passed into AsyncTask<arg1, arg2, arg3>?。希望這可以幫助。

+0

揍我一拳:) –

+0

我會在什麼地方說它的URL? private class DownloadFilesTask extends AsyncTask { – simfrek

+0

您可以根據您的需求選擇'AsyncTask'的參數。它不一定是'URL'。如果你第一次閱讀它的文檔頁面或者做一些關於它的教程,對你會有幫助。 http://stackoverflow.com/questions/6053602/what-arguments-are-passed-into-asynctaskarg1-arg2-arg3後肯定會幫助你清除你的疑惑。 –

0

使用在AsyncTask

所有網絡操作,如下面的例子

private class UploadTask extends AsyncTask<URL, Integer, Long> { 
protected Long doInBackground(URL... urls) { 
    int count = urls.length; 
    long totalSize = 0; 
    // upload task 
    return totalSize; 
} 

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

protected void onPostExecute(Long result) { 
    showDialog("Downloaded " + result + " bytes"); 
} 
} 

一旦創建了任務執行得很乾脆:

new UploadTask().execute(url1, url2, url3); 
相關問題