0

嗨,我创建了一个用于下载和上传的活动。我使用了一个菜单(项目:下载、上传、删除、退出)。但是当我按下下载按钮时,下载工作开始了,但菜单按钮在此任务完成之前不会消失。没有任何通知的用户会感到沮丧。如何在按下菜单后立即隐藏菜单以及如何包含进度条,通知用户下载和上传任务。

嗨我有方法:

 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;
}

 }
4

2 回答 2

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...

注意:请仔细阅读此链接以了解如何使用 AsyncTask。

于 2012-05-05T14:29:02.970 回答
0

我认为您的 UI 被阻止,因为下载发生在 UI 线程中。因此,将您的下载过程移至后台线程。它解决了你的问题。

于 2012-05-05T12:36:29.110 回答