1 PackageCom.example.asynctask;2 3 Importjava.net.MalformedURLException;4 ImportJava.net.URL;5 6 Importandroid.app.Activity;7 ImportAndroid.os.AsyncTask;8 ImportAndroid.os.Bundle;9 ImportAndroid.util.Log;Ten ImportAndroid.view.Menu; One ImportAndroid.view.MenuItem; A ImportAndroid.widget.TextView; - /** - * Asynchronous task implementation, you can implement directly modify the UI thread of the control. the * @authorSHB - * - */ - Public classMainactivityextendsActivity { + PrivateTextView TView; - @Override + protected voidonCreate (Bundle savedinstancestate) { A Super. OnCreate (savedinstancestate); at Setcontentview (r.layout.activity_main); -TView = (TextView) This. Findviewbyid (r.id.textview1); -Testasyn Testasyn =NewTestasyn (); - Try { -Testasyn.execute (NewURL ("Https://www.baidu.com/img/bd_logo1.png")); -}Catch(malformedurlexception e) { in //TODO auto-generated Catch block - e.printstacktrace (); to } + } - Public classTestasynextendsAsynctask<url,integer,string> { the * @Override $ protectedString doinbackground (URL ... arg0) {Panax Notoginseng //TODO auto-generated Method Stub - returnArg0[0].tostring (); the } + @Override A protected voidOnPostExecute (String result) { the //TODO auto-generated Method Stub + Super. OnPostExecute (result); -Mainactivity. This. Tview.settext (result); $ } $ @Override - protected voidOnPreExecute () { - //TODO auto-generated Method Stub the Super. OnPreExecute (); -LOG.D ("msg", "Onpreex");Wuyi } the } -}
Android Asynchronous Task Asynctask