Recently, I have been exposed to some processing pages with a large amount of data, so it is inevitable to use the asynchronous loading method.
Summary:
1. Handle + thread is the most popular method;
2. Use asynctask
3. activity. runonuithread (runnable)
View. Post (runnable)
View. postdelayed (runnable, long)
4. Use asyncqueryhandler
Examples are given respectively:
① Handle + thread mode
private Handler mHandler = new Handler(); public class MyThread extends Thread { Message msg = new Message(); mHandler.sendMessage(msg); }
② Asynctask
That is, if the volume of data in oncreate is large, and you don't want to use it for paging
New loaddata(cmd.exe cute (); private class loaddata extends asynctask <object, object, Object> {protected object doinbackground (object... params) {return NULL; // query data} protected void onpostexecute (string result) {mytext. settext (result); // refresh the page }}
③ View. Post (runnable)
For example, you can use the "Search for contacts" function on the dial to listen for changes in edittext.
private Handler searchListHandler = new Handler(); public void afterTextChanged(Editable s){ searchListHandler.post(runnable); } private Runnable runnable = new Runnable(){ public void run(){} };
Activity. runonuithread (runnable) and view. Post (runnable) are similar.
public void onClick( View v ) { new Thread( new Runnable() { public void run() { loadNetWork(); Activity.runOnUiThread(new Runnable() { myText.setText("Farmer is a good man!"); }); } }).start(); }
View. postdelayed (runnable, long) is passed to the runnable object in the postdelayed method and executed after the specified time.
Handler is executed in the associated thread.
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mHander.postDelayed(new MyThread(), 1000); }