0

您好,我正在编写一个下载项目,但我被困在这里。

@Override
protected void onProgressUpdate(Integer... progress) {

}

//I'm using in my mainactivity like that
Download download = new Download();
download.execute("http://");

我想在更新时取得进展,是的,我可以使用 onProgressUpdate 来做到这一点,但我想知道我能否从我的 mainactivity 中获得它,我的意思是我在哪里调用类。我喜欢那种动态类,因为我可以在每个项目中轻松使用它们。谢谢你顺便原谅我的语法。

4

2 回答 2

0

我可以在这里看到两个选项:

您可以使用构造函数将要显示进度的对象传递给 AsyncTask,然后将其存储:

private ProgressObject mObject;
public void MyAsyncTask(ProgressObject object) {
   mObject = object
}

onProgress() 方法中的更新:

object.setProgress();   //assuming the ProgressObject has a method setProgress() obviously

或者您可以设置某种侦听器:

public interface ProgressListener() {
     public void onProgress(int progress);
}
private mProgressListener;
public void MyAsyncTask(ProgressListener listener) {
    mProgressListener = listener;
}

然后在 onProgress() 方法中使用它:

mProgressListener.onProgress(80);

只是一些例子,不多,但我希望有所帮助。

于 2013-08-05T12:21:27.073 回答
0

用这个

       class DownloadFileAsync extends AsyncTask<String, String, String> {

@SuppressWarnings("deprecation")
@Override
protected void onPreExecute() {
    super.onPreExecute();

    showDialog(DIALOG_DOWNLOAD_PROGRESS);
}

@Override
protected String doInBackground(String... aurl) {
    int count;
    File root = android.os.Environment.getExternalStorageDirectory();               
     //
    File dir = new File (root.getAbsolutePath()+"/downoad");  //make ur folder to put download
    if(dir.exists()==false) {
             dir.mkdirs();
         }
    File file = new File(dir, "enter file name");
        try {

                URL url = new URL(aurl[0]);
                URLConnection conexion = url.openConnection();
                conexion.connect();

                int lenghtOfFile = conexion.getContentLength();
                Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);

                InputStream input = new BufferedInputStream(url.openStream());
                OutputStream output = new FileOutputStream(file);

                byte data[] = new byte[1024];

                long total = 0;

                while ((count = input.read(data)) != -1) 
                    {
                        total += count;
                        publishProgress(""+(int)((total*100)/lenghtOfFile));
                        output.write(data, 0, count);
                    }

                output.flush();
                output.close();
                input.close();
        } catch (Exception e) {}
        return null;

}

protected void onProgressUpdate(String... progress) {
     Log.d("ANDRO_ASYNC",progress[0]);
     mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}

@SuppressWarnings("deprecation")
@Override
protected void onPostExecute(String unused) {
    dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
    Toast.makeText(DisplayActivity.this,"Successfully downloaded in phone memory.", Toast.LENGTH_SHORT).show();
}
   }

怎么打电话?

    new DownloadFileAsync().execute("your URL");
于 2013-08-05T12:27:03.483 回答