我一直在使用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
這顯然告訴我蔭仍然在主線程中運行的發現。感謝您的幫助
嘗試更新和查看或ui項目在你使用的線程,如果錯誤,那麼它不是主線程或UI線程 – Meenal 2014-09-11 13:29:44
你的意思是嘗試更新視圖或UI項目? – 2014-09-11 13:40:45
ui item..like imageview或textview – Meenal 2014-09-11 13:41:20