2014-09-11 122 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

相关问题