Several ways for Android to update UI through subthread
In general, the update of UI cannot be without Handler. First, let's understand the Handler mechanism:
Handler message mechanism
Definition
Message
The data unit for inter thread communication can carry the required data through message to create an object: Message.obtain(what)
Handler
Handler is the processor of Message, and it is also responsible for sending and removing messages
Send instant message: even send instant processing
Send delay message: instant, later processing
MessageQueue: message queue
It is used to store messages sent through the Handler. It is a priority queue sorted by Message when
Looper: circulator
It is responsible for recycling the current Message to be processed in the Message Queue and handing it to the corresponding Handler for processing. After processing, the Message is cached in the Message pool for reuse
Basic use of Handler
Steps:
① Create a Handler member variable and override its handleMessage()
② Create Message object in sub thread
③ Send Message using handler object
④ Processing messages in handleMessage()
Message mechanism principle
Character description
Get a message object from the handler, encapsulate the data into the message object, push the message to the MessageQueue queue through the send method of the handler, and the looper object will poll the MessageQueue and take out the message object. It is distributed to the handler through dispatchMessage, and then the message is processed by the handleMessage method implemented by the handler.
Next, let's look at some common ways to update UI through subthreads
Subthread update UI
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button update_handler; private Button update_ViewPost; private Button update_handlerPost; private Button update_handlerPostDelay; private Button update_RunOnUiThread; private Button update_AsyncTask; private TextView textView;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { update_handler=findViewById(R.id.button1); update_ViewPost=findViewById(R.id.button2); update_handlerPost=findViewById(R.id.button3); update_handlerPostDelay=findViewById(R.id.button4); update_RunOnUiThread=findViewById(R.id.button5); update_AsyncTask=findViewById(R.id.button6); textView=findViewById(R.id.myword); update_handler.setOnClickListener(this); update_ViewPost.setOnClickListener(this); update_handlerPost.setOnClickListener(this); update_handlerPostDelay.setOnClickListener(this); update_RunOnUiThread.setOnClickListener(this); update_AsyncTask.setOnClickListener(this); }
@Override public void onClick(View v) { switch (v.getId()){ case R.id.button1: update_handler(); break; case R.id.button2: update_ViewPost(); break; case R.id.button3: update_handlerPost(); break; case R.id.button4: update_handlerPostDelay(); break; case R.id.button5: update_RunOnUiThread(); break; case R.id.button6: new updateAsyncTask().execute(); break; } }
Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if(msg.what==1){ textView.setText("Little Mu Mu"); } } };
/*
Method 1
*/
private void update_handler(){ new Thread(new Runnable() { @Override public void run() { Message message=handler.obtainMessage(); message.what=1; handler.sendMessage(message); } }).start(); } /* Method 2 */ private void update_ViewPost(){ new Thread(new Runnable() { @Override public void run() { textView.post(new Runnable() { @Override public void run() { textView.setText("Small nine nine"); } });
} }).start(); } /* Method 3 */ private void update_handlerPost() { new Thread(new Runnable() { @Override public void run() { handler.post(new Runnable() { @Override public void run() { textView.setText("Sake"); } }); } }).start(); }
/*
Method 4
*/
private void update_handlerPostDelay(){ new Thread(new Runnable() { @Override public void run() { handler.postDelayed(new Runnable() { @Override public void run() { textView.setText("Nine wine"); } },3000); } }).start(); }
/*
Method 5
*/
private void update_RunOnUiThread(){ new Thread(new Runnable() { @Override public void run() { runOnUiThread(new Runnable() { @Override public void run() { textView.setText("Mu nine"); } }); } }).start(); }
/*
Method 6
*/
class updateAsyncTask extends AsyncTask<String,Integer,String>{ @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected String doInBackground(String... strings) { publishProgress(); return null; } @Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); } @Override protected void onPostExecute(String s) { super.onPostExecute(s); textView.setText("End"); } }
}
Layout file
<?xml version="1.0" encoding="utf-8"?>
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" >
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="handler" android:textAllCaps="false" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="View.post()" android:textAllCaps="false" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="handlerPost()" android:textAllCaps="false"/> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="handlerPostDelay()" android:textAllCaps="false"/> <Button android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="runOnUiThread()" android:textAllCaps="false"/> <Button android:id="@+id/button6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text=" update_AsyncTask()" android:textAllCaps="false"/> <TextView android:id="@+id/myword" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Mu nine wine"/>
Operation result:
summary
In fact, there is not much difference between the six methods. The first five methods are based on the encapsulation of the first method, which adopts the Thread+handler mode. The sixth method is more powerful, which is Thread+handler + ThreadPool.
Reference resources: https://blog.csdn.net/oheg2010/article/details/93092541
Original address https://www.cnblogs.com/dearnotes/p/12189643.html