Analysis of usages of handle class in Android _android

Source: Internet
Author: User

This example describes the use of the handle class in Android. Share to everyone for your reference. Specifically as follows:

When we are working on a download or other task that takes a long time to perform, if the process function is placed directly in the OnCreate or onstart of the activity, it will result in the execution being unresponsive and, if the time is too long, the program will be dead. Handler is the ability to put these functions into a separate thread to perform, with no effect on the activity.

When the user clicks on a button if the execution is a time-consuming operation, bad handling will lead to the system suspended animation, user experience is poor, and Android is further, if any one of the acitivity did not respond more than 5 seconds will be forced to close, Therefore, we need to start a thread to handle the long time operation, while the main thread is unaffected by it, sending messages to the main thread at the end of the time-consuming operation, and then processing the main thread accordingly. Then the message passing and asynchronous processing between threads is handler.

The following simulates a simple photo album viewer that automatically replaces the next picture every 2 seconds.

Main.xml Layout file:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
  android:orientation=" vertical "
  android:layout_width=" fill_parent "
  android:layout_" height= "Fill_parent" 
  android:gravity= "center" >
  <imageview android:id= "@+id/imageview"
    Android : layout_width= "fill_parent"
    android:layout_height= "wrap_content" 
    android:src= "@drawable/p1"
    android:gravity= "center"/>
</LinearLayout>

Handleactivity class:

Package com.ljq.handle;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.os.Message;
Import Android.widget.ImageView;
  public class Handleactivity extends activity {private ImageView ImageView = null; Private Handler Handler = new Handler () {@Override public void Handlemessage (msg) {switch (Msg.wha
        T) {case 0:imageview.setimageresource (R.DRAWABLE.P1);
      Break
        Case 1:imageview.setimageresource (R.DRAWABLE.P2);
      Break
        Case 2:imageview.setimageresource (R.DRAWABLE.P3);
      Break
        Case 3:imageview.setimageresource (R.DRAWABLE.P4);
      Break
    Super.handlemessage (msg);
  }
  };
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
    ImageView = (ImageView) Findviewbyid (R.id.imageview);
  Thread.Start ();
  int what = 0; Thread thRead = new Thread (new Runnable () {public void run () {while (true) {Handler.sendemptymessage (what++)
        % 4);
        try {thread.sleep (2000);
        catch (Interruptedexception e) {e.printstacktrace ();
}
      }
    }
  });

 }

Run Result:

I hope this article will help you with your Android program.

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.