几秒钟后Android更改活动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24745546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android change Activity after few seconds
提问by earny1989
I need some help with my first Android project. I want to write a app which is showing you a picture with a ImageView for a few seconds I would say so about 4 seconds and after that it change to a second activity which shows a button(only for testing).
我的第一个 Android 项目需要一些帮助。我想编写一个应用程序,它向您显示带有 ImageView 的图片几秒钟,我会说大约 4 秒钟,然后它更改为显示按钮的第二个活动(仅用于测试)。
My Problem is that my app after I started it in my AVD jump over the picture and shows immediately the button.
我的问题是我的应用程序在我的 AVD 中启动它后跳过图片并立即显示按钮。
How can I fix it? I looked up so long and tried so many things I hope someone of you have a idea :)
我该如何解决?我查了很久,尝试了很多东西,希望你们中有人有想法:)
Thanks for helping
谢谢你的帮助
Here my Code of my MainActivity:
这是我的 MainActivity 代码:
package com.example.parkourspots;
public class MainActivity extends Activity {
private ViewTreeObserver vto;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final View myLayout = findViewById(R.id.startscreen);
vto = myLayout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
@Override
public void onGlobalLayout(){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent intent = new Intent(MainActivity.this, select_activity_class.class);
startActivity(intent);
}
});
}}
回答by Chefes
Check this code.
检查此代码。
package com.example.parkourspots;
public class MainActivity extends Activity {
private static int TIME_OUT = 4000; //Time to launch the another activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final View myLayout = findViewById(R.id.startscreen);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(MainActivity.this, ActivityTwo.class);
startActivity(i);
finish();
}
}, TIME_OUT);
}
});
回答by Plinio.Santos
You can try:
你可以试试:
public class MainActivity extends Activity {
private Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mHandler.postDelayed(new Runanble() {
@Override
public void run() {
Intent intent = new Intent(MainActivity.this, select_activity_class.class);
startActivity(intent);
}
}, 4000); // 4 seconds
}
}
In addiction, you may add this for your second activity declaration in AndroidManifest: android:finishOnTaskLaunch="true"
在上瘾中,您可以在 AndroidManifest 中为您的第二个活动声明添加以下内容: android:finishOnTaskLaunch="true"
回答by fecub
never stall the UI thread. The UI thread is responsible for keeping your app feeling responsive.
永远不要停止 UI 线程。UI 线程负责让您的应用程序感觉响应。
But this is an fast and alternative solution for your problem.
但这是解决您问题的快速替代解决方案。
public class MyActivity extends Activity {
private Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mHandler.postDelayed(new Runnable() {
public void run() {
doStuff();
}
}, 5000);
}
private void doStuff() {
Intent intent = new Intent(MainActivity.this, select_activity_class.class);
startActivity(intent);
}
}
Then 5 seconds after the intent must start.
然后意图必须在 5 秒后开始。
But i recommend async task
但我推荐异步任务
回答by Sohaib Aslam
Proper and short solution
正确和简短的解决方案
Make a handler and give them a delay to call back itself:
制作一个处理程序并给他们延迟回调自己:
final Handler h = new Handler();
h.postDelayed(new Runnable() {
@Override
public void run() {
//Do something after 1s
}
}, 1000);
Remember that 1 sec = 1000 milliseconds
请记住 1 秒 = 1000 毫秒
Adjust time with that formula.
用那个公式调整时间。
Happy Coding.
快乐编码。
回答by Cruceo
The problem is you're only sleeping for 500 milliseconds (half of one second), so it makes sense that it happens seemingly-immediately. You're also going to want to remove the OnGlobalLayoutListener after it's called. Here's an example of an approach that should work for you:
问题是你只睡了 500 毫秒(一秒的一半),所以它似乎是立即发生的。您还需要在调用 OnGlobalLayoutListener 后将其删除。这是一个应该适合您的方法示例:
final Handler handler = new Handler(); // Create a Handler on the main Thread
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
@Override
public void onGlobalLayout(){
removeOnGlobalLayoutListener(vto, this);
handler.postDelayed(new Runnable(){
public void run(){
Intent intent = new Intent(MainActivity.this, select_activity_class.class);
startActivity(intent);
}
}, 4000); //Post back to the main Thread after 4000 mils (4 seconds)
}
});
@SuppressLint("NewApi")
public static void removeOnGlobalLayoutListener(View v, ViewTreeObserver.OnGlobalLayoutListener listener){
if (Build.VERSION.SDK_INT < 16) v.getViewTreeObserver().removeGlobalOnLayoutListener(listener);
else v.getViewTreeObserver().removeOnGlobalLayoutListener(listener);
}
回答by Gabe Sechan
1)Sleeping 500 only sleeps for .5 seconds. So it would blink quickly anyway
1)睡眠 500 只睡眠 0.5 秒。所以无论如何它都会快速闪烁
2)Sleeping doesn't allow the thread to get back to the looper, so it freezes your UI. This means it won't update and draw anyway. Use a timer instead. Or posting a message to a handler would be acceptable here.
2)休眠不允许线程返回到looper,因此它会冻结您的UI。这意味着它无论如何都不会更新和绘制。改用计时器。或者在这里向处理程序发布消息是可以接受的。