Android asynchronous loading

Source: Internet
Author: User

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);     } 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.