gpt4 book ai didi

android - Android中进度条结束时如何更改 Activity ?

转载 作者:行者123 更新时间:2023-11-30 01:01:18 25 4
gpt4 key购买 nike

我想在进度条结束后更改 Activity 。表示进度条线程结束。我在线程后添加 activity2。但是 activity2 在应用程序运行时启动。为什么会这样?

   @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

progressBar = (ProgressBar) findViewById(R.id.progressBar1);
new Thread(new Runnable() {
public void run() {
while (progressStatus < 100) {
progressStatus += 1;
handler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressStatus);
}
});
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();


Intent myIntent = new Intent(getApplicationContext(), activity2.class);
myIntent.putExtra("key", "......"); //Optional parameters
startActivity(myIntent);

最佳答案

您已经创建了一个新线程,它负责进度条,而在主线程上执行您的新 Activity 代码。您需要将启动 Activity 代码放在同一个线程中。

你可以做的是:

new Thread(new Runnable() {
public void run() {
while (progressStatus <= 100) {
progressStatus += 1;
handler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressStatus);
}
});
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

Intent myIntent = new Intent(getApplicationContext(), activity2.class);
myIntent.putExtra("key", "......"); //Optional parameters
startActivity(myIntent);
}
}).start();

关于android - Android中进度条结束时如何更改 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39347115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com