2017-02-22 94 views
-2

我是新的android和我問一個關於我創建的線程的問題。我認爲這是一個愚蠢的問題,但我很抱歉。我有一個onClick按鈕監聽器。它的工作是獲取URL下載鏈接並存儲在一個變量中。延遲運行線程

/** 
* this method invoke from setPositiveButton's dialog 
* 
* @param rootView 
*/ 
private void addURLToList(View rootView) { 

    editTextAddURL = (EditText) rootView.findViewById(R.id.editText_add_url); 

    Log.i("===", "addURLToList: " + editTextAddURL.getText()); 
    stringUrl = editTextAddURL.getText().toString(); 

    *start GetSizeOfFile thread for getting size file and store 
    * in lenghtOfFile variable 
    */ 
    new GetSizeOfFile().start(); 

    Log.i("====", "size of file after Thread: " + lenghtOfFile); 


} 

我創建了一個線程,因爲我想獲取文件大小。

private class GetSizeOfFile extends Thread { 
    @Override 
    public void run() { 
     super.run(); 
     try { 
      URL url = new URL(stringUrl); 
      HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
      lenghtOfFile = connection.getContentLength(); 
      Log.i("====", "size of file in Thread: " + lenghtOfFile); 

     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } 
} 

一切正常,但是當線程啓動,幾秒鐘後,我的lenghtOfFile變量初始化,我在lenghtOfFile在此行中得到0 Log.i("====", "size of file after Thread: " + lenghtOfFile); 這是我的logcat:

02-22 10:02:11.352 11333-11333/com.example.manifest.simplefiledownloadmanager I/===: addURLToList: http://dl2.soft98.ir/soft/a/Adobe.Shockwave.Player.12.2.7.197.IE.rar 
02-22 10:02:11.352 11333-11333/com.example.manifest.simplefiledownloadmanager I/====: file name : Adobe.Shockwave.Player.12.2.7.197.IE.rar 
02-22 10:02:11.352 11333-11333/com.example.manifest.simplefiledownloadmanager I/====: size of file after Thread: 0 
02-22 10:02:36.544 11333-11495/com.example.manifest.simplefiledownloadmanager I/====: size of file in Thread: 13524394 

我想從線程獲得文件的大小first.is它正確的,我必須睡覺的線程或退出標準的方式嗎?對不起,我是新的android

+0

像往常一樣...創建回調,從「線頭」呼回調,做這在calback需要從線程結果工作人員...等待線程'addURLToList'內部的結果使得Thread無用(是的,我知道NetworkOnMainThreadExcpetion ...但是這就是爲什麼它被製作...因爲沒有阻塞主線程) – Selvin

+0

我不知道爲什麼有些人給減一些帖子?在android中新的如果你不想回答我然後不回答,爲什麼給減號! –

+0

@謝爾文謝謝你。你能發送關於你的方式的教程鏈接嗎? –

回答

0

當使用線程你不能假定他們的執行順序。

你的情況發生了什麼我認爲,當你的新線程正在等待建立的連接時,原始線程將與未初始化的lenghtOfFile變量一起運行,因此日誌看起來就像它一樣。另一種可能性是,當lenghtOfFile=0行被記錄時,新線程甚至沒有開始運行。這就是線程的工作方式。

爲了確切的目的,ASyncTask類存在於Android中。 你的代碼應該是有點這樣的:

private class GetSizeOfFile extends AsyncTask<String, Void, Long> { 

    // runs on a background thread 
    protected Long doInBackground(String... stringUrls) { 
     String stringUrl = stringUrls[0]; 
     try { 
      URL url = new URL(stringUrl); 
      HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
      long lenghtOfFile = connection.getContentLength(); 
      return lenghtOfFile; 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return -1; 
    } 

    // runs on main thread 
    protected void onPostExecute(Long lenghtOfFile) { 
     if (lenghtOfFile == -1) { 
      // something went wrong 
     } else { 
      Log.i("====", "size of file: " + lenghtOfFile); 
      // whatever else you want to do 
     } 
    } 
} 

new GetSizeOfFile().execute(stringUrl);