Android asynchronous Task Loading interface implementation

Source: Internet
Author: User

Android asynchronous Task Loading interface implementation

A background method of android asynchronous tasks is essentially to enable a thread to perform time-consuming operations. Other onPostExecute methods and onPreExecute methods run in the UI main thread to update the UI interface. To improve user experience, the common asynchronous Task Loading Methods are summarized as follows:

1. asynchronous loading interface:


The key code is as follows:

/*** Load data to the list using an asynchronous task */private void fillData () {new AsyncTask
 
  
() {@ Overrideprotected void onPreExecute () {loading. setVisibility (View. VISIBLE); super. onPreExecute () ;}@ Overrideprotected void onPostExecute (Void result) {loading. setVisibility (View. INVISIBLE); // refresh the interface list data if (mAdapter = null) {mAdapter = new RubishSmsInfosAdapter (); mRubishSms. setAdapter (mAdapter);} else {mAdapter. yydatasetchanged ();} 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 (2000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke. printStackTrace ();} return null000000000000.exe cute ();}
 



The 
loading=findViewById(R.id.ll_rublish_sms_info_loading);

The layout file is as follows:

Activity_rubish_sms.xml

 
 
  
<FrameLayout android: layout_width = "match_parent" android: layout_height = "match_parent" android: layout_weight = "100">
              
               
           
  </FrameLayout>
      
  
 

The above code can achieve the above effect.

2. Use the dialog box to implement

Implementation:


Logic code for asynchronous task implementation:

/*** Scan the virus and dynamically display it on the Interface */private void scanVirus () {new AsyncTask
 
  
() {List
  
   
Apps = new ArrayList
   
    
(); 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... "); dialog. setCancelable (false); dialog. setContentView (viewKilling); dialog. show (); pm = 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 completed! Detected virus "+ apps. size () + "+", "+" Clean up in time "); for (final VirusApp app: apps) {// View with virus 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 the 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) ;}}); ImageView 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 (); m Container. addView (view) ;}} else if (apps. size () = 0) {mKillVirusResult. setText ("Scan completed! Congratulations! No virus found! ");} Super. onPostExecute (result) ;}@ Overrideprotected Void doInBackground (Void... params) {int total = 0; // obtain the database operation object VirusDao virusDao = new VirusDao (); // obtain the signature of all applications on the device for (PackageInfo: 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 is MD5 encoded to obtain the signature information of the application encrypted by MD5. // Log. I ("anti-virus MD5 <", md5); // query the local virus signature database // desc = virusDao. findVirus (md5); if (desc! = Null) {// query the virus VirusApp app = new VirusApp (); // obtain the application icon Drawable drawable = pm. getApplicationIcon (info. applicationInfo); app. setmAppVirusIcon (drawable); // obtain the application name String appName = (String) pm. getApplicationLabel (info. applicationInfo); app. setmAppVirusName (appName); // obtain the virus description of the application. setmAPPVirusDesc (desc); apps. add (app); app = null; // publishProgress (total, app);} else {Log. I ("anti-virus MD5 <", "not virus" + total) ;}// progressBar. setProgress (total); try {Thread. sleep (200);} catch (InterruptedException e) {// TODO Auto-generated catch blocke. printStackTrace ();} return null000000000000.exe cute ();}
   
  
 
The layout page of the activity corresponding to the logic processing:
 
     
      
      
          
           
       
  
 
This interface has a linear layout container that can dynamically load data.


Customize the dialog box layout:

 
     
      
  
 

The entire Activity code that dynamically scans and loads 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; // scan and KillVirusActivity extends Activity {private LinearLayout mContainer; private TextView mKillVirusResult; private PackageManager pm; private ProgressBar progressBar; private List
 
  
PackageInfos; public KillVirusActivity () {}@ Overrideprotected void onCreate (Bundle savedInstanceState) {setContentView (R. layout. activity_kill_virus); mContainer = (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 the local virus signature to the system directory scanVirus (); super. onCreate (savedInstanceState);}/*** scan the virus and dynamically display it on the */private void scanVirus () {new AsyncTask
  
   
() {List
   
    
Apps = new ArrayList
    
     
(); 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... "); dialog. setCancelable (false); dialog. setContentView (viewKilling); dialog. show (); pm = 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 completed! Detected virus "+ apps. size () + "+", "+" Clean up in time "); for (final VirusApp app: apps) {// View with virus 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 the 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) ;}}); ImageView 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 (); m Container. addView (view) ;}} else if (apps. size () = 0) {mKillVirusResult. setText ("Scan completed! Congratulations! No virus found! ");} Super. onPostExecute (result) ;}@ Overrideprotected Void doInBackground (Void... params) {int total = 0; // obtain the database operation object VirusDao virusDao = new VirusDao (); // obtain the signature of all applications on the device for (PackageInfo: 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 is MD5 encoded to obtain the signature information of the application encrypted by MD5. // Log. I ("anti-virus MD5 <", md5); // query the local virus signature database // desc = virusDao. findVirus (md5); if (desc! = Null) {// query the virus VirusApp app = new VirusApp (); // obtain the application icon Drawable drawable = pm. getApplicationIcon (info. applicationInfo); app. setmAppVirusIcon (drawable); // obtain the application name String appName = (String) pm. getApplicationLabel (info. applicationInfo); app. setmAppVirusName (appName); // obtain the virus description of the application. setmAPPVirusDesc (desc); apps. add (app); app = null; // publishProgress (total, app);} else {Log. I ("anti-virus MD5 <", "not virus" + total) ;}// progressBar. setProgress (total); try {Thread. sleep (200);} catch (InterruptedException e) {// TODO Auto-generated catch blocke. printStackTrace ();} return null;}cmd.exe cute ();}/*** copy the local virus database to the app directory */private void copyVirusDB () {new AsyncTask
     
      
() {@ Overrideprotected Void doInBackground (Void... params) {// obtain the object to be copied to the destination File file = new File (getFilesDir (), "antivirus. db "); if (file. exists () & file. length ()> 0) {} else {// obtain 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 null000000000000.exe cute ();}}
     
    
   
  
 



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.