Android工作室启动画面循环我的活动 - 线程

时间:2016-02-08 12:41:19

标签: android multithreading

首先我是Android新手。我找到了一段代码来启动app启动。当我与我的项目集成时,我在登录/第二次活动中获得了不定式循环。我猜我在线程上做错了。

import android.app.Activity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;



public class WelcomeSplashLogo extends Activity{

public void onAttachedToWindow() {
    super.onAttachedToWindow();
    Window window = getWindow();
    window.setFormat(PixelFormat.RGBA_8888);
}

Thread splashTread;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.welcome_splash_logo);
    StartAnimations();


}

private void StartAnimations() {
    Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
    anim.reset();
    LinearLayout l=(LinearLayout) findViewById(R.id.lin_lay);
    l.clearAnimation();
    l.startAnimation(anim);

    anim = AnimationUtils.loadAnimation(this, R.anim.translate);
    anim.reset();
    ImageView iv = (ImageView) findViewById(R.id.splash);
    iv.clearAnimation();
    iv.startAnimation(anim);

    splashTread = new Thread() {
        @Override
        public void run() {
            try {
                int waited = 0;
                // Splash screen pause time
                while (waited < 3500) {
                    sleep(100);
                    waited += 100;
                }
                Intent intent = new Intent(WelcomeSplashLogo.this, WelcomeSplashLogo.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                startActivity(intent);
                WelcomeSplashLogo.this.finish();
            } catch (InterruptedException e) {
                // do nothing
            } finally {
                WelcomeSplashLogo.this.finish();

                Intent intentGoToHomeActivity = new Intent(WelcomeSplashLogo.this, LoginActivity.class);
                WelcomeSplashLogo.this.startActivity(intentGoToHomeActivity);
            }

        }
    };
    splashTread.start();

}

}

4 个答案:

答案 0 :(得分:1)

这只是因为以下几行:

Intent intent = new Intent(WelcomeSplashLogo.this, WelcomeSplashLogo.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            startActivity(intent);
            WelcomeSplashLogo.this.finish();

您正在调用Same Activity,即WelcomeSplashLogo.class。

答案 1 :(得分:1)

在指定的时间过后,使用Handler排队并运行您的任务,然后转到下一个屏幕......

Handler handler;
Runnable runnable;    
runnable = new Runnable() {
        @Override
        public void run() {
            Intent intent = new Intent(WelcomeSplashLogo.this, LoginActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            startActivity(intent);
            finish();
        }
    };
handler = new Handler();
handler.postDelayed(runnable, 3000);  // delay time...

答案 2 :(得分:0)

就个人而言,如果您能早点显示主应用程序,我认为让用户等待3.5秒并不是一个好主意。

我对你的代码发表了一些评论:

splashTread = new Thread() {
    @Override
    public void run() {
        try {
            int waited = 0;
            // Splash screen pause time
            while (waited < 3500) {    // why loop? sleep(3500) would do it, too.
                sleep(100);
                waited += 100;
            }
            /*
            Intent intent = new Intent(WelcomeSplashLogo.this, WelcomeSplashLogo.class);  // You call your SplashLogo Activity again. 
            intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            startActivity(intent);
            WelcomeSplashLogo.this.finish();   // this is called in finally block already, therefore it is not needed here
            */
        } catch (InterruptedException e) {
            // do nothing
        } finally {
            WelcomeSplashLogo.this.finish();

            Intent intentGoToHomeActivity = new Intent(WelcomeSplashLogo.this, LoginActivity.class);
            WelcomeSplashLogo.this.startActivity(intentGoToHomeActivity);
        }

    }
};
splashTread.start();

答案 3 :(得分:-1)

使用处理程序

 new Handler().postDelayed(new Runnable() {

                /*
                 * Showing splash screen with a timer. This will be useful when you
                 * want to show case your app logo / company
                 */

                @Override
                public void run() {
                    // This method will be executed once the timer is over
                    // Start your app main activity
                    Intent i = new Intent(WelcomeSplashLogo.this, WelcomeSplashLogo.class);
                    startActivity(i);

                    // close this activity
                    finish();
                }
            }, 3000);