了解UI线程 [英] Understanding the UI thread

查看:252
本文介绍了了解UI线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个初学者到安卓和我有一些混乱关于 Android的UI线程。现在,我知道没有从创建用户界面可以修改它的一个线程分开。

大。
这里是活动从我的第一个Android应用程序,它稍微混淆了我。

 公共类NasaDailyImage延伸活动{
    公共ProgressDialog modalDialog = NULL;
// ------------------------------------------------ ------------------------------
    @覆盖
    保护无效的onCreate(包savedInstanceState){

        //实例化进度对话框,跳过细节。

        按钮B = //获取引用到按钮
        b.setOnClickListener(新OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                modalDialog.show(); //显示模式
                Toast.makeText(getApplicationContext(),获得饲料,500).show();
                新AsyncRetriever()执行(新IotdHandler()); //获取供稿!
            }
        });
    }
// ------------------------------------------------ ------------------------------
    市民同步无效resetDisplay(布尔parseErrorOccured,
        布尔imageErrorOccured,
        IotdHandler newFeeds){
        如果(parseErrorOccured || imageErrorOccured){
            //敬酒
            //不更新显示
        }其他{
            //敬酒
            //更新显示
            //根据新饲料
        }
    }
// ------------------------------------------------ ------------------------------
    类AsyncRetriever扩展的AsyncTask< IotdHandler,太虚,IotdHandler> {

        @覆盖
        保护IotdHandler doInBackground(IotdHandler ......为arg0){
            IotdHandler处理器= arg0中[0];
            handler.processFeed(); //获取RSS订阅源数据!
            返回的处理程序;
        }
// ------------------------------------------------ ------------------------------
        @覆盖
        保护无效onPostExecute(IotdHandler fromInBackground){
            resetDisplay(//调用更新显示
            fromInBackground.errorOccured,
            fromInBackground.imageError,
            fromInBackground);
        }
// ------------------------------------------------ ------------------------------


}
 


1. 的onCreate 是在UI线程上,所以我可以做我想做的事情,但的onClick 不是。 为什么我可以做一个 ProgressDialog 吐司在该方法?为什么没有出现错误?
2. 的AsyncTask 是在 NasaDailyImage 的子类。这意味着它可以访问所有 NasaDailyImage 的方法,包括 resetDisplay()用于更新显示。 resetDisplay()是所谓的 onPostExecute 运行在从用户界面不同的线程。那么,我为什么不能更新显示有而没有从中得到任何错误?

解决方案
  1. 的onClick()确实是在UI线程上。大多数在活动发生的事情会发生在UI线程上。

  2. onPostExecte()(及其对应在preExecute())运行在UI线程也是如此。该 AsyncTask.onPostExecte()文档明确指出这一点。 AsyncTask的故意设计,使得开发人员可以更新UI之前和之后,他们做后台工作。

在一般情况下,你的code将在UI线程上运行,除非你明确告诉它,否则。一旦你创建AsyncTasks,的Runnable,或线程,你需要确保你明白,你的code正在执行。在活动中,通常是安全的,假设你是在UI线程上。

I am a beginner to Android and I have some confusions regarding Android UI Thread. Now, I know that no thread apart from the one that created the UI can modify it.

Great.
Here is the Activity from my first Android app which slightly confuses me.

public class NasaDailyImage extends Activity{
    public ProgressDialog modalDialog = null;
//------------------------------------------------------------------------------
    @Override
    protected void onCreate(Bundle savedInstanceState){

        //Instantiate progress dialog, skipping details.

        Button b = //get reference to button
        b.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                modalDialog.show(); // show modal
                Toast.makeText(getApplicationContext(), "Getting feeds", 500).show();
                new AsyncRetriever().execute(new IotdHandler()); // Get the feeds !!
            }
        });
    }
//------------------------------------------------------------------------------
    public synchronized void resetDisplay(boolean parseErrorOccured, 
        boolean imageErrorOccured,
        IotdHandler newFeeds){
        if(parseErrorOccured || imageErrorOccured){
            // make a Toast
            // do not update display
        }else{
            // make a Toast
            // update display
            // based on new feed
        }
    }
//------------------------------------------------------------------------------
    class AsyncRetriever extends AsyncTask<IotdHandler,Void,IotdHandler>{

        @Override
        protected IotdHandler doInBackground(IotdHandler... arg0) {
            IotdHandler handler = arg0[0];
            handler.processFeed(); // get the RSS feed data !
            return handler;
        }
//------------------------------------------------------------------------------    
        @Override
        protected void onPostExecute(IotdHandler fromInBackground){
            resetDisplay( // call to update the display
            fromInBackground.errorOccured,
            fromInBackground.imageError,
            fromInBackground);
        }
//------------------------------------------------------------------------------


}  


1. onCreate is on the UI thread so I can do whatever I want but onClick is not. Why can I make a ProgressDialog and a Toast in that method? Why no error there?
2. The AsyncTask is subclass of the the NasaDailyImage. This means it can access all the methods of NasaDailyImage including resetDisplay() which updates the display. resetDisplay() is called in the onPostExecute which runs on a different thread from UI. So, why can I update the display there and yet get no errors ?

解决方案

  1. onClick() is indeed on the UI thread. Most of what happens in an Activity happens on the UI thread.

  2. onPostExecte() (and its counterpart onPreExecute()) runs on the UI thread as well. The AsyncTask.onPostExecte() documentation clearly states this. AsyncTask was deliberately designed such that developers could update the UI before and after they do background work.

In general, your code will be running on the UI thread unless you explicitly tell it otherwise. Once you create AsyncTasks, Runnables, or Threads, you need to ensure you understand where your code is executing. In an Activity, it is typically safe to assume you are on the UI thread.

这篇关于了解UI线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆