Android multithreading and interface interaction and simple runonuithread applications

Source: Internet
Author: User

From: http://blog.csdn.net/keyboard_workers/article/details/7620566

Runonuithread is the internal method of the activity. It is best to specify the context when using it ).

New thread (New runnable (){

@ Override
Public void run (){
System. Out. println (thread. currentthread (). GETID ());
Runonuithread (New runnable (){

@ Override
Public void run (){
Toast. maketext (runonuithreadactivity. This, "UI operation...", 1000). Show ();
}
});
}

}). Start ();

,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
New thread (New runnable (){

@ Override
Public void run (){
If (isclose)
Return;
If (I! = 0)
Toast. maketext (runonuithreadactivity. This, I + "", 1000). Show ();
I ++;
Handler. postdelayed (this, 2000 );
}

}). Start ();
}

The above two actually work in the same way. runonuithread will call

Public final void runonuithread (runnable action ){
If (thread. currentthread ()! = Muithread ){
Mhandler. Post (action );
} Else {
Action. Run ();
}
}

First, determine whether the thread is a UI thread. If not, post the thread. If yes, the thread runs normally.

As long as it passes through the handler. Post or postdelayed processing thread runnable in the main thread, it can be converted to the UI main thread. Besides, the handler mechanism is used to process thread-UI communication.



Iii. Android multi-thread interface Interaction


1. activity. runonuithread (runnable)


2. View. Post (runnable), view. postdelay (runnable, long)


3. Handler


4. asynctask


Below we use a project to implement the above four interaction Methods




Public class mainactivity extends activity {

Private textview txview;
Private button;
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Log. I ("rootyinfo", "oncreate ");
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );

Txview = (textview) findviewbyid (R. Id. textview1 );
Button = (button) findviewbyid (R. Id. button1 );
Button. setonclicklistener (New onclicklistener (){

@ Override
Public void onclick (view v ){
// Todo auto-generated method stub

// Create a thread for displaying the interaction between the first three background threads and UI threads
New testthread (mainactivity. This). Start ();

// Create a testasynctask for displaying asynctask for interaction.
New testasynctask(cmd.exe cute ("test", "asynctask ");
}
});
}


Class testasynctask extends asynctask <string, integer, string>
{
// After testasynctask is executed by the background thread, it is called by the UI thread and is generally used to initialize interface controls, such as progress bars
@ Override
Protected void onpreute (){
// Todo auto-generated method stub
Super. onpreute ();
}

// After doinbackground is executed, it is called by the UI thread to update the interface.
@ Override
Protected void onpostute (string result ){
// Todo auto-generated method stub
Txview. settext (result );
Super. onpostute (result );
}

// After preexcute is executed, it is called by the background thread of aysnctask, and the result is returned to the UI thread.
@ Override
Protected string doinbackground (string... Params ){
// Todo auto-generated method stub
Stringbuffer sb = new stringbuffer ();
For (string: Params ){
SB. append (string );
}
Return sb. tostring ();
}

}
// Handler used for inter-thread Communication
Class testhandler extends Handler
{

Public testhandler (low.logoff ){
Super (logoff );
// Todo auto-generated constructor stub
}

@ Override
Public void handlemessage (Message MSG ){
// Todo auto-generated method stub
System. Out. println ("123 ");
Txview. settext (string) msg. getdata (). Get ("tag "));
Super. handlemessage (MSG );
}

}
// Background Thread class
Class testthread extends thread
{
Activity activity;
Public testthread (activity ){

This. Activity = activity;
}
@ Override
Public void run (){

// The following Code demonstrates the implementation of the activity. runonuithread (runnable) method.
Activity. runonuithread (New runnable (){
@ Override
Public void run (){
// Todo auto-generated method stub
Txview. settext ("test runonuithread ");
}
});

// The following Code demonstrates the implementation of the activity. runonuithread (runnable) method.
Txview. Post (New runnable (){

@ Override
Public void run (){
// Todo auto-generated method stub
Txview. settext ("test view. Post (runnable )");
}
});

// The following Code demonstrates the implementation of the activity. runonuithread (runnable) method.
Txview. postdelayed (New runnable (){

@ Override
Public void run (){
// Todo auto-generated method stub
Txview. settext ("test view. postdelay (runnable, long )");
}
},1000 );

// The following Code demonstrates the implementation of the handler method.
Message MSG = new message ();
Bundle bundle = new bundle ();
Bundle. putstring ("tag", "test handler ");
MSG. setdata (bundle );
New testhandler (Looper. getmainlooper (). sendmessage (MSG );

Super. Run ();
}

}

}

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.