Android UI Master process communicates directly with child processes

Source: Internet
Author: User

Package Com.example.threadcomm;

Import Android. R.integer;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.os.Looper;
Import Android.os.Message;
Import Android.util.Log;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.TextView;

public class Mainactivity extends Activity {
/** called when the activity is first created. */
The main thread sends a message to a child thread
Button Verifybutton;
TextView TextView;

private static int from_main_msg1 = 1;
private static int from_sub_msg2 = 11;

Main thread
Mainactivityhandler MainHandler;
Other threads
Subthreadhandler Subthreadhandler;

private int i = 0;

@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);

Verifybutton = (Button) Findviewbyid (R.id.verify_button);
TextView = (TextView) Findviewbyid (R.id.text);

Textview.settext ("");

Verifybutton.setonclicklistener (Clicklistener);

Creating Child Threads
MyThread thread = new MyThread ();
Thread.Start ();
}

Onclicklistener Clicklistener = new Onclicklistener () {

@Override
public void OnClick (View v) {
Switch (V.getid ()) {
The main thread sends a message to a child thread
Case R.id.verify_button:
Textview.append ("UI main thread:shall Send message to sub-thread......\n\n");
Subthreadhandler.sendemptymessage (1);
Break

Default
Break
}
}
};

Main thread Processing message

Class Mainactivityhandler extends Handler {

Public Mainactivityhandler (Looper Looper) {
Super (Looper);
}

@Override
public void Handlemessage (Message msg) {
Super.handlemessage (msg);

if (msg.what = = 11) {
Textview.append ("UI main thread:received message from sub-thread......\n\n");
}
}
}

Here are the sub-threading classes and his handler class
Class MyThread extends Thread {
@Override
public void Run () {
LOG.E ("Threads_comm", "Sub thread is created here ...");

Super.run ();
Creates a Looper object for the thread that receives the message
Looper.prepare ();
The looper created by the thread's handler indicates that the message receiver is a child thread
Subthreadhandler = new Subthreadhandler (Looper.mylooper ());
Loops the message from the MessageQueue.
Looper.loop ();
}
}

Handler of child threads

Class Subthreadhandler extends Handler {
Public Subthreadhandler (Looper Looper) {
Super (Looper);
}

@Override
public void Handlemessage (Message msg) {
Super.handlemessage (msg);

if (msg.what = = FROM_MAIN_MSG1) {
MainHandler = new Mainactivityhandler (Looper.getmainlooper ());
Textview.append ("sub-thread:received message from UI main thread, and would send message to UI main thread......\n\n");
Mainhandler.sendemptymessage (11);

}
}
}
}



The corresponding Activity_main.xml file:

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
tools:context= "${relativepackage}.${activityclass}" >

<textview
Android:id= "@+id/text"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/hello_world"
Android:layout_above= "@+id/button3"
Android:layout_marginbottom= "50DP"
/>

<button
Android:id= "@+id/verify_button"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Send"
Android:layout_centerinparent= "true"
android:background= "@drawable/shape"
/>
</RelativeLayout>


Android UI Master process communicates directly with child processes

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.