您好我创建了一个用于下载和上传的活动。我使用了一个菜单(项目:下载,上传,删除,退出)。但是当我按下下载按钮时,下载工作开始,但菜单按钮不会消失,直到此任务完成。如果用户没有任何通知,这将是令人沮丧的。如何在按下菜单后立即隐藏菜单以及如何包含进度条,该进度条通知用户有关下载和上传任务的信息。
嗨我有方法:
void downloadFile(SmbFile file,String desPath)
void downloadFolder(SmbFile folder,String tempStore)
void uploadFile(File file,SmbFile destination)
void uploadFolder(File f,SmbFile destination)
请告诉我在以下代码中添加这些方法的位置,这些代码扩展了异步任务...
class helper extends AsyncTask
{
@Override
protected Object doInBackground(Object... arg0) {
// TODO Auto-generated method stub
return null;
}
}
答案 0 :(得分:1)
因为您使用2个差异类型参数,所以您可以使用构造函数(或使用getter,setter作为您想要传递的参数):
public class Helper extends AsyncTask<Integer, Void, Void> {
//The filed you want to pass
private SmbFile file;
private String desPath;
private SmbFile folder;
private String tempStore;
private File file;
private SmbFile destination;
//Create setter
public void setFile(SmbFile file) {
this.file = file;
}
public void setDesPath(String desPath) {
this.desPath = desPath;
}
public void setFolder(SmbFile folder) {
this.folder = folder;
}
public void setTempStore(String tempStore) {
this.tempStore = tempStore;
}
public void setFile(File file) {
this.file = file;
}
public void setDestination(SmbFile destination) {
this.destination = destination;
}
@Override
protected Void doInBackground(Integer... params) {
//check flag to excute exactly method
switch (params[0]) {
case 0:
//Call your download file method
downloadFile(file,desPath);
break;
case 1:
//Call your download folder method
downloadFolder(folder,tempStore);
//etc...
default:
break;
}
return null;
}
}
按下下载菜单后,您可以调用异步任务:
//Download the file
Helper helper = new Helper();
//set file and des path for download file
helper.setFile(your_file);
helper.setDesPath(your_despath);
//Excute the download file method
helper.excute(0);
//Download the folder
//set file and des path for download file
helper.setFolder(your_folder);
helper.setTempStore(your_tempStore);
//Excute the download folder method
helper.excute(1);
//etc...
注意:请仔细阅读this link以了解如何使用AsyncTask。
答案 1 :(得分:0)
我认为你的UI被阻止了,因为下载发生在UI线程中。因此,将您的下载过程移至后台线程。它解决了你的问题。