进度条%完成(下载)在Android中显示

问题描述 投票:2回答:2

我在进度条上显示了已完成的下载百分比有问题。我有一个服务类,可以在其中进行下载。我可以从该课程中获得下载的开始时间和结束时间。在主要活动中,单击按钮后,我必须显示进度%。我听说可以通过AsyncTask实现,但是我不知道它是如何工作的。请帮我提供一些与此相关的示例代码示例。谢谢

android service android-asynctask progress-bar
2个回答
2
投票
这里是一个例子

ProgressDialog mProgressDialog; // instantiate it within the onCreate method mProgressDialog = new ProgressDialog(YourActivity.this); mProgressDialog.setMessage("A message"); mProgressDialog.setIndeterminate(false); mProgressDialog.setMax(100); mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); // execute this when the downloader must be fired DownloadFile downloadFile = new DownloadFile(); downloadFile.execute("the url to the file you want to download");

这里是AsyncTask

private class DownloadFile extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... sUrl) {
    try {
        URL url = new URL(sUrl[0]);
        URLConnection connection = url.openConnection();
        connection.connect();
        // this will be useful so that you can show a typical 0-100% progress bar
        int fileLength = connection.getContentLength();

        // download the file
        InputStream input = new BufferedInputStream(url.openStream());
        OutputStream output = new FileOutputStream("/sdcard/file_name.extension");

        byte data[] = new byte[1024];
        long total = 0;
        int count;
        while ((count = input.read(data)) != -1) {
            total += count;
            // publishing the progress....
            publishProgress((int) (total * 100 / fileLength));
            output.write(data, 0, count);
        }

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

这可以使用从服务下载以及下载管理器类来完成。向details询问此问题。

编辑

完成的百分比是您实际在进度对话框中发布的百分比。如果要显示百分比,则可以使用此百分比(总计* 100 / fileLength)。

int percentage = (total * 100 / fileLength); TextView tv = (TextView)findViewById(R.id.textview); tv.setText("" + percentage);

使用此代码在所需的文本视图中显示百分比。


0
投票
这是一堂课

public class XYZ extends Activity { public static final int DIALOG_DOWNLOAD_PROGRESS = 0; private Button startBtn; private ProgressDialog mProgressDialog; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); startBtn = (Button)findViewById(R.id.startBtn); startBtn.setOnClickListener(new OnClickListener(){ public void onClick(View v) { startDownload(); } }); } private void startDownload() { String url = "http://.jpg"; new DownloadFileAsync().execute(url); } @Override protected Dialog onCreateDialog(int id) { switch (id) { case DIALOG_DOWNLOAD_PROGRESS: mProgressDialog = new ProgressDialog(this); mProgressDialog.setMessage("Downloading file.."); mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); mProgressDialog.setCancelable(false); mProgressDialog.show(); return mProgressDialog; default: return null; } }

class DownloadFileAsync类扩展了AsyncTask {

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

@Override
protected String doInBackground(String... aurl) {
    int count;

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("/sdcard/some_photo_from_gdansk_poland.jpg");

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

@Override
protected void onPostExecute(String unused) {
    dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
}

}}

这是一个xml文件

http://pastie.org/4265745

祝你好运Aamirkhan I。

© www.soinside.com 2019 - 2024. All rights reserved.