Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
623 views
in Technique[技术] by (71.8m points)

android - Run progress bar while switching Activity

im stuck in a situation where i am switching from activity 1 to activity 2. i am using Thread.sleep(5000) to start another activity after 5 seconds But the progress bar which i want to run for five seconds also sleeps with the first activity Pleaze help me as to when i click next Button on first activity a progress bar shoud run for five sec and then activity should be loaded My Code is:

    public class Activity1 extends Activity  {          
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button next = (Button) findViewById(R.id.B);
    final ProgressBar  p=(ProgressBar) findViewById(R.id.pr);
    next.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
              p.setVisibility(4);
            Thread t=new Thread();
            try{                    
                t.sleep(5000);              

        }
            catch(Exception e){}

            Intent myIntent = new Intent(view.getContext(), activity2.class);
            startActivityForResult(myIntent, 0);
        }

    });   

}}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Change your OnClickListener for this. This will not block your main thread, as you are doing (that explains why your application freezes for 5 seconds):

next.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        new AsyncTask<Integer, Long, Boolean>()
        {
            ProgressDialog pd;

            @Override
            protected Boolean doInBackground(Integer... params)
            {
                pd = new ProgressDialog(Activity1.this);
                pd.setTitle("Loading Activity");
                pd.setMessage("Please Wait ...");
                pd.setMax(params[0]);
                pd.setIndeterminate(false);
                pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

                publishProgress(0L);

                long start = System.currentTimeMillis();
                long waitTime = params[0] * 1000;
                try
                {
                    while (System.currentTimeMillis() - start < waitTime)
                    {
                        Thread.sleep(500);
                        publishProgress(System.currentTimeMillis() - start);
                    }
                }
                catch (Exception e)
                {
                    return false;
                }

                return true;
            }

            @Override
            protected void onProgressUpdate(Long... values)
            {
                if (values[0] == 0)
                {
                    pd.show();
                }
                else
                {
                    pd.setProgress((int) (values[0] / 1000));
                }
            }

            @Override
            protected void onPostExecute(Boolean result)
            {
                pd.dismiss();
                Intent myIntent = new Intent(view.getContext(), activity2.class);
                startActivityForResult(myIntent, 0);
            }
        }.execute(5);
    });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...