Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start Activity on a thread

I have a Splash Activity. and I want to start a new Activity when the progressStatus reach its max value. My problem is i dont know where to locate the start intent. I have an error on my 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();

on 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 Class then NewMainActivity Class is the second activity.

like image 388
user3698267 Avatar asked Jan 22 '15 13:01

user3698267


People also ask

Is an activity a thread?

Activity is any ui that user sees when using the app , a thread is a place where your tasks are running ..

What is start activity?

Starting activities or services. To start an activity, use the method startActivity(intent) . This method is defined on the Context object which Activity extends. The following code demonstrates how you can start another activity via an intent.

How do I open background activity?

Intent intent = new Intent(myActivity. this, myActivity. class); PendingIntent contentIntent = PendingIntent. getActivity(this, REQUEST_CODE, intent, 0); notification.


3 Answers

You have to call the startActivity(intent) from the UI thread. You can just create a new method like the following:

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

The Looper.getMainLooper() verifies that this will be executed on the Main thread.

Then your code will look like this:

  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();
like image 96
Alexios Karapetsas Avatar answered Oct 02 '22 02:10

Alexios Karapetsas


try to check the while loop as simply 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 under the Thread.sleep() put your condition.

like image 37
icecream Avatar answered Oct 02 '22 03:10

icecream


Just Send empty message to Handler when progress reached to max level.From run u cant directly start activity . 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();
like image 34
user1140237 Avatar answered Oct 02 '22 02:10

user1140237