简体   繁体   English

Android几秒后更改活动

[英]Android change Activity after few seconds

I need some help with my first Android project. 我的第一个Android项目需要一些帮助。 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). 我想写一个应用程序,它会向你展示带有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: 这是我的主要活动代码:

     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);


    }
}); 


}}

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);
    }
}); 

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"

1)Sleeping 500 only sleeps for .5 seconds. 1)睡觉500只能睡眠0.5秒。 So it would blink quickly anyway 所以无论如何它会快速闪烁

2)Sleeping doesn't allow the thread to get back to the looper, so it freezes your UI. 2)睡眠不允许线程回到弯针,因此它会冻结你的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. 或者在这里发布消息给处理程序是可以接受的。

The problem is you're only sleeping for 500 milliseconds (half of one second), so it makes sense that it happens seemingly-immediately. 问题是你只睡了500毫秒(一秒钟的一半),所以看起来很快就会发生这种情况。 You're also going to want to remove the OnGlobalLayoutListener after it's called. 您还希望在调用之后删除OnGlobalLayoutListener。 Here's an example of an approach that should work for you: 以下是适合您的方法示例:

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);
}

never stall the UI thread. 从不停止UI线程。 The UI thread is responsible for keeping your app feeling responsive. 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 但我建议异步任务

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. 快乐的编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM