Will handler.post(new Runnable());在Android中创建新线程?

Will handler.post(new Runnable());在Android中创建新线程?,android,handler,runnable,ui-thread,Android,Handler,Runnable,Ui Thread,我写了一个小应用程序,每3秒更改一次应用程序背景。我使用Handler和Runnable对象来实现这一点。很好用。这是我的密码: public class MainActivity extends Activity { private RelativeLayout backgroundLayout; private int count; private Handler hand = new Handler(); @Overr

我写了一个小应用程序,每3秒更改一次应用程序背景。我使用Handler和Runnable对象来实现这一点。很好用。这是我的密码:

  public class MainActivity extends Activity {

        private RelativeLayout backgroundLayout;
        private int count;
        private Handler hand = new Handler();

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            Button clickMe = (Button) findViewById(R.id.btn);

            backgroundLayout = (RelativeLayout) findViewById(R.id.background);

            clickMe.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {

                    count = 0;

                    hand.postDelayed(changeBGThread, 3000);

                }
            });

        }

private Runnable changeBGThread = new Runnable() {

        @Override
        public void run() {

            if(count == 3){
                count = 0;
            }

            switch (count) {
            case 0:
                backgroundLayout.setBackgroundColor(getResources().getColor(android.R.color.black));
                count++;
                break;

            case 1:
                backgroundLayout.setBackgroundColor(Color.RED);
                count++;
                break;

            case 2:
                backgroundLayout.setBackgroundColor(Color.BLUE);
                count++;
                break;

            default:
                break;
            }

             hand.postDelayed(changeBGThread, 3000);

        }
    };
}

这里我在非UI线程中更改UI背景,即
backgroundLayout.setBackgroundColor(Color.RED)内部运行();它是如何工作的?

可运行线程不是后台线程,它是可以在给定线程中运行的工作单元

处理程序不创建新线程,它绑定到创建它的线程的循环器(在本例中是主线程),或者绑定到构造过程中提供给它的循环器


因此,您没有在后台线程中运行任何东西,您只是在处理程序上排队等待稍后在主线程上运行的消息

谢谢,正如我在这里读到的,默认情况下线程没有与之关联的消息循环,我们必须通过调用prepare()来创建它。那么主线程是否默认有Looper,bcoz我没有在我的应用程序中创建任何Looper。是的,主ui线程有一个与其关联的Looper,您可以通过Looper.getMainLooper()获得它(如果您想检查处理程序绑定的是哪个Looper,这很有用)