Run progress bar while switching Activity

后端 未结 4 1640
死守一世寂寞
死守一世寂寞 2020-12-20 08:16

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 whic

相关标签:
4条回答
  • 2020-12-20 08:40

    First of all in the Above code, you need to start the Thread using this.

    t.start();

    you can also try below code,

    new Thread ( new Runnable() 
    {
            public void run()
            {
               // Place your Intent Code here
            }
    }.start();
    
    0 讨论(0)
  • 2020-12-20 08:46

    Don't use Thread.sleep() - it is the root to all evil. Instead, use a Handler and its postDelayed( Runnable, time )-method like below:

    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);
    
          final Handler handler = new Handler();
          handler.postDelayed(new Runnable() {
            @Override
            public void run() {
              Intent myIntent = new Intent(view.getContext(), activity2.class);
              startActivityForResult(myIntent, 0);
            }
          }, 5000);
        }
      });   
    }
    
    0 讨论(0)
  • 2020-12-20 08:52

    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);
        });
    
    0 讨论(0)
  • 2020-12-20 08:56

    You better use an AsyncTask for this purpose.Using threads like that in your activity is not proper and can lead to some fails.Check this docs about AsyncTask.

    http://developer.android.com/resources/articles/painless-threading.html

    0 讨论(0)
提交回复
热议问题