加载时显示进度条

时间:2012-06-01 03:43:55

标签: android progress-bar

我在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);
    }
}

2 个答案:

答案 0 :(得分:12)

您在这里犯的错误是您将特定时间转储到代码中 你永远不知道得到多少回应。 您应该遵循以下方法

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

步骤2让下载花费自己的时间。但是应该在新线程中完成

步骤3下载完成后,它会发出任务已完成的消息,现在将其删除        进度对话框并继续。

我在这里粘贴示例代码。希望它可以帮到你。

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();

    }

}

答案 1 :(得分:2)

你尝试过Asynctask吗?您的处理过程将在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
    }
}