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( ...
随机推荐
- js 删除数组方法
今天遇到一个比较脑残的问题 ,在在用js删除数组的时候 delete 数组[下标]的方法删除数组时,该数组的下标变为null,但是数组的长度并没有发生相应的变化 转而使用splice(小标,第N个)删 ...
- C# 数据结构 栈 Stack
栈和队列是非常重要的两种数据结构,栈和队列也是线性结构,线性表.栈和队列这三种数据结构的数据元素和元素的逻辑关系也相同 差别在于:线性表的操作不受限制,栈和队列操作受限制(遵循一定的原则),因此栈和队 ...
- Xsd: Xml序列化、反序列化的利器
下面讲述根据xml生成对应序列化反序列化类的过程,xml需要首先转化为xsd,然后再生成为实体类.其中,XSD是XML Schema Definition的缩写. 1.制作xml文件: <? ...
- 数据结构练习 00-自测5. Shuffling Machine (20)
Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techn ...
- LightOj_1274 Beating the Dataset
题目链接 题意: 给一个文档, 这个文档由yes .no 组成, 共有s个byte, 共有n个yes.no. 假设yes的个数为yes_num, no的个数为no_num. 将这n个数进行排列, 对于 ...
- 修改weblogic11g的JDK版本
1:进入Weblogic域文件夹下面 [wzh@localhost bin]$ pwd/app/wzh/oracle/middleware/user_projects/domains/base_dom ...
- 上网必备AdBlock,远离广告!
浏览器广告拦截插件,绝对上网必备... 从此告别讨厌的百度边栏广告!!! https://getadblock.com/ https://adblockplus.org/zh_CN/
- 【UVALive - 3713】Astronauts (2-SAT)
题意: 有n个宇航员,按照年龄划分,年龄低于平均年龄的是年轻宇航员,而年龄大于等于平均年龄的是老练的宇航员. 现在要分配他们去A,B,C三个空间站,其中A站只有老练的宇航员才能去,而B站是只有年轻的才 ...
- QSplashScreen开机画面(不断的repaint)
QApplication a(argc, argv); QPixmap pixmap(":/Image/start.png");//绑定启动图片 QSplashScre ...
- 大整数相乘的C实现
//之前有个测试这个题没做完,现在把它做完,通过这个程序可以对乘法了解更深刻.分析:运用整数乘法,当然进制越高越好,考虑到乘法不要越界,故考虑进制底数N应该满 //足,N^2<2^32次方.所以 ...