In Android, the sub-thread uses the components in the main thread to solve the problem. The android thread
In Android, components in the main thread cannot be called by the quilt thread; otherwise, an exception occurs.
The method used here is to useHandler classInCallback (), In the receiving threadMessage classSend the message, and then submit the function to be executed in the threadHandler class. In this way, the thread problems are solved.
The function of the following example is to click an image. The image transparency changes to 50,300 milliseconds and then becomes opaque. The Code is as follows:
Public class Demo extends Activity {private ImageView changeImg = null; // ImageView component Handler handler = new Handler (new Handler. callback () {@ Override public boolean handleMessage (Message msg) {if (msg. what = 0) {*** // ID of the Message *** changeImg. setImageAlpha (255); ** // set the image transparency to 255, that is, opacity **} return false ;}); @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanc EState); setContentView (R. layout. activity_main); ** changeImg = (ImageView) findViewById (R. id. change_img); ** // bind the image component} private class OnClick implements View. onClickListener {// inherited click listening class @ Override public void onClick (View v) {changeImg. setImageAlpha (50); // sets the changeImg transparency Toast. makeText (MainActivity. this, "this function has not yet been developed. Please wait! ", Toast. LENGTH_SHORT ). show (); new Thread () {// The status of changeImg is resumed with a delay. public void run () {try {sleep (300 ); // Message msg = new Message (); // declare and instantiate a Message class to notify Handler to execute the function msg. what = 0; // ID of the msg Object handler. sendMessage (msg); // send the msg object. This will execute the Callbak () event} catch (InterruptedException e) {// throw an exception e. printStackTrace ();}}}. start ();}}