We often do some time-consuming operations in background threads, such as fetching data from the network. However, when the data is retrieved and needs to be displayed on the page, there will be some troubles, because we all know that the UI page of android is not allowed to be directly operated in other threads. The following summarizes the 4 methods used to operate the UI interface in the thread.
Simulate time-consuming operations
private void connectNet() throws InterruptedException { Thread.sleep(2000); }
Method 1: Handler
In the child thread, the event is sent through the Handler's sendMessage (msg):
private void method1() { new Thread(new Runnable() { @Override public void run() { try { // Time -consuming operation connectNet(); // Send message to Handler mHadndler.sendEmptyMessage(111 ); } catch (InterruptedException e) { e.printStackTrace (); } } }).start(); }
Method 2: View.post (Runnable)
private void method2() { myText.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Thread(new Runnable() { @Override public void run() { try { // time-consuming operation connectNet(); myText.post(new Runnable() { @Override public void run() { myText.setText( "End of networking, update UI data" ); } }); } catch (InterruptedException e) { e.printStackTrace (); } } }).start(); } }); }
Method 3: Activity.runOnUiThread
Similar to view.post
private void method3() { new Thread(new Runnable() { @Override public void run() { try { // time-consuming operation connectNet(); runOnUiThread(new Runnable() { @Override public void run() { myText.setText("runOnUiThread..."); } }); } catch (InterruptedException e) { e.printStackTrace (); } } }).start(); }
Method 4: AsyncTask
private void method4() { // execute new LoadTask().execute("www.91dota.com" ) in UI thread } private class LoadTask extends AsyncTask { protected void onPostExecute(String result) { myText.setText(result); // Get the information from the network to refresh the page } protected String doInBackground(Object[] objects) { return "..."; // Background time-consuming operation } }
===========================》
Attach the code and layout
MainActivity.java
public class MainActivity extends AppCompatActivity { private TextView myText; private Handler mHadndler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); myText.setText( "Received a message..." + msg.what); } }; @Override protected void onCreate (Bundle savedInstanceState) { super .onCreate (savedInstanceState); setContentView(R.layout.activity_main); myText = findViewById(R.id.myTextView); } /** * Method 1: Handler */ private void method1() { new Thread(new Runnable() { @Override public void run() { try { // Time -consuming operation connectNet(); // Send message to Handler mHadndler.sendEmptyMessage(111 ); } catch (InterruptedException e) { e.printStackTrace (); } } }).start(); } /** * Method 2: Through View.post(Runnable) */ private void method2() { myText.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Thread(new Runnable() { @Override public void run() { try { // time-consuming operation connectNet(); myText.post(new Runnable() { @Override public void run() { myText.setText( "End of networking, update UI data" ); } }); } catch (InterruptedException e) { e.printStackTrace (); } } }).start(); } }); } /** * Method 3: Activity.runOnUiThread (Runnable) */ private void method3() { runOnUiThread(new Runnable() { @Override public void run() { try { // time-consuming operation connectNet(); myText.setText("runOnUiThread..."); } catch (InterruptedException e) { e.printStackTrace (); } } }); } /** * Method 4: AsyncTask */ private void method4() { // execute new LoadTask().execute("www.91dota.com" ) in UI thread } private class LoadTask extends AsyncTask { protected void onPostExecute(String result) { myText.setText(result); // Get the information from the network to refresh the page } protected String doInBackground(Object[] objects) { return "..."; // Background time-consuming operation } } private void connectNet() throws InterruptedException { Thread.sleep(2000); } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 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"> <TextView android:id="@+id/myTextView" android:layout_width="match_parent" android:layout_height="50dp" android:layout_margin="10dp" android:background="#dea" android:gravity="center" android:text="Hello World!" /> </LinearLayout>
Reference: http://www.it165.net