Use asynctask to open sub-threads to get Server data, update UI UI
/*** Asynchronous Task * *@authorTaoshihan **/ Public classHttpasynctaskextendsAsynctask<string, Void, string> { /*** Execute before performing a background task*/@Overrideprotected voidOnPreExecute () {String netInfo=Netutils.getnetworkinfo (context); if("No". Equals (NetInfo)) {Toast.maketext (context,"Network connection Failed", 0). Show (); } Else if("WiFi". Equals (NetInfo)) {Toast.maketext (context,"WiFi connection", 0). Show (); } Else if("mobile". Equals (NetInfo)) {Toast.maketext (context,"Phone traffic Connection", 0). Show (); } } /*** Background run, time-consuming operations are put here, equivalent to the thread of the Run method*/@Overrideprotectedstring Doinbackground (String ... params) {//TODO auto-generated Method Stub return NULL; } /*** After performing a background task, you can change the UI interface in this way, equivalent to the processing method inside the handler*/@Overrideprotected voidOnPostExecute (String result) {//TODO auto-generated Method Stub Super. OnPostExecute (Result); } }
Get information asynchronously using Thread and Handler
/*** Using threads and handler *@authorTaoshihan **/ Public classHttpHandlerextendshandler{@Override Public voidhandlemessage (Message msg) {//Handling Message } } Public classHttpthreadextendsthread{@Override Public voidrun () {//Send Message NewHttpHandler (). SendMessage (NewMessage ()); } }
New Httpthread (). Start (); New Httpasynctask (). Execute ("htp://www.baidu.com");
[Android] Two kinds of asynchronous way