请教高手Handle 和AsyncTask的区别
1个回答
展开全部
一、使用Handle:
bn2.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
mpDialog = new ProgressDialog(Page4.this);
mpDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mpDialog.setTitle("导出进度");
//mpDialog.setIcon(R.drawable.icon);
mpDialog.setMessage("查询数据库中......");
mpDialog.setMax(100);
mpDialog.setProgress(0);
//mpDialog.setSecondaryProgress(50);
mpDialog.setCancelable(false);
mpDialog.show();
new Thread()
{
public void run()
{
try
{
database = SQLiteDatabase.openOrCreateDatabase(DBManager.DB_PATH + "/" + DBManager.DB_NAME, null);
String sqlstr;
sqlstr="SELECT * FROM laccell";
cur = database.rawQuery(sqlstr,null);
if (cur != null && cur.getCount()>0)
{
//mCount = 0;
// 构建 Workbook 对象 , 只读 Workbook 对象
//Method 1:创建可写入的 Excel 工作薄
jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(new File(Environment.getExternalStorageDirectory()+"/laccell.xls"));
//Method 2:将 WritableWorkbook 直接写入到输出流
/*
OutputStream os = new FileOutputStream(targetfile);
jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(os);
*/
//加上进度条。
WritableSheet ws = wwb.createSheet("Sheet1", 0);
int line=0;
int maxline=cur.getCount();
for(cur.moveToFirst();!cur.isAfterLast();cur.moveToNext())
{
for(int i=0;i<=cur.getColumnCount()-1;i++)
{
Label labelC = new Label(i,line,cur.getString(i));
ws.addCell(labelC);
}
line++;
try
{
Message msg = new Message();
msg.what = 1;
msg.getData().putInt("line", line);
msg.getData().putInt("maxline", maxline);
msg.getData().putBoolean("saved",false);
handler.sendMessage(msg);//handle发送消息
}
catch (Exception e)
{
handler.obtainMessage(-1).sendToTarget();
e.printStackTrace();
}
}
// 写入 Exel 工作表
wwb.write();
// 关闭 Excel 工作薄对象
wwb.close();
Message msg = new Message();
msg.what = 1;
msg.getData().putInt("line", maxline);
msg.getData().putInt("maxline", maxline);
msg.getData().putBoolean("saved",true);
handler.sendMessage(msg);//handle发送消息
}
else
{
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}.start();
}
});
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
if(!msg.getData().getBoolean("saved"))
{
mpDialog.setMessage("正在导出第"+msg.getData().getInt("line")+"行");
mpDialog.setProgress(100*msg.getData().getInt("line")/msg.getData().getInt("maxline"));
}
else
{
Toast.makeText(Page4.this, "导出完毕!", 1).show();
mpDialog.dismiss();
}
break;
case -1:
Toast.makeText(Page4.this,"导出失败!", 1).show();
break;
}
}
};
二、使用AsyncTask:
bn3.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
new DownloadTask().execute();
}
});
class DownloadTask extends AsyncTask<Void, Integer, Integer>{
//后面尖括号内分别是参数(例子里是线程休息时间),进度(publishProgress用到),返回值 类型
@Override
protected void onPreExecute() {
//第一个执行方法 ,在doInBackground方法执行前执行
mpDialog = new ProgressDialog(Page4.this);
mpDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mpDialog.setTitle("导出进度");
//mpDialog.setIcon(R.drawable.icon);
mpDialog.setMessage("查询数据库中......");
mpDialog.setMax(100);
mpDialog.setProgress(0);
//mpDialog.setSecondaryProgress(50);
mpDialog.setCancelable(false);
mpDialog.show();
}
@Override
protected Integer doInBackground(Void... params) {
//第二个执行方法,onPreExecute()执行完后执行 , 做具体的耗时操作
try
{
database = SQLiteDatabase.openOrCreateDatabase(DBManager.DB_PATH + "/" + DBManager.DB_NAME, null);
String sqlstr;
sqlstr="SELECT * FROM laccell";
cur = database.rawQuery(sqlstr,null);
if (cur != null && cur.getCount()>0)
{
//mCount = 0;
// 构建 Workbook 对象 , 只读 Workbook 对象
//Method 1:创建可写入的 Excel 工作薄
jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(new File(Environment.getExternalStorageDirectory()+"/laccell.xls"));
//Method 2:将 WritableWorkbook 直接写入到输出流
/*
OutputStream os = new FileOutputStream(targetfile);
jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(os);
*/
//加上进度条。
WritableSheet ws = wwb.createSheet("Sheet1", 0);
int line=0;
int maxline=cur.getCount();
for(cur.moveToFirst();!cur.isAfterLast();cur.moveToNext())
{
for(int i=0;i<=cur.getColumnCount()-1;i++)
{
Label labelC = new Label(i,line,cur.getString(i));
ws.addCell(labelC);
}
line++;
publishProgress(line,maxline); //通过推送消息传递数据给onProgressUpdate方法执行
}
// 写入 Exel 工作表
wwb.write();
// 关闭 Excel 工作薄对象
wwb.close();
return 0;
}
else
{
return 1;
}
}
catch (Exception e)
{
e.printStackTrace();
return 1;
}
}
@Override
//在doInBackground方法执行过程中执行
protected void onProgressUpdate(Integer... progress) {
//这个函数在doInBackground调用publishProgress时触发,虽然调用时只有一个参数
//但是这里取到的是一个数组,所以要用progesss[0]来取值
//第n个参数就用progress[n]来取值
//tv.setText(progress[0]+"%");
super.onProgressUpdate(progress);
mpDialog.setMessage("正在导出第"+progress[0]+"行");
mpDialog.setProgress(100*progress[0]/progress[1]);
}
@Override
//在doInBackground方法结束后执行
protected void onPostExecute(Integer result) {
//doInBackground返回时触发,换句话说,就是doInBackground执行完后触发
//这里的result就是上面doInBackground执行后的返回值,所以这里是"执行完毕"
//setTitle(result);
//super.onPostExecute(result);
mpDialog.dismiss();
}
}
bn2.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
mpDialog = new ProgressDialog(Page4.this);
mpDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mpDialog.setTitle("导出进度");
//mpDialog.setIcon(R.drawable.icon);
mpDialog.setMessage("查询数据库中......");
mpDialog.setMax(100);
mpDialog.setProgress(0);
//mpDialog.setSecondaryProgress(50);
mpDialog.setCancelable(false);
mpDialog.show();
new Thread()
{
public void run()
{
try
{
database = SQLiteDatabase.openOrCreateDatabase(DBManager.DB_PATH + "/" + DBManager.DB_NAME, null);
String sqlstr;
sqlstr="SELECT * FROM laccell";
cur = database.rawQuery(sqlstr,null);
if (cur != null && cur.getCount()>0)
{
//mCount = 0;
// 构建 Workbook 对象 , 只读 Workbook 对象
//Method 1:创建可写入的 Excel 工作薄
jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(new File(Environment.getExternalStorageDirectory()+"/laccell.xls"));
//Method 2:将 WritableWorkbook 直接写入到输出流
/*
OutputStream os = new FileOutputStream(targetfile);
jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(os);
*/
//加上进度条。
WritableSheet ws = wwb.createSheet("Sheet1", 0);
int line=0;
int maxline=cur.getCount();
for(cur.moveToFirst();!cur.isAfterLast();cur.moveToNext())
{
for(int i=0;i<=cur.getColumnCount()-1;i++)
{
Label labelC = new Label(i,line,cur.getString(i));
ws.addCell(labelC);
}
line++;
try
{
Message msg = new Message();
msg.what = 1;
msg.getData().putInt("line", line);
msg.getData().putInt("maxline", maxline);
msg.getData().putBoolean("saved",false);
handler.sendMessage(msg);//handle发送消息
}
catch (Exception e)
{
handler.obtainMessage(-1).sendToTarget();
e.printStackTrace();
}
}
// 写入 Exel 工作表
wwb.write();
// 关闭 Excel 工作薄对象
wwb.close();
Message msg = new Message();
msg.what = 1;
msg.getData().putInt("line", maxline);
msg.getData().putInt("maxline", maxline);
msg.getData().putBoolean("saved",true);
handler.sendMessage(msg);//handle发送消息
}
else
{
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}.start();
}
});
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
if(!msg.getData().getBoolean("saved"))
{
mpDialog.setMessage("正在导出第"+msg.getData().getInt("line")+"行");
mpDialog.setProgress(100*msg.getData().getInt("line")/msg.getData().getInt("maxline"));
}
else
{
Toast.makeText(Page4.this, "导出完毕!", 1).show();
mpDialog.dismiss();
}
break;
case -1:
Toast.makeText(Page4.this,"导出失败!", 1).show();
break;
}
}
};
二、使用AsyncTask:
bn3.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
new DownloadTask().execute();
}
});
class DownloadTask extends AsyncTask<Void, Integer, Integer>{
//后面尖括号内分别是参数(例子里是线程休息时间),进度(publishProgress用到),返回值 类型
@Override
protected void onPreExecute() {
//第一个执行方法 ,在doInBackground方法执行前执行
mpDialog = new ProgressDialog(Page4.this);
mpDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mpDialog.setTitle("导出进度");
//mpDialog.setIcon(R.drawable.icon);
mpDialog.setMessage("查询数据库中......");
mpDialog.setMax(100);
mpDialog.setProgress(0);
//mpDialog.setSecondaryProgress(50);
mpDialog.setCancelable(false);
mpDialog.show();
}
@Override
protected Integer doInBackground(Void... params) {
//第二个执行方法,onPreExecute()执行完后执行 , 做具体的耗时操作
try
{
database = SQLiteDatabase.openOrCreateDatabase(DBManager.DB_PATH + "/" + DBManager.DB_NAME, null);
String sqlstr;
sqlstr="SELECT * FROM laccell";
cur = database.rawQuery(sqlstr,null);
if (cur != null && cur.getCount()>0)
{
//mCount = 0;
// 构建 Workbook 对象 , 只读 Workbook 对象
//Method 1:创建可写入的 Excel 工作薄
jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(new File(Environment.getExternalStorageDirectory()+"/laccell.xls"));
//Method 2:将 WritableWorkbook 直接写入到输出流
/*
OutputStream os = new FileOutputStream(targetfile);
jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(os);
*/
//加上进度条。
WritableSheet ws = wwb.createSheet("Sheet1", 0);
int line=0;
int maxline=cur.getCount();
for(cur.moveToFirst();!cur.isAfterLast();cur.moveToNext())
{
for(int i=0;i<=cur.getColumnCount()-1;i++)
{
Label labelC = new Label(i,line,cur.getString(i));
ws.addCell(labelC);
}
line++;
publishProgress(line,maxline); //通过推送消息传递数据给onProgressUpdate方法执行
}
// 写入 Exel 工作表
wwb.write();
// 关闭 Excel 工作薄对象
wwb.close();
return 0;
}
else
{
return 1;
}
}
catch (Exception e)
{
e.printStackTrace();
return 1;
}
}
@Override
//在doInBackground方法执行过程中执行
protected void onProgressUpdate(Integer... progress) {
//这个函数在doInBackground调用publishProgress时触发,虽然调用时只有一个参数
//但是这里取到的是一个数组,所以要用progesss[0]来取值
//第n个参数就用progress[n]来取值
//tv.setText(progress[0]+"%");
super.onProgressUpdate(progress);
mpDialog.setMessage("正在导出第"+progress[0]+"行");
mpDialog.setProgress(100*progress[0]/progress[1]);
}
@Override
//在doInBackground方法结束后执行
protected void onPostExecute(Integer result) {
//doInBackground返回时触发,换句话说,就是doInBackground执行完后触发
//这里的result就是上面doInBackground执行后的返回值,所以这里是"执行完毕"
//setTitle(result);
//super.onPostExecute(result);
mpDialog.dismiss();
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询