Daily Development and Application of Android Handler message mechanism and code Testing

Source: Internet
Author: User

Daily Development and Application of Android Handler message mechanism and code Testing

In many cases, we need to create and maintain a View for each component or all UI threads. For example, to update the display of a TextView

We cannot directly create sub-threads in the UI thread. We need to use the message mechanism: handler

This blog will show you the Handler we are familiar with, along with an example to verify the message mechanism of Handler. Through the processing of sub-threads, Handler implements

Operations such as updating the UI

Private TextView time; private Handler handler = new Handler () {public void handleMessage (Message msg) {if (msg. what = Reflsh) {time. setText (String. valueOf (msg. obj);} super. handleMessage (msg) ;};}; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); text = getResources (). getStringArray (R. array. array); for (int I = 0; I
 
  
Map = new HashMap
  
   
(); Map. put ("img", icon [I]); map. put ("name", text [I]); list. add (map);} adapter = new SimpleAdapter (getActivity (), list, R. layout. log, new String [] {"img", "name"}, new int [] {R. id. g_icon, R. id. g_text}) ;}@ Override public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View view = inflater. inflate (R. layout. fragment_shouye, container, false); gridview = (GridView) view. findViewById (R. id. gridView); time = (TextView) view. findViewById (R. id. time); // time ImageView icon = (ImageView) view. findViewById (R. id. icon); // TextView name = (TextView) view. findViewById (R. id. name); // nickname // File file = new File (Environment. getExternalStorageDirectory () + "/" + "weiinfo"); BufferedReader reader = null; String line = null; try {reader = new BufferedReader (new FileReader (file )); line = reader. readLine ();} catch (Exception e) {e. printStackTrace ();} data = line. split ("\ *"); Picasso. with (getActivity (). getApplicationContext ()). load (data [5]). into (icon); name. setText (data [0]); new Thread (new Runnable () {@ Overridepublic void run () {try {while (true) {Thread. sleep (1000); Message msg = new Message (); msg. what = Reflsh; msg. obj = new Date (System. currentTimeMillis ()). toLocaleString (); Log. d ("XXXXXXXXXXXXXX", msg. obj. toString (); handler. sendMessage (msg) ;}} catch (Exception e) {Log. d ("exception", "" + e );}}}). start ();
  
 

The Handler code tested above is a dynamic Beijing time demonstration from the MaoZhuaWeiBo homepage interface. I wrote a Handler to process the new sub-thread, in the sub-thread, I perform the following operations every second to obtain the current system time of the simulator and send the message to Handler for processing. Handler synchronously updates the message update view without blocking the main UI, this is the benefit of the Handler message mechanism!


Message Mechanism printing result:



The print result will be stopped until the application is closed. This is the Handler message mechanism ~


Related Article

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.