android异步任务载入数据界面实现
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(); } }
android异步任务载入数据界面实现的更多相关文章
- android listView 滑动载入数据  该数据是服务端获取的
		
package com.sunway.works.applycash; import java.util.ArrayList; import java.util.Calendar; import ja ...
 - Android  异步加载数据   AsyncTask异步更新界面
		
官方文档: AsyncTask enables proper and easy use of the UI thread. This class allows to perform backg ...
 - Android fragment 切换载入数据卡顿问题
		
接着上一篇项目的进度.上一篇讲了怎样利用fragment来实现下拉菜单.公用菜单,以实现切换主界面数据的功能,这时候遇到的问题是:使用了fragment的切换界面方法.但载入的数据太多.用户从一个界面 ...
 - Android异步载入全解析之使用AsyncTask
		
Android异步载入全解析之使用AsyncTask 概述 既然前面提到了多线程,就不得不提到线程池,通过线程池,不仅能够对并发线程进行管理.更能够提高他们运行的效率.优化整个App.当然我们能够自己 ...
 - Android异步载入全解析之使用多线程
		
异步载入之使用多线程 初次尝试 异步.异步,事实上说白了就是多任务处理.也就是多线程执行.多线程那就会有各种问题,我们一步步来看.首先.我们创建一个class--ImageLoaderWithoutC ...
 - android中listview分页载入数据
		
前段时间做的新浪微博项目一直想实现listview分页载入数据,今天最终实现了,哈哈!感觉挺好的,今天又写了个demo给大家分享下. 首先说下listview的优化方案,这也是面试中常考的题目.优化方 ...
 - Android异步载入全解析之IntentService
		
Android异步载入全解析之IntentService 搞什么IntentService 前面我们说了那么多,异步处理都使用钦定的AsyncTask.再不济也使用的Thread,那么这个Intent ...
 - Android学习笔记_36_ListView数据异步加载与AsyncTask
		
一.界面布局文件: 1.加入sdcard写入和网络权限: <!-- 访问internet权限 --> <uses-permission android:name="andr ...
 - android 网络异步加载数据进度条
		
ProgressDialog progressDialog = null; public static final int MESSAGETYPE = 0; private void execute( ...
 
随机推荐
- linux自定义开机启动服务
			
转 http://www.cnblogs.com/jimeper/archive/2013/03/12/2955687.html 手工创建服务 1.在/etc/rc.d/init.d目录下创建shel ...
 - 支付宝接口使用文档说明 支付宝异步通知(notify_url)与return_url.
			
支付宝接口使用文档说明 支付宝异步通知(notify_url)与return_url. 现支付宝的通知有两类. A服务器通知,对应的参数为notify_url,支付宝通知使用POST方式 B页面跳转通 ...
 - 《python源码剖析》笔记一——python编译
			
1.python的架构: 2.python源码的组织结构: 3.windows环境下编译python:
 - coder
			
#include <iostream>#include <GL/glut.h>using std::cout;using std::endl;float windowWidth ...
 - ASP.NET MVC轻教程 Step By Step 13——页面布局
			
一般在一个网站中页面会使用相同的结构和元素,如果每个页面都要重复添加这些元素,不仅繁琐更会给我们后期维护带来大麻烦.所以我们采用网页模板之类的技术,将固定不变的元素放入模板,同时留下一些占位符供页面各 ...
 - golang_protobuf环境搭建
			
搭建golang使用rotobuf使用环境 一 安装protobuf: 1 下载protobuf源码:https://github.com/google/protobuf 2 进入源码目录: ./au ...
 - Uva_11916 Emoogle Grid
			
题目链接 题意: 有个N X M的棋盘, 有K种颜色, 有B个不可涂色的位置, 共有R种涂色方案. 1)每个可涂色的位置必须涂上一种颜色 2)不可涂色位置不能涂色 3)每个位置必须从K种颜色中选出一种 ...
 - bzoj 2631: tree 动态树+常数优化
			
2631: tree Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 1716 Solved: 576[Submit][Status] Descrip ...
 - 如何使用 Laravel Facades ?
			
Facade 布局是在面向对象编程中经常使用的一种软件设计布局方式.Facade 实际上是一种包括复杂函数库的类,提供了更加简洁易读的接口.Facade 布局还能为一组结构复杂.设计简陋的 API 提 ...
 - struts2中获取request、response,与android客户端进行交互(文件传递给客户端)
			
用struts2作为服务器框架,与android客户端进行交互需要得到request.response对象. struts2中获取request.response有两种方法. 第一种:利用Servle ...