Display progress bar while loading

后端 未结 2 416
迷失自我
迷失自我 2021-01-16 20:16

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

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-16 20:55

    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

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

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

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

提交回复
热议问题