One, thread
1. UI thread
A, Handler Handler = new Handler ();
Runnable Runnable = new Runnable () {
public void Run () {
}
};
Handler.post (runnable);
B, Handler Handler = new Handler () {
public void Handlermessage (Message msg) {
}
};
Handler.sendmessage ();
Second, new thread
A, thread thread = new Thread (Runnable () {
Public void Run () {
}
). Start ();
B, Handlerthread handlerthread = new Handlerthread ("Handler_thread");
Handlerthread.start ();
Mhandler Mhandler = new Handler (Handlerthread.getlooper ());
Message msg = Hangdler.obtainmessage ();
Msg.sendtotarget;
Class Mhandler extends handler{
Handlemessage
}
C,Asynctask
Steps:
① Instantiation of Asynctask:
Instantiate Asynctask then pass task.exec (Pamas); Pass in the parameter, this parameter list is dynamic, can be one can also make multiple, length variable.
Asynctask, the first parameter is passed in the method Doinbackground (params), the second parameter is the value of the data update, and the third is the result of processing the return of the transaction.
②onpreexecute Method:
This method has no parameters and no return value, so you can do some reminders in this method. Like show a dialog, or play a toast and tell the user to start the download.
③doinbackground (params) method:
Enter the ASYNCTASK internal structure, first will execute the Reslut doinbackground (params) method, this method will handle the time consuming transaction, exec () parameters will be passed into this method to do parameters, and the return value will be as OnPostExecute () The parameters. If you want to update your progress, you need to execute the publicprogress () method.
④onprogressupdate (values) method:
The parameter of this method must execute the Publicprogress () method in the Doinbackground () method, this method will pass the parameter into the Onprogressupdate () method, then can do some UI update display in this method, For example, the value of the progress bar can be changed dynamically through this values value.
⑤onpostexecute (Result) method:
Here is the way the transaction is completed, and the results of the Doinbackground method execution will be uploaded here, if the method returns data. The UI can be handled in this way, and the processed data can be displayed on the UI. than a piece of text Ah, everything you want results.
November 10, 2014 Android Learning Summary