Get started in the thread

I have one Splash Activity. and I want to start a new action when progressStatus reaches its maximum value. My problem is that I do not know where to find the intention to start. I have a mistake in mine IF statement.

    new Thread(new Runnable() {
        public void run() {
                while (progressStatus < 100) {
                progressStatus += 5;

                }   
                if (progressStatus == progressBar.getMax()) {
                        Intent intent = new Intent(".MENU");
                        startActivity(intent);
                    }
                // Update the progress bar and display the
                // current value in the text view
                handler.post(new Runnable() {
                    public void run() {
                        progressBar.setProgress(progressStatus);
                        textView.setText(progressStatus + "/"
                                + progressBar.getMax());
                    }
                });

                try {
                    // Sleep for 200 milliseconds.
                    // Just to display the progress slowly

                    Thread.sleep(200);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

    }).start();

in my manifest :

<activity
android:name="NewMainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
       <category android:name="android.intent.category.DEFAULT" />
       <action android:name=".MENU" />
</intent-filter>
</activity>

My Splash activity is MainActivity Classthen NewMainActivity Classthe second action.

+4
source share
5 answers

try checking while loop as simple as this

new Thread(new Runnable() {
    @Override
    public void run() {
        while (progressStatus < 100) {
           progressStatus += 5;
           // Update the progress bar and display the 
           //current value in the text view
           handler.post(new Runnable() {
                @Override
                public void run() {
                   progressBar.setProgress(progressStatus);
                   textView.setText(progressStatus+"/"+progressBar.getMax());
                }
            });

            try {
               // Sleep for 200 milliseconds. 
               //Just to display the progress slowly
               Thread.sleep(200); 
               if (progressStatus == 100){
                   Intent intent = new Intent(".MENU");
                   startActivity(intent);
               }

            } catch (InterruptedException e) {
               e.printStackTrace();
            }
         } // while loop
      } // run()
  }).start();

then Thread.sleep()put your condition under .

+2
source

You must call startActivity (intent) from the user interface thread. You can simply create a new method, for example:

public void startActivityFromMainThread(){

   Handler handler = new Handler(Looper.getMainLooper());
   handler.post(new Runnable() {
   @Override
      public void run() {
          Intent intent = new Intent (MainActivity.this, NewMainActivity.class);
          startActivity(intent);
      }
   });
}

Looper.getMainLooper() , .

:

  new Thread(new Runnable() {
    public void run() {
            progressBar.setMax(100);
            progressStatus = 0;

            while (progressStatus < 100) {
               progressStatus += 5;            

               // Update the progress bar and display the
               // current value in the text view
               handler.post(new Runnable() {
                  @Override
                  public void run() {
                      progressBar.setProgress(progressStatus);
                      textView.setText(progressStatus + "/"
                              + progressBar.getMax());
                  }
               });


             try {
                 // Sleep for 200 milliseconds.
                 // Just to display the progress slowly

                 Thread.sleep(200);

              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
           }
           startActivityFromMainThread();
        }

}).start();
+7

Just send an empty message to the handler when the progress reaches its maximum level. Run the direct action. you need to do it in ui thread

private Handler handlerIntentStart = new Handler() {

        /*
         * (non-Javadoc)
         * 
         * @see android.os.Handler#handleMessage(android.os.Message)
         */
        @Override
        public void handleMessage(Message msg) {

            // ****** Acitity class must be added in manifest
            startActivity(new Intent(MainActivity.this,
                    NewMainActivity.class));
        }

    };


new Thread(new Runnable() {
        public void run() {
            while (progressStatus < 100) {
                progressStatus += 5;

                if (progressStatus == progressBar.getMax()) {
                    handlerIntentStart.sendEmptyMessage(0);
                }
                // Update the progress bar and display the
                // current value in the text view
                handler.post(new Runnable() {
                    public void run() {
                        progressBar.setProgress(progressStatus);
                        textView.setText(progressStatus + "/"
                                + progressBar.getMax());
                    }
                });

                try {
                    // Sleep for 200 milliseconds.
                    // Just to display the progress slowly

                    Thread.sleep(200);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

    }).start();
+1
source

Use Explicit Intent.

if (progressStatus == progressBar.getMax()) {
        Intent intent = new Intent(CurrentActivity.this, MenuActivity.class);
        startActivity(intent);
}

EDIT: try to do it

if (progressStatus == progressBar.getMax()) {
     Handler handler = new Handler(Looper.getMainLooper());
     handler.post(new Runnable() {
         public void run() {
                Intent intent = new Intent(CurrentActivity.this, MenuActivity.class);
                CurrentActivity.this.startActivity(intent);
         }
     });
}
0
source

Is it possible to call startActivity in a thread?

0
source

All Articles