繁体   English   中英

加载URL时Android显示进度对话框

[英]Android display progressdialog while loading URLs

我的问题真的很奇怪。 场景非常简单:只要我的活动正在加载网址内容,我就不会显示进度对话框。

首先,我试图单独显示ProgressDialog,内部没有任何特殊功能。 工作得很好。 但是只要我添加了一个加载url的函数,程序就会先加载这些url,然后显示进度条。 这是我的代码:

progDialog = new ProgressDialog(this);
progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progDialog.setMessage("Logging in ...");
progDialog.show();

client = new GameClient(context, universe, username, password);
client.login();

progDialog.dismiss();

没什么特别的。 但由于某种原因,活动首先制作“登录部分”然后尝试显示对话框,但它无论如何都不会显示...

你能给我一些解决问题的提示吗?

您需要在单独的线程中运行登录,因此它不会阻止UI线程,就像这样

...
progDialog.show();

new Thread(new Runnable() {
    @Override
    public void run() {
        client = new GameClient(context, universe, username, password);
        client.login();

        progDialog.dismiss();
    }
}).start();

或使用AsyncTask 请参阅http://developer.android.com/resources/articles/painless-threading.html

使用ProgressDialog类来显示它。

/*****************************************
 * AsyncTask Class to Parse and Display
 ******************************************/
class AsyncTaskClassName extends AsyncTask<Void,Void, Void>{
    ProgressDialog progressDialog = null;

    /* ***********************************
     * Pre-Execute Method 
     * ********************************** */
    @Override
    protected void onPreExecute() {
        progressDialog = util.getProgressDialog(ActivityClassName.this, "Please wait...", "Parsing List...    ");
           //ActivityClassName -> The Name of the Activity Class you want to show ProgressDialog
        // progressDialog.hide();
        progressDialog.show();

        /* Do your Pre-Execute Configuration */
    }

    /* ***********************************
     * Execute Method 
     * ********************************** */
    @Override
    protected Void doInBackground(Void... arg0) {
        /* Do yourxec Task ( Load from URL) and return value */
        return null;
    }

    /* ***********************************
     * Post-Execute Method 
     * ********************************** */
    @Override
    protected void onPostExecute(Void result) {
        progressDialog.dismiss();

                    /* Do your Post -Execute Tasks */
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM