androidUI设计|android异步任务加载数据界面实现
android 异步任务的一个后台方法本质是开启一个线程完成耗时操作,其他onPostExecute方法和onPreExecute方法运行在UI主线程用于更新UI界面。为了提高用户体验常见的异步任务加载方式现在总结如下:
1、异步加载界面效果如下:
文章图片
关键代码如下所示:
/**
* 异步任务给列表加载数据
*/
private void fillData(){
new AsyncTask(){@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
通过如上代码即可实现上述效果。 2、利用对话框实现
实现效果图:
文章图片
异步任务实现 逻辑代码:
【androidUI设计|android异步任务加载数据界面实现】
/**
* 扫描病毒 并动态显示在界面
*/
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);
@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布局界面:
该界面有一个线性布局容器,可以实现动态加载数据的效果。
实现自定义对话框的布局:
实现动态扫描并加载数据的整个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 mContainer;
private TextView mKillVirusResult;
private PackageManager pm;
private ProgressBar progressBar;
private List packageInfos;
public KillVirusActivity() { }
@Override
protected 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();
//拷贝本地病毒签名到系统目录
scanVirus();
super.onCreate(savedInstanceState);
}
/**
* 扫描病毒 并动态显示在界面
*/
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);
@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();
}
/**
* 拷贝本地病毒库数据库到app目录
*/
private void copyVirusDB(){
new AsyncTask(){@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();
}
}
推荐阅读
- PMSJ寻平面设计师之现代(Hyundai)
- android第三方框架(五)ButterKnife
- 基于微信小程序带后端ssm接口小区物业管理平台设计
- Android中的AES加密-下
- 爱琐搭配(喜欢复古、冷淡,像这种双环设计的气质耳环)
- 带有Hilt的Android上的依赖注入
- android|android studio中ndk的使用
- 别墅庭院设计,不同的别墅庭院设计也给人视觉上完全不一样的!
- Android事件传递源码分析
- RxJava|RxJava 在Android项目中的使用(一)