2014-09-11 93 views
-1

我一直在使用android上的線程概念掙扎。我認爲下面的代碼是在不同的線程上運行到主UI線程,但我不是100%確定的,所以我認爲我會來澄清,因爲Android文檔沒有用我理解的任何語言編寫。下面是我的代碼。此代碼是否在與主UI線程不同的線程中執行

public void retrieveImages(final ImagePresenterInt imagepresenter) { 

     storedImages = new ArrayList<Image>(); 

     new Handler().postDelayed(new Runnable() { 
      @Override 
      public void run() { 

       File imagedir = new File("/mnt/shared/images"); 
       File [] myfiles = File.listRoots(); 
       String canread = String.valueOf(imagedir.canRead()); 
       String isfile = String.valueOf(imagedir.isFile()); 
       String isdir = String.valueOf(imagedir.isDirectory()); 

       Log.d("Can Read?","Canread from file :" + canread); 
       Log.d("is file?","Is a file ? :" + isfile); 
       Log.d("is dir?","Is a Dir file :" + isdir); 

       File [] rootfiles =myfiles[0].listFiles(); 

       for (File item : rootfiles) 
       { 
        Log.d(item.getAbsolutePath(),item.getName()); 
       } 

       if(Looper.myLooper() == Looper.getMainLooper()) 
       { 
        Log.d("main thread ?", "YES"); 
       } 
      } 
     }, 2000); 


} 

我對上述代碼的理解是我創建了一個處理程序。它與主線程或UI線程相關聯。它有一個消息隊列和一個與之關聯的活套。這段代碼被傳遞給消息隊列並由循環線程運行到主UI線程?我可能在這裏錯了。但主要是我想知道這是否在主線程上運行。如果不是,我將如何把它放到不同的線程上?我試圖驗證碼是在不同的線程使用運行的代碼我在這個問題

How to check if current thread is not main thread

這顯然告訴我蔭仍然在主線程中運行的發現。感謝您的幫助

+0

嘗試更新和查看或ui項目在你使用的線程,如果錯誤,那麼它不是主線程或UI線程 – Meenal 2014-09-11 13:29:44

+0

你的意思是嘗試更新視圖或UI項目? – 2014-09-11 13:40:45

+0

ui item..like imageview或textview – Meenal 2014-09-11 13:41:20

回答

0

處理程序在調用線程中創建,這可能是您的情況下的UI線程。如果你想開始一個新的線程,有三種可能性,我知道的:第一是簡單的開始一個新的線程:

thread = new Thread() { 
    @Override 
    public void run() { 
    //Do your thing here 
    } 
}; 
thread.start(); 

線程會死,如果你的活動就會被殺死。

二是定義一個IntentService:

public class SimpleIntentService extends IntentService { 

    public SimpleIntentService() { 
     super("SimpleIntentService"); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
    //Do your thing here 
    } 

,並通過

Intent intent = new Intent(this, SimpleIntentService.class); 
intent.putExtra("SomeString", "SomeValueYouNeed"); 
startService(intent); 

啓動它的IntentService將運行,直到onHandleIntent()做比接近本身。

第三種可能性是的AsyncTask:

private class TestTask extends AsyncTask<Datatype1, Datatype2, Datatype3>{ 
    protected Long doInBackground(Datatype1... params) { 
    // Do your thing here 
    } 

    protected void onProgressUpdate(Datatype2... progress) { 
     //Do a Progress-Bar or something like that 
    } 

    protected void onPostExecute(Datatype3 result) { 
     //Do something, when your work is done 
    } 
} 

而在你的活動:

new TestTask().execute(params); 

的文檔說明你不應該使用異步任務很長calulations,但我不清楚爲什麼。如果你使用Asynctask而不是Intentservice,那麼將數據返回到UI-Thread可能會更容易,但我自己並不經常使用它們,所以我可能不是最好的人在這裏問。

編輯:我忘了這個: IntentService對你傳遞的每一個ntent執行一次,Asynctask只能調用一次。

此外IntentService必須在Manifest中聲明。

+0

我認爲asyncTask類會導致活動與您調用線程的任何類緊密耦合,並且Iam嘗試使用MVP模式,因此完全排除。加上還有各種其他問題與Asynctask – 2014-09-11 14:07:26

+0

我想在這種情況下IntentService(或一個正常的服務與線程中,如果你的任務應該是可以從外部停止)是要走的路。 – 2014-09-11 14:21:23

1

您在retrieveImages()中創建的Handler綁定到調用此函數的線程。

The doc on Handler說:

默認的構造同夥這個處理器與樂句當前線程。如果此線程沒有活套,則此處理程序將無法接收消息,因此引發異常。

因此,如果從UI線程調用retrieveImages(),其中創建的Handler也綁定到UI線程。

更新:如果您希望您的代碼在不同的線程中執行,最簡單的方法是使用AsyncTask

+0

是的,retrieveImages()從UI線程調用來定義 – 2014-09-11 13:39:47

+0

@ user1232726那麼它是否回答你的問題?如果你需要在不同的線程中運行,你可以使用'AsyncTask' - 查看答案更新。 – 2014-09-11 13:44:41

+0

asyncTask導致thight耦合。無論如何,我已經發現它的方式。不要認爲不適當使用它 – 2014-09-11 14:19:18

相關問題