在线程上启动 Activity
Start Activity on a thread
我有一个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>
我的 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();
当进度达到最大值时,只向 Handler 发送空消息 level.From 运行 你不能直接启动 activity 。你需要在 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 (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);
}
});
}
尝试像这样简单地检查 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()
下放你的条件。
我有一个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>
我的 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();
当进度达到最大值时,只向 Handler 发送空消息 level.From 运行 你不能直接启动 activity 。你需要在 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 (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);
}
});
}
尝试像这样简单地检查 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()
下放你的条件。