One of the background methods of an Android asynchronous task is to turn on a thread to complete the time-consuming operation, and the other OnPostExecute methods and OnPreExecute methods run in the UI main thread to update the UI interface. To improve the user experience the common asynchronous task load mode is now summarized as follows:
1, the asynchronous loading interface effect is as follows:
The key code is as follows:
/** * Asynchronous task Load data to list */private void Filldata () {new asynctask<void,void,void> () {@Overrideprotected void OnPreExecute () {loading.setvisibility (view.visible); Super.onpreexecute ();} @Overrideprotected void OnPostExecute (void result) {loading.setvisibility (view.invisible);//Refresh Interface list data if (madapter== NULL) {madapter=new rubishsmsinfosadapter (); Mrubishsms.setadapter (Madapter);} Else{madapter.notifydatasetchanged ();} Super.onpostexecute (result);} @Overrideprotected void Doinbackground (void ... params) {if (minfos==null) {minfos=mrubishsmsinfodao.findinfosbypage ( Maxnum, offset);} Else{minfos.addall (Mrubishsmsinfodao.findinfosbypage (maxnum, offset));} try {thread.sleep;} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} return null;}}. Execute ();}
Loading the corresponding
Loading=findviewbyid (r.id.ll_rublish_sms_info_loading);
The layout file is as follows:
Activity_rubish_sms.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vertical " > <framelayout android:layout_width= "match_parent" android:layout_height= "Match_parent" Android oid:layout_weight= "> <linearlayout android:layout_gravity=" center "android:id=" @+i D/ll_rublish_sms_info_loading "android:layout_width=" match_parent "android:layout_height=" Wrap_conte NT "android:orientation=" vertical "android:visibility=" invisible "> <progressbar Android:layout_width= "Match_parent" android:layout_height= "Wrap_content"/> < TextView android:gravity= "center" android:layout_width= "Match_parent" Android : layout_height= "Wrap_content" android:text= "Data loading, please later ..." android:textsize= "13sp"/> </LinearLayout> </fr amelayout> <listview android:id= "@+id/lv_rubish_sms_content" android:layout_width= "Match_parent" android:layout_height= "Wrap_content" > </ListView></LinearLayout>
This can be achieved with the above code.
2, using the dialog box to achieve
Realize:
Asynchronous task Implementation Logic code:
/** * Scan virus and dynamically display in interface */private void Scanvirus () {New asynctask<void,object, void> () {list<virusapp> apps=new Ar Raylist<virusapp> (); String desc=null;dialog dialog=new Dialog (killvirusactivity.this); View viewkilling=view.inflate (killvirusactivity.this,r.layout.killing_dialog,null); @Overrideprotected void OnPreExecute () {Mkillvirusresult.settext ("scanning, please wait ...");d ialog.setcancelable (False);d Ialog.setcontentview ( viewkilling);d ialog.show ();p M=getpackagemanager (); Packageinfos=pm.getinstalledpackages (packagemanager.get_uninstalled_packages| Packagemanager.get_signatures);//Progressbar.setmax (Packageinfos.size ()); Super.onpreexecute ();} @Overrideprotected void OnPostExecute (void result) {Dialog.dismiss (); if (Apps.size () >0) { Mkillvirusresult.settextcolor (color.red); Mkillvirusresult.settext ("Scan done! Found virus "+apps.size () +" + "," + "please clean up"); for (final Virusapp App:apps) {//have virus view view=view.inflate ( Killvirusactivity.this,r.layout.app_virus_info_item,null); view.setclickable (true); view.setoncliCklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//Uninstall program Intent Intent = new intent (); Intent.setaction ("Android.intent.action.DELETE"); Intent.addcategory (Intent.category_default); Intent.setdata ( Uri.parse ("Package:" + app.getmappvirusname ())); StartActivity (intent);}); I Mageview appicon= (ImageView) View.findviewbyid (R.id.iv_app_icon); TextView appname= (TextView) View.findviewbyid (r.id.tv_app_name); TextView appdesc= (TextView) View.findviewbyid (R.ID.TV_APP_VIRUS_DESC); Appicon.setbackground (App.getmappvirusicon ()); Appname.settext (App.getmappvirusname ()); Appdesc.settext (App.getmappvirusdesc ()); Mcontainer.addview (view);}} else if (apps.size () ==0) {mkillvirusresult.settext ("Scan done! Congratulations on not finding the virus! ");} Super.onpostexecute (result);} @Overrideprotected void Doinbackground (void ... params) {int total=0;//gets the database operand Virusdao virusdao=new Virusdao ();// Get signatures for all apps on the device for (PackageInfo Info:packageinfos) {total++; Signature[] Signature=info.signatures;try {String md5=md5util.encode (signature[0].tostring ()); Desc=virusdao.findvirus (MD5); LOG.I ("Anti-Virus md5<<<", MD5); Desc=virusdao.findvirus (MD5);} catch (Exception e) {//Todo:handle exception}//log.i ("Antivirus <<<", (Signature[0]). toString ());// The signature uses MD5 code to obtain the signature information of MD5 encrypted application//log.i ("Antivirus md5<<<", MD5);//query local virus Signature Database//Desc=virusdao.findvirus (MD5); if (desc!=null) {//query to Virus Virusapp app=new virusapp ();//Get app icon drawable Drawable=pm.getapplicationicon ( Info.applicationinfo); App.setmappvirusicon (drawable); Get the application name string Appname= (String) Pm.getapplicationlabel (info.applicationinfo); App.setmappvirusname (AppName); Obtain the virus description of the application APP.SETMAPPVIRUSDESC (DESC); Apps.add (APP); App=null; Publishprogress (Total,app); }else{log.i ("Antivirus md5<<<", "Not Virus" +total);}} Progressbar.setprogress (total); try {thread.sleep ($);} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} return null;}}. Execute ();}The logic handles the corresponding activity layout interface:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vertical " > <textview android:layout_width= "match_parent" android:layout_height= "35DP" Android:backgro und= "@drawable/ll_title_bg" android:gravity= "center" android:text= "mobile phone virus Avira"/> <textview Android Oid:id= "@+id/tv_kill_virus_result" android:layout_width= "match_parent" android:layout_height= "125DP" android:gravity= "center" android:background= "@drawable/ll_title_bg"/> <scrollview android:layout_w Idth= "Match_parent" android:layout_height= "wrap_content" > <linearlayout <span style= "Col Or: #ff0000; " > android:id= "@+id/ll_container" </span> android:layout_width= "Match_parent" Android:layout_ height= "Wrap_content" android:orientation= "vertical" > </LinearLayout> </ScrollView></LinearLayout>
the interface has a linear layout container that enables the effect of dynamically loading data.
To implement the layout of a custom dialog box:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" > <progressbar android:layout_width= "wrap_content" android:layout_ height= "Wrap_content" android:layout_gravity= "center"/> <textview android:layout_width= " Match_parent " android:layout_height=" wrap_content " android:gravity=" center " android:text=" virus scan ... " android:textsize=" 15SP "/></linearlayout>
Implement a dynamic scan and load the entire activity code of the data:
Package Com.example.yqqmobilesafe;import Java.io.file;import Java.io.ioexception;import java.io.InputStream;import Java.util.arraylist;import Java.util.list;import Android.app.activity;import Android.app.dialog;import Android.content.intent;import Android.content.pm.packageinfo;import Android.content.pm.packagemanager;import Android.content.pm.signature;import Android.graphics.color;import Android.graphics.drawable.drawable;import Android.net.uri;import Android.os.asynctask;import Android.os.bundle;import Android.util.log;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.imageview;import Android.widget.linearlayout;import Android.widget.progressbar;import Android.widget.textview;import Com.example.yqqmobilesafe.db.dao.virusdao;import Com.example.yqqmobilesafe.domain.virusapp;import Com.example.yqqmobilesafe.utils.copyfiletosystem;import com.example.yqqmobilesafe.utils.md5util;//Local virus Avira public Class Killvirusactivity extends Activity {private LinearlaYout<span style= "color: #ff0000;" > mcontainer</span>;p rivate TextView mkillvirusresult;private packagemanager pm;private ProgressBar Progressbar;private list<packageinfo> packageinfos;public killvirusactivity () {} @Overrideprotected void OnCreate (Bundle savedinstancestate) {Setcontentview (R.layout.activity_kill_virus); <span style= "color: #ff0000;" >mcontainer</span>= (LinearLayout) This.findviewbyid (R.id.ll_container); mkillvirusresult= (TextView) This.findviewbyid (R.id.tv_kill_virus_result);//progressbar= (ProgressBar) This.findviewbyid (r.id.pb_scanning_ virus); Copyvirusdb ();//Copy Local virus signature to System directory Scanvirus (); super.oncreate (savedinstancestate);} /** * Scan virus and dynamically display in interface */private void Scanvirus () {New asynctask<void,object, void> () {list<virusapp> apps=new Ar Raylist<virusapp> (); String desc=null;dialog dialog=new Dialog (killvirusactivity.this); View viewkilling=view.inflate (killvirusactivity.this,r.layout.killing_dialog,null); @Overrideprotected void OnPreExecute() {Mkillvirusresult.settext ("scanning, please wait ...");d ialog.setcancelable (False);d Ialog.setcontentview (viewkilling); Dialog.show ();p M=getpackagemanager (); Packageinfos=pm.getinstalledpackages (packagemanager.get_uninstalled_packages| Packagemanager.get_signatures);//Progressbar.setmax (Packageinfos.size ()); Super.onpreexecute ();} @Overrideprotected void OnPostExecute (void result) {Dialog.dismiss (); if (Apps.size () >0) { Mkillvirusresult.settextcolor (color.red); Mkillvirusresult.settext ("Scan done! Found virus "+apps.size () +" + "," + "please clean up"); for (final Virusapp App:apps) {//have virus view view=view.inflate ( Killvirusactivity.this,r.layout.app_virus_info_item,null); view.setclickable (true); View.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {//Uninstaller Intent Intent = new intent (); Intent.setaction (" Android.intent.action.DELETE "); Intent.addcategory (Intent.category_default); Intent.setdata (Uri.parse (" Package: " + App.getmappvirusname ())); StartActivity (intent);}); I Mageview appicon= (ImageView) VIEW.FINDVIEWBYid (R.id.iv_app_icon); TextView appname= (TextView) View.findviewbyid (r.id.tv_app_name); TextView appdesc= (TextView) View.findviewbyid (R.ID.TV_APP_VIRUS_DESC); Appicon.setbackground (App.getmappvirusicon ()); Appname.settext (App.getmappvirusname ()); Appdesc.settext (App.getmappvirusdesc ()); <span style= "color:# ff0000; " >mcontainer.addview (view); </span>}}else if (Apps.size () ==0) {mkillvirusresult.settext ("Scan done! Congratulations on not finding the virus! ");} Super.onpostexecute (result);} @Overrideprotected void Doinbackground (void ... params) {int total=0;//gets the database operand Virusdao virusdao=new Virusdao ();// Get signatures for all apps on the device for (PackageInfo Info:packageinfos) {total++; Signature[] Signature=info.signatures;try {String md5=md5util.encode (signature[0].tostring ()); desc= Virusdao.findvirus (MD5); LOG.I ("Anti-Virus md5<<<", MD5); Desc=virusdao.findvirus (MD5);} catch (Exception e) {//Todo:handle exception}//log.i ("Antivirus <<<", (Signature[0]). toString ());// Signature using MD5 code to obtain MD5 encrypted application signature information//log.i ("Anti-Virus md5<<<", MD5);//query local virus Signature Database//Desc=virusdao. Findvirus (MD5); if (desc!=null) {//query to Virus Virusapp app=new virusapp ();//Get app icon drawable Drawable=pm.getapplicationicon ( Info.applicationinfo); App.setmappvirusicon (drawable); Get the application name string Appname= (String) Pm.getapplicationlabel (info.applicationinfo); App.setmappvirusname (AppName); Obtain the virus description of the application APP.SETMAPPVIRUSDESC (DESC); Apps.add (APP); App=null; Publishprogress (Total,app); }else{log.i ("Antivirus md5<<<", "Not Virus" +total);}} Progressbar.setprogress (total); try {thread.sleep ($);} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} return null;}}. Execute ();} /** * Copy Local virus library database to app directory */private void Copyvirusdb () {new asynctask<void,void,void> () {@Overrideprotected void Doinbackground (Void ... params) {//Get the file to be copied to the destination files file=new (Getfilesdir (), "antivirus.db"); if (File.exists () &&file.length () >0) {}else{//Gets the file input stream InputStream is=null;try {is=getresources (). Getassets (). Open (" Antivirus.db "); Copyfiletosystem.copyfile (IS, File.getabsolutepath ());} CaTCH (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} return null;}}. Execute ();}}
Android asynchronous task loading Data interface implementation