我想要一个进度条,但需要一个旋转进度对话框

发布于 2024-09-11 02:07:06 字数 2344 浏览 12 评论 0原文

我正在使用公共 AsynTask 来下载数据,并且我试图显示一个进度条来显示下载进度。我认为我的代码是正确的,但我得到的只是一个旋转进度对话框。我错过了什么吗?为什么进度条不显示?这是代码。 感谢您的指点。

public class FileDownloader extends AsyncTask<String, Integer, Void>
{
private Context _appContext;
private HttpURLConnection _urlConn;
private ProgressDialog _progressDia = null;
private DialogInterface.OnCancelListener _progDiaCancelListener = new DialogInterface.OnCancelListener()
{
    /**
     * When the progress dialog is canceled, stop the GET request.
     */
    public void onCancel(DialogInterface dialog) 
    {
        FileDownloader.this.cancel(true);
    }
};

/**
 * Constructor.
 * @param appContext
 */
public FileDownloader(Context appContext)
{
    _appContext = appContext;
    _progressDia = new ProgressDialog(_appContext, ProgressDialog.STYLE_HORIZONTAL);
    _progressDia.setMax(100);
    _progressDia.setTitle(_appContext.getString(R.string.diaHeader1));
    _progressDia.setMessage(_appContext.getString(R.string.diaBody1));
    _progressDia.setCancelable(true);
    _progressDia.setIndeterminate(false);
    _progressDia.setOnCancelListener(_progDiaCancelListener);
}

// Runs on the UI thread
@Override
protected void onPreExecute() 
{
         _progressDia.setProgress(0);
         _progressDia.show();                           
}

@Override
protected Void doInBackground(String... args) 
{
    String dloadURL = args[0],
           saveLoc = args[1];
            ...
            ...
        while((len = input.read(buf)) > 0)
        {
            output.write(buf, 0, len);
            total += len;
            publishProgress((int)total * 100/lenghtOfFile);
        }
            ...
            ...
    }
    catch(SocketTimeoutException ex)
    {   
    }
    finally
    {
         ...
}

// This is executed on main UI thread.
@Override
protected void onProgressUpdate(Integer... values) 
{
     _progressDia.setProgress(values[0]);
}

@Override
protected void onCancelled() 
{
         ...
}

// This is executed on main UI thread.
@Override
protected void onPostExecute(Void result) 
{
        removeProgressDialog();
        ...
}

    /**
     * Remove the message dialog, if still showing.
     */
    private void removeProgressDialog()
    {
        if(_progressDia != null && _progressDia.isShowing())
        _progressDia.dismiss();
    }
}

I am using a public AsynTask to download data, and I am trying to show a progress bar which would show the download progress. I think I have the code right, but all I get is a spinner progressdialog. Am I missing something? Why isn't the progress bar showing up? Here is the code.
Thanks for any pointers.

public class FileDownloader extends AsyncTask<String, Integer, Void>
{
private Context _appContext;
private HttpURLConnection _urlConn;
private ProgressDialog _progressDia = null;
private DialogInterface.OnCancelListener _progDiaCancelListener = new DialogInterface.OnCancelListener()
{
    /**
     * When the progress dialog is canceled, stop the GET request.
     */
    public void onCancel(DialogInterface dialog) 
    {
        FileDownloader.this.cancel(true);
    }
};

/**
 * Constructor.
 * @param appContext
 */
public FileDownloader(Context appContext)
{
    _appContext = appContext;
    _progressDia = new ProgressDialog(_appContext, ProgressDialog.STYLE_HORIZONTAL);
    _progressDia.setMax(100);
    _progressDia.setTitle(_appContext.getString(R.string.diaHeader1));
    _progressDia.setMessage(_appContext.getString(R.string.diaBody1));
    _progressDia.setCancelable(true);
    _progressDia.setIndeterminate(false);
    _progressDia.setOnCancelListener(_progDiaCancelListener);
}

// Runs on the UI thread
@Override
protected void onPreExecute() 
{
         _progressDia.setProgress(0);
         _progressDia.show();                           
}

@Override
protected Void doInBackground(String... args) 
{
    String dloadURL = args[0],
           saveLoc = args[1];
            ...
            ...
        while((len = input.read(buf)) > 0)
        {
            output.write(buf, 0, len);
            total += len;
            publishProgress((int)total * 100/lenghtOfFile);
        }
            ...
            ...
    }
    catch(SocketTimeoutException ex)
    {   
    }
    finally
    {
         ...
}

// This is executed on main UI thread.
@Override
protected void onProgressUpdate(Integer... values) 
{
     _progressDia.setProgress(values[0]);
}

@Override
protected void onCancelled() 
{
         ...
}

// This is executed on main UI thread.
@Override
protected void onPostExecute(Void result) 
{
        removeProgressDialog();
        ...
}

    /**
     * Remove the message dialog, if still showing.
     */
    private void removeProgressDialog()
    {
        if(_progressDia != null && _progressDia.isShowing())
        _progressDia.dismiss();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一袭水袖舞倾城 2024-09-18 02:07:06

可能您忘记设置 dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

请参阅适用于我的示例代码:

ProgressDialog dialog;

@Override
protected void onPreExecute() {
    dialog = new ProgressDialog(this);
    dialog.setMessage("Matching progress");
    dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    dialog.setMax(100);
    dialog.setCancelable(false);
    dialog.show();
}

/*
 * (non-Javadoc)
 * @see android.os.AsyncTask#doInBackground(Params[])
 */
@Override
protected Void doInBackground(Void... params) {

    return null;

}

protected void onPostExecute(Void result) {
    dialog.hide();
    dialog = null;
}

Probably you forget to set dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

See this example code that works me:

ProgressDialog dialog;

@Override
protected void onPreExecute() {
    dialog = new ProgressDialog(this);
    dialog.setMessage("Matching progress");
    dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    dialog.setMax(100);
    dialog.setCancelable(false);
    dialog.show();
}

/*
 * (non-Javadoc)
 * @see android.os.AsyncTask#doInBackground(Params[])
 */
@Override
protected Void doInBackground(Void... params) {

    return null;

}

protected void onPostExecute(Void result) {
    dialog.hide();
    dialog = null;
}
南…巷孤猫 2024-09-18 02:07:06

看来您正在为 ProgressDialog 使用两个参数构造函数,文档建议该构造函数用于上下文(第一个参数)和主题 id(第二个参数)。

http://developer.android.com/参考/android/app/ProgressDialog.html#ProgressDialog(android.content.Context, int)

因此,当您认为将 ProgressStyle 设置为 STYLE_HORIZONTAL 时,您根本没有这样做,您正在设置主题 id 我建议使用一个接受 Context 的 arg 构造函数,然后按照 Pentium10 的建议

调用 _progressDia.setProgressStyle(ProgressStyle.STYLE_HORIZONTAL);

It appears that you are using the two arg constructor for ProgressDialog, which the documentation suggests is for the Context (first arg) and the theme id (the second arg).

http://developer.android.com/reference/android/app/ProgressDialog.html#ProgressDialog(android.content.Context, int)

So while you THINK you are setting the ProgressStyle to STYLE_HORIZONTAL, you are not doing that at all, you are setting the theme id to something that is likely not a valid theme id.

I would suggest using the one arg constructor that takes in a Context, and then do as Pentium10 suggests and call _progressDia.setProgressStyle(ProgressStyle.STYLE_HORIZONTAL);

2024-09-18 02:07:06

你也可以在xml文件中给它这个标签

style="@android:style/Widget.ProgressBar.Horizo​​ntal"

you can also give it this tag in the xml file

style="@android:style/Widget.ProgressBar.Horizontal"

GRAY°灰色天空 2024-09-18 02:07:06

执行进度条输入
在你的android项目中,当前的intent XML文件作为进度条,

保存后也检查它
在 R.java 文件中,进度条的 id

应该在下载开始时调用,

例如

startActivity(new Intent(this, Progress.class));

do entry of progress bar
in your android project current intent XML file as a progress bar

also check it after saving
in R.java file for id of your progress bar

the intent should be called whenever download is started

like

startActivity(new Intent(this, Progress.class));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文