How do we use runOnUiThread in Android?

前端 未结 12 922
太阳男子
太阳男子 2020-11-22 00:09

I\'m new to Android and I\'m trying to use the UI-Thread, so I\'ve written a simple test activity. But I think I\'ve misunderstood something, because on clicking the button

12条回答
  •  耶瑟儿~
    2020-11-22 00:52

    There are several techniques using of runOnUiThread(), lets see all

    This is my main thread (UI thread) called AndroidBasicThreadActivity and I'm going to update it from a worker thread in various ways -

    public class AndroidBasicThreadActivity extends AppCompatActivity
    {
        public static TextView textView;
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_android_basic_thread);
    
            textView = (TextView) findViewById(R.id.textview);
    
            MyAndroidThread myTask = new MyAndroidThread(AndroidBasicThreadActivity.this);
            Thread t1 = new Thread(myTask, "Bajrang");
            t1.start();
        }
    }
    

    1.) By passing Activity's instance as an argument on worker thread

    class MyAndroidThread implements Runnable
    {
        Activity activity;
        public MyAndroidThread(Activity activity)
        {
            this.activity = activity;
        }
        @Override
        public void run()
        {
    
            //perform heavy task here and finally update the UI with result this way - 
            activity.runOnUiThread(new Runnable()
            {
                @Override
                public void run()
                {
                    AndroidBasicThreadActivity.textView.setText("Hello!! Android Team :-) From child thread.");
                }
            });
        }
    }
    

    2.) By using View's post(Runnable runnable) method in worker thread

    class MyAndroidThread implements Runnable
    {
        Activity activity;
        public MyAndroidThread(Activity activity)
        {
            this.activity = activity;
        }
        @Override
        public void run()
        {
         //perform heavy task here and finally update the UI with result this way - 
           AndroidBasicThreadActivity.textView.post(new Runnable()
          { 
            @Override
            public void run()
            {
                AndroidBasicThreadActivity.textView.setText("Hello!! Android Team :-) From child thread.");
            }
        });
    
        }
    }
    

    3.) By using Handler class from android.os package If we don't have the context (this/ getApplicationContext()) or Activity's instance (AndroidBasicThreadActivity.this) then we have to use Handler class as below -

    class MyAndroidThread implements Runnable
    {
        Activity activity;
        public MyAndroidThread(Activity activity)
        {
            this.activity = activity;
        }
        @Override
       public void run()
      {
      //perform heavy task here and finally update the UI with result this way - 
      new Handler(Looper.getMainLooper()).post(new Runnable() {
            public void run() {
                AndroidBasicThreadActivity.textView.setText("Hello!! Android Team :-) From child thread.");
            }
        });
      }
    }
    

提交回复
热议问题