我有Splash Activity
。我想在progressStatus达到最大值时启动一个新的Activity。我的问题是我不知道在哪里找到开始意图。我的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();
在清单:
上<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>
我的启动活动为MainActivity Class
,然后NewMainActivity Class
是第二项活动。
答案 0 :(得分:7)
您必须从UI线程调用startActivity(intent)。您可以创建一个如下所示的新方法:
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();
答案 1 :(得分:2)
尝试像这个
一样检查while循环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();
然后根据Thread.sleep()
提出你的条件。
答案 2 :(得分:1)
当进度达到最高级别时,只需向处理程序发送空消息。从运行中直接启动活动。你需要在ui线程中做到这一点
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();
答案 3 :(得分:0)
使用显式意图。
if (progressStatus == progressBar.getMax()) {
Intent intent = new Intent(CurrentActivity.this, MenuActivity.class);
startActivity(intent);
}
编辑:尝试这样
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);
}
});
}