Android development example of Thread + Handler (hamster) and androidhandler

Source: Internet
Author: User

Android development example of Thread + Handler (hamster) and androidhandler
Directly Add code

Package com. mingrisoft; import java. util. random; import android. app. activity; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. view. motionEvent; import android. view. view; import android. view. view. onTouchListener; import android. widget. imageView; import android. widget. toast; public class MainActivity extends Activity {private int I = 0; // records the number of hamster private ImageVi Ew mouse; // declare an ImageView object private Handler handler; // declare a Handler object public int [] [] position = new int [] [] {231,325 }, {424,349 },{ 521,256 },{ 543,296 },{ 719,245 },{ 832,292 },{ 772,358 }}; // create an array @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); mouse = (ImageView) findViewById (R. id. imageView1 );// Obtain the ImageView object mouse. setOnTouchListener (new OnTouchListener () {@ Overridepublic boolean onTouch (View v, MotionEvent event) {v. setVisibility (View. INVISIBLE); // set the mouse to not display I ++; Toast. makeText (MainActivity. this, "hit [" + I + "] only the hamster! ", Toast. LENGTH_SHORT ). show (); // display the Message prompt box return false ;}}); handler = new Handler () {@ Overridepublic void handleMessage (Message msg) {int index = 0; if (msg. what = 0x101) {index = msg. arg1; // obtain the location index value mouse. setX (position [index] [0]); // you can specify the x-axis position for mouse. setY (position [index] [1]); // you can specify the Y axis for mouse. setVisibility (View. VISIBLE); // set the mouse display} super. handleMessage (msg) ;}}; Thread t = new Thread (new Runnable () {@ Overridepu Blic void run () {int index = 0; // create an index value for recording the location of the hamster while (! Thread. currentThread (). isInterrupted () {index = new Random (). nextInt (position. length); // generate a random number Message m = handler. obtainMessage (); // get a Messagem. what = 0x101; // sets the Message ID m. arg1 = index; // Save the index value handler at the location of the cursor. sendMessage (m); // send the message try {Thread. sleep (new Random (). nextInt (500) + 500); // sleep for a period of time} catch (InterruptedException e) {e. printStackTrace () ;}}}); t. start (); // enable thread }}

Layout file:

<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/fl"    android:background="@drawable/background"    android:layout_width="fill_parent"    android:layout_height="fill_parent">    <ImageView        android:id="@+id/imageView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/mouse" /></FrameLayout>



In Android Thread programming, an error occurs when I use Toast to output information in the Thread run () method.

Note: Toast. makeText (ThreadActivity. this, "toast", Toast. LENGTH_SHORT). show ();
It is to be called in the main thread, that is, in the thread where ThreadActivity. this is located.
Directly corresponds to the context.

How to Use android Thread and Handler

There is a problem with the way I write it. I should not remove the time-consuming data operations from handler, and put the time-consuming operations directly in new Thread, this new thread completes the last step to send handler. sendMessage (message) tells handler what to do to update the UI, and then only updates the UI in Handler. Thank you very much for the number of people upstairs ~
 

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.