Android 加载时显示进度条

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10844116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:11:22  来源:igfitidea点击:

Display progress bar while loading

androidprogress-bar

提问by melvintcs

I have one button in the main.xml which will link to another xml which include information from server. I include progress bar to avoid the blank screen while the system is loading the information. i already done the code as below but it's still not the things i wanted. the code below will "WAIT" for 1000 ms then only will execute the next code. how can i modify it so that the loading "WAIT TIME" will depends on the internet speed, if internet connection is slow, then the progress-bar-screen will show longer.

我在 main.xml 中有一个按钮,它将链接到另一个 xml,其中包含来自服务器的信息。我包括进度条以避免在系统加载信息时出现空白屏幕。我已经完成了如下代码,但它仍然不是我想要的东西。下面的代码将“等待”1000 毫秒,然后才会执行下一个代码。我如何修改它以便加载“等待时间”将取决于互联网速度,如果互联网连接速度较慢,则进度条屏幕将显示更长的时间。

package com.android.myApps;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;

public class MainScr extends Activity {

    private final int WAIT_TIME = 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {       
        super.onCreate(savedInstanceState);     
        setContentView(R.layout.MainScr);       
    }   

    public void onClickCategory(View view)
    {
        findViewById(R.id.mainSpinner1).setVisibility(View.VISIBLE);
        new Handler().postDelayed(new Runnable(){
            @Override
                public void run() {                          
                      Intent mainIntent = new Intent(MainScr.this, Category.class); 
                      MainScr.this.startActivity(mainIntent); 
                      MainScr.this.finish(); 
                      } 
            }, WAIT_TIME);
    }
}

回答by Vipul Shah

The mistake you are doing here is you are dumping specific time into your code You never know how much it will take to get response. You should follow following approach

你在这里犯的错误是你在代码中倾倒了特定的时间 你永远不知道需要多少时间才能得到响应。您应该遵循以下方法

Step 1 Show progress dialog on screen

步骤 1 在屏幕上显示进度对话框

Step 2 Let download take its own time.But it should be done in new thread

第 2 步让下载慢慢来。但它应该在新线程中完成

Step 3 Once download is complete it will raise message that task is done,now remove that progress dialog and proceed.

第 3 步下载完成后,它会提示任务已完成,现在删除该进度对话框并继续。

I am pasting sample code here.Hope it will help you.

我在这里粘贴示例代码。希望它会帮助你。

package com.android.myApps;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class MainScr extends Activity
{
    private Handler handler;
    private ProgressDialog progress;
    private Context context;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        context = AncActivity.this;
        progress = new ProgressDialog(this);
        progress.setTitle("Please Wait!!");
        progress.setMessage("Wait!!");
        progress.setCancelable(false);
        progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);

        handler = new Handler()
        {

            @Override
            public void handleMessage(Message msg)
            {
                progress.dismiss();
                Intent mainIntent = new Intent(context, Category.class);
                startActivity(mainIntent);
                super.handleMessage(msg);
            }

        };
        progress.show();
        new Thread()
        {
            public void run()
            {
                // Write Your Downloading logic here
                // at the end write this.
                handler.sendEmptyMessage(0);
            }

        }.start();

    }

}

回答by Tai Tran

Did you try Asyntask? Your doing process will be update in UI.

你试过 Asyntask 吗?您的操作过程将在 UI 中更新。

public final class HttpTask
        extends
        AsyncTask<String/* Param */, Boolean /* Progress */, String /* Result */> {

    private HttpClient mHc = new DefaultHttpClient();

    @Override
    protected String doInBackground(String... params) {
        publishProgress(true);
        // Do the usual httpclient thing to get the result
        return result;
    }

    @Override
    protected void onProgressUpdate(Boolean... progress) {
        // line below coupled with 
        //    getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS) 
        //    before setContentView 
        // will show the wait animation on the top-right corner
        MyActivity.this.setProgressBarIndeterminateVisibility(progress[0]);
    }

    @Override
    protected void onPostExecute(String result) {
        publishProgress(false);
        // Do something with result in your activity
    }
}