Android UI update
Anyone who has done Android development has encountered such a problem: with the change of demand, some entry interfaces will have problems such as UI increase or decrease, content changes and jump interface changes. Here are several methods to achieve UI. 's update.
1、Activity的 runOnUiThread
textView = (TextView) findViewById( R.id.tv ); new Thread(new Runnable() { @Override public void run() { runOnUiThread(new Runnable() { @Override public void run() { textView.setText("Update UI"); } }); } }).start();
The android Activity runOnUiThread() method uses
2 、 Handles sendEmptyMessage ()
package lib.com.myapplication; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private TextView textView ; Handler handler = new Handler () { @Override public void handleMessage(Message msg) { super.handleMessage(msg); textView.setText( "Ui updated"); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById( R.id.tv ); new Thread(new Runnable() { @Override public void run() { try { Thread.sleep( 2000 ); } catch (InterruptedException e) { e.printStackTrace (); } handler.sendEmptyMessage (2); } }).start(); } }
3、Handler post()
package lib.com.myapplication; import android.os.Bundle; import android.os.Handler; import android.support.v7.app.AppCompatActivity; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private TextView textView ; Handler handler = new Handler (); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById( R.id.tv ); new Thread(new Runnable() { @Override public void run() { try { Thread.sleep( 2000 ); } catch (InterruptedException e) { e.printStackTrace (); } handler.post(new Runnable() { @Override public void run() { textView.setText( "Ui updated"); } }) ; } }).start(); } }
Switch to main thread in child thread
new Thread(new Runnable() { @Override public void run() { LogUtil.d( "ttt 11111111111" + Thread.currentThread().getName() ); new Handler(Looper.getMainLooper()).post(new Runnable() { @Override public void run() { LogUtil.d( "ttt 55555555" + Thread.currentThread().getName() ); } }); LogUtil.d( "ttt 22222222222" + Thread.currentThread().getName() ); LogUtil.d( "ttt 33333333333" + Thread.currentThread().getName() ); LogUtil.d( "ttt 44444444444" + Thread.currentThread().getName() ); } }).start();
result
ttt 11111111111Thread-155 ttt 22222222222Thread-155 ttt 33333333333Thread-155 ttt 44444444444Thread-155 ttt 55555555main
It can be seen that this method can quickly switch threads. From the log log, switching to the main thread will not block the child thread.
4、view Post()
textView = (TextView) findViewById( R.id.tv ); new Thread(new Runnable() { @Override public void run() { try { Thread.sleep( 2000 ); } catch (InterruptedException e) { e.printStackTrace (); } textView.post(new Runnable() { @Override public void run() { textView.setText( "Ui updated"); } }) ; } }).start();
Summarize:
1. In fact, the above four methods can be attributed to one method: handler is used for communication between Android threads.
2. Why does android require UI operations to be performed only on the UI thread? Mainly to avoid concurrency problems caused by multithreading. It is safe to operate the UI on a single thread