下载后检查条件

时间:2015-11-12 16:03:49

标签: android eclipse

我的活动中有一个类,它下载一个mp3文件,然后返回一些数据。在下载完成之前,它会检查一个条件。下载过程在后台完成。

我想在下载完成后执行条件检查。你能提一个这样做的方法吗?

更新      它工作正常,但在完成下载resultFromDownload = getFromSdcard();执行。 但之后我希望它返回此语句执行:

  return resultFromDownload;

但在onPostExecute之后没有任何事情发生。

 private String downloadSomeFiles(String filename) {

 DownloadImage downloadimage = new DownloadImage();
 downloadimage.execute(filename);
  **return resultFromDownload;**
}

这个AsyncTask:

private  class DownloadImage extends AsyncTask<String, Integer, Boolean> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    // This is run in a background thread
    @Override
    protected Boolean doInBackground(String... params) {
        // get the string from params, which is an array

        Boolean resul = true;
        try{
            DownloadRequest downloadRequestLrc = new DownloadRequest()
                    .downloadPath("http://192.168.1.108/music+".mp3)
                    .filepath(G.DIR_APP + "/" + music+ ".mp3")
                    .simulate(true)
                    .download();
            downloadRequests =downloadRequestLrc;

        } catch (Exception e){
            resul=false;
        }

        return resul;
    }

    // This is called from background thread but runs in UI
    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);

        // Do things like update the progress bar
    }

    // This runs in UI when background thread finishes
    @Override
    protected void onPostExecute(Boolean result) {
       if(result){
            // Log.i("resulyyyy : ",result);

            **resultFromDownload= getFromSdcard();**

            Log.i("result1 : ",resultFromDownload);

       } else {
            resultFromDownload="";
            Log.i("result2 : ",resultFromDownload);

       }


        // Do things like hide the progress bar or change a TextView
    }
}

1 个答案:

答案 0 :(得分:0)

您可以创建一个AsyncTask类,以@codeMagic表示。

一个例子是:

private class DownloadImage extends AsyncTask<String, Integer, Boolean> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    // This is run in a background thread
    @Override
    protected Boolean doInBackground(String... params) {
        // get the string from params, which is an array

        Boolean resul = true;
        try{
            DownloadRequest downloadRequestLrc = new DownloadRequest()
                    .downloadPath("http://192.168.1.108/music.mp3")
                    .filepath(G.DIR_APP + "/" + params[0] + ".mp3")
                    .simulate(true)
                    .download();
            downloadRequests =downloadRequestLrc;

        }
        catch (Exception e){
            resul=false;
        }

        return resul;
    }

    // This is called from background thread but runs in UI
    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);

        // Do things like update the progress bar
    }

    // This runs in UI when background thread finishes
    @Override
    protected void onPostExecute(Boolean result) {
        if(result){
            //All allright
        }
        else{
            //Problem!
        }

        // Do things like hide the progress bar or change a TextView
    }
}

电话会是:

DownloadImage downloadimage = new DownloadImage();
                                downloadimage.execute(filename));