android 异步任务的一个后台方法本质是开启一个线程完成耗时操作,其他onPostExecute方法和onPreExecute方法运行在UI主线程用于更新UI界面。为了提高用户体验常见的异步任务加载方式现在总结如下:
1、异步加载界面效果如下:
关键代码如下所示:
/** * 异步任务给列表加载数据 */ private void fillData(){ new AsyncTask<Void,Void,Void>(){ @Override protected void onPreExecute() { loading.setVisibility(View.VISIBLE); super.onPreExecute(); } @Override protected void onPostExecute(Void result) { loading.setVisibility(View.INVISIBLE); //刷新界面列表数据 if(mAdapter==null){ mAdapter=new RubishSmsInfosAdapter(); mRubishSms.setAdapter(mAdapter); }else{ mAdapter.notifyDataSetChanged(); } super.onPostExecute(result); } @Override protected 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 block e.printStackTrace(); } return null; } }.execute(); }
loading对应的
loading=findViewById(R.id.ll_rublish_sms_info_loading);
布局文件如下:
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:layout_weight="100" > <LinearLayout android:layout_gravity="center" android:id="@+id/ll_rublish_sms_info_loading" android:layout_width="match_parent" android:layout_height="wrap_content" 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="数据加载中,请稍后..." android:textSize="13sp" /> </LinearLayout> </FrameLayout> <ListView android:id="@+id/lv_rubish_sms_content" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView></LinearLayout>
通过如上代码即可实现上述效果。
2、利用对话框实现
实现效果图:
异步任务实现 逻辑代码:
/** * 扫描病毒 并动态显示在界面 */ private void scanVirus(){ new AsyncTask<Void,Object, Void>() { List<VirusApp> apps=new ArrayList<VirusApp>(); String desc=null; Dialog dialog=new Dialog(KillVirusActivity.this); View viewKilling=View.inflate(KillVirusActivity.this,R.layout.killing_dialog,null); @Override protected void onPreExecute() { mKillVirusResult.setText("正在扫描中,请稍等..."); 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(); } @Override protected void onPostExecute(Void result) { dialog.dismiss(); if(apps.size()>0){ mKillVirusResult.setTextColor(Color.RED); mKillVirusResult.setText("扫描完成!发现病毒"+apps.size()+"个"+","+"请及时清理"); for(final VirusApp app:apps){ //有病毒 View view=View.inflate(KillVirusActivity.this,R.layout.app_virus_info_item,null); view.setClickable(true); view.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //卸载程序 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()); mContainer.addView(view); } }else if(apps.size()==0){ mKillVirusResult.setText("扫描完成!恭喜没有发现病毒!"); } super.onPostExecute(result); } @Override protected Void doInBackground(Void... params) { int total=0; //获得数据库操作对象 VirusDao virusDao=new VirusDao(); //获得设备上所有应用的签名 for(PackageInfo info:packageInfos){ total++; Signature[] signature=info.signatures; try { String md5=Md5Util.encode(signature[0].toString()); desc=virusDao.findVirus(md5); Log.i("杀毒MD5<<<", md5); desc=virusDao.findVirus(md5); } catch (Exception e) { // TODO: handle exception } //Log.i("杀毒<<<", (signature[0]).toString()); //签名用MD5编码获得MD5加密后的应用的签名信息 //Log.i("杀毒MD5<<<", md5); //查询本地病毒签名数据库 // desc=virusDao.findVirus(md5); if(desc!=null){//查询到病毒 VirusApp app=new VirusApp(); //获得应用的图标 Drawable drawable=pm.getApplicationIcon(info.applicationInfo); app.setmAppVirusIcon(drawable); //获得应用名 String appName=(String) pm.getApplicationLabel(info.applicationInfo); app.setmAppVirusName(appName); //获得该应用的病毒描述 app.setmAPPVirusDesc(desc); apps.add(app); app=null; //publishProgress(total,app); }else{ Log.i("杀毒MD5<<<","不是病毒"+total); } } //progressBar.setProgress(total); try { Thread.sleep(200); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } }.execute(); }
该逻辑处理对应的activity布局界面:
<?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:background="@drawable/ll_title_bg" android:gravity="center" android:text="手机病毒查杀" /> <TextView android: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_width="match_parent" android:layout_height="wrap_content" > <LinearLayout <span style="color:#ff0000;"> android:id="@+id/ll_container"</span> android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > </LinearLayout> </ScrollView></LinearLayout>
该界面有一个线性布局容器,可以实现动态加载数据的效果。
实现自定义对话框的布局:
<?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" > <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="病毒扫描中..." android:textSize="15sp" /></LinearLayout>
实现动态扫描并加载数据的整个Activity代码:
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;//本地病毒查杀public class KillVirusActivity extends Activity {private LinearLayout<span style="color:#ff0000;"> mContainer</span>;private TextView mKillVirusResult;private PackageManager pm;private ProgressBar progressBar;private List<PackageInfo> packageInfos; public KillVirusActivity() { } @Override protected 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();//拷贝本地病毒签名到系统目录 scanVirus(); super.onCreate(savedInstanceState); } /** * 扫描病毒 并动态显示在界面 */ private void scanVirus(){ new AsyncTask<Void,Object, Void>() { List<VirusApp> apps=new ArrayList<VirusApp>(); String desc=null; Dialog dialog=new Dialog(KillVirusActivity.this); View viewKilling=View.inflate(KillVirusActivity.this,R.layout.killing_dialog,null); @Override protected void onPreExecute() { mKillVirusResult.setText("正在扫描中,请稍等..."); 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(); } @Override protected void onPostExecute(Void result) { dialog.dismiss(); if(apps.size()>0){ mKillVirusResult.setTextColor(Color.RED); mKillVirusResult.setText("扫描完成!发现病毒"+apps.size()+"个"+","+"请及时清理"); for(final VirusApp app:apps){ //有病毒 View view=View.inflate(KillVirusActivity.this,R.layout.app_virus_info_item,null); view.setClickable(true); view.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //卸载程序 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()); <span style="color:#ff0000;">mContainer.addView(view);</span> } }else if(apps.size()==0){ mKillVirusResult.setText("扫描完成!恭喜没有发现病毒!"); } super.onPostExecute(result); } @Override protected Void doInBackground(Void... params) { int total=0; //获得数据库操作对象 VirusDao virusDao=new VirusDao(); //获得设备上所有应用的签名 for(PackageInfo info:packageInfos){ total++; Signature[] signature=info.signatures; try { String md5=Md5Util.encode(signature[0].toString()); desc=virusDao.findVirus(md5); Log.i("杀毒MD5<<<", md5); desc=virusDao.findVirus(md5); } catch (Exception e) { // TODO: handle exception } //Log.i("杀毒<<<", (signature[0]).toString()); //签名用MD5编码获得MD5加密后的应用的签名信息 //Log.i("杀毒MD5<<<", md5); //查询本地病毒签名数据库 // desc=virusDao.findVirus(md5); if(desc!=null){//查询到病毒 VirusApp app=new VirusApp(); //获得应用的图标 Drawable drawable=pm.getApplicationIcon(info.applicationInfo); app.setmAppVirusIcon(drawable); //获得应用名 String appName=(String) pm.getApplicationLabel(info.applicationInfo); app.setmAppVirusName(appName); //获得该应用的病毒描述 app.setmAPPVirusDesc(desc); apps.add(app); app=null; //publishProgress(total,app); }else{ Log.i("杀毒MD5<<<","不是病毒"+total); } } //progressBar.setProgress(total); try { Thread.sleep(200); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } }.execute(); } /** * 拷贝本地病毒库数据库到app目录 */ private void copyVirusDB(){ new AsyncTask<Void,Void,Void>(){ @Override protected Void doInBackground(Void... params) { //获得要拷贝到目的地的文件 File file=new File(getFilesDir(),"antivirus.db"); if(file.exists()&&file.length()>0){ }else{ //获得文件输入流 InputStream is=null; try { is=getResources().getAssets().open("antivirus.db"); CopyFileToSystem.copyFile(is, file.getAbsolutePath()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; } }.execute(); } }