As part of Android development, the role of multithreading is important, first of all to say the application scenario
1 multi-threaded usage scenarios
1.1 In normal use, there are often sub-threads to update the interface UI requirements, but Android does not allow child threads to update the UI
Using a child thread to process the UI, if the thread takes a long time, is bound to cause the UI to be unresponsive, resulting in program ANR (application is not responding), which makes the user experience poor.
1.2 Requirements for multi-threaded execution
As with other programming development, multithreading can increase the concurrency of programs and increase the throughput capacity of programs.
2 Multi-Threading Development mode
2.1 Using Asynctask
Asynctask is an auxiliary class of asynchronous processing provided by the Android framework, with multiple back-up interfaces, which are quite powerful
2.2 Using Handler
Through the messaging team to achieve inter-thread communication, with the looper, with respect to asynctask, with higher plasticity.
3 on the Code
Public classUihandlerextendshandler{PrivateEditText Medittext =NULL;; PublicUihandler (EditText EditText) {Medittext=EditText; } @Override Public voidhandlemessage (Message msg) {Super. Handlemessage (msg); String value=string.valueof (msg.obj); if(medittext!=NULL) {Medittext.settext (value); } }}
Simple implementation of Handler subclass
Public classUiasynctaskextendsAsynctask<integer, Void, integer>{@Overrideprotected voidoncancelled () {Super. oncancelled (); } @Overrideprotected voidOnPreExecute () {Super. OnPreExecute (); } @Overrideprotected voidonprogressupdate (Void ... values) {Super. Onprogressupdate (values); } @OverrideprotectedObject Clone ()throwsclonenotsupportedexception {return Super. Clone (); } @Override Public Booleanequals (Object o) {return Super. Equals (o); } @Overrideprotected voidFinalize ()throwsThrowable {Super. Finalize (); } @Override Public inthashcode () {return Super. Hashcode (); } @Override PublicString toString () {return Super. toString (); } PrivateEditText Medittext =NULL; PublicUiasynctask (EditText EditText) {Medittext=EditText; } @Overrideprotectedinteger doinbackground (integer ... param) {returnparam[0]++; } @Overrideprotected voidOnPostExecute (Integer result) {Medittext.settext (string.valueof (result)); }}
Simple implementation of Asyntask subclass
Public classMainactivityextendsActivity {PrivateEditText Medittext =NULL; PrivateUihandler Mhandler =NULL; PrivateUiasynctask Masynctask =NULL; Private intMCount = 0; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Medittext= (EditText) This. Findviewbyid (r.id.txttips); Mhandler=NewUihandler (Medittext); Masynctask=NewUiasynctask (Medittext); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; } Public voidOnbtnhandlerclick (View v) {Message msg=Mhandler.obtainmessage (); Msg.obj= mcount++; Mhandler.sendmessage (msg); } Public voidOnbtnuithreadclick (View v) { This. Runonuithread (NewRunnable () { Public voidrun () {Medittext.settext (string.valueof (MCount++)); } }); } Public voidOnbtnviewpostclick (View v) {//can only be executed once, otherwise the exception will occur when multiple calls are made; NewThread (NewRunnable () { Public voidrun () {Medittext.settext (string.valueof (MCount++)); } }); } Public voidOnbtnaysncclick (View v) {//can only be executed once, otherwise the exception will occur when multiple calls are made;Masynctask.execute (MCount); }}
Mainactivity's Content
<?xml version= "1.0" encoding= "Utf-8"?><resources> <string name= "app_name" > Thread use </string > <string name= "action_settings" >Settings</string> <string name= "Hello_world" >hello world!</string> <string name= "info_tips" > Thread usage </string> <string name= "Info_show" > Waiting for thread return information </string> <string name= "Handler_method" >handler method </string> <string Name = "Uithread_method" >uithread method </string> <string name= "Uipost_method" >viewpost method (can only be performed once) </ string> <string name= "Asynctask_method" >asynctask method (can only be performed once) </string></resources>
String definitions
<LinearLayoutxmlns: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"android:orientation= "vertical"Tools:context=". Mainactivity " > <TextViewAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "@string/info_tips" /> <EditTextAndroid:id= "@+id/txttips"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:hint= "@string/info_show" /> <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:onclick= "Onbtnhandlerclick"Android:text= "@string/handler_method" /> <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:onclick= "Onbtnuithreadclick"Android:text= "@string/uithread_method" /> <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:onclick= "Onbtnviewpostclick"Android:text= "@string/uipost_method" /> <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:onclick= "Onbtnaysncclick"Android:text= "@string/asynctask_method" /></LinearLayout>
Layout file
4 Summary
Time is rather hasty, writing is not very detailed, hey!
Android Learning three: threading operations