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.
Activity is any ui that user sees when using the app , a thread is a place where your tasks are running ..
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.
Intent intent = new Intent(myActivity. this, myActivity. class); PendingIntent contentIntent = PendingIntent. getActivity(this, REQUEST_CODE, intent, 0); notification.
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();
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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With