我有一个Splash Activity
。当 progressStatus 达到最大值时,我想开始一个新的 Activity 。我的问题是我不知道在哪里可以找到开始 Intent 。我的 IF 语句
有错误。
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();
在我的 list 上:
<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>
我的 Splash Activity 是 MainActivity Class
然后 NewMainActivity Class
是第二个 Activity 。
最佳答案
您必须从 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();
关于android - 在线程上启动 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28089803/