質問

In my app i set the splash screen timer to 5 sec and later on think that 5 sec is too long so i change it back to 1 sec and my splash screen doesn't seen on the screen and keep me waiting for more than 5 sec i couldn't find what is wrong so here is my Splashscreen code

    public class Splash extends Activity
{
    private Timer_Countdown timer_Countdown = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.splash_screen);
        timer_Countdown = new Timer_Countdown(5000, 1000);
        timer_Countdown.start();
    }

    class Timer_Countdown extends CountDownTimer
    {
        public Timer_Countdown(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            timer_Countdown.cancel();
            Intent startIntent;
            startIntent = new Intent("android.intent.action.MAINMENU");
            startActivity(startIntent);
        }

        @Override
        public void onTick(long millisUntilFinished) {

        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        finish();
    }
    }

And one last thing if I change it back to 5 sec it shows up on the screen again.

役に立ちましたか?

解決

Why you are using this much of code just to use splash screen. Make it simple, you can use below code.

public class Splash extends Activity {

    Timer timer = new Timer();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        timer.schedule(new TimerTask() {
               public void run() {
                   Intent intent = new Intent(Splash.this, NewActivity.class);
                   startActivity(intent);
                               finish();
               }
            }, 2000);
    }
}

他のヒント

You can use Handler also

    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub

            startActivity(new Intent(SplashActivity.this, YourNewActivity.class));
            finish();

        }
    }, 3000);

or Using Timer with Timer Schedule

public class Splash extends Activity {

 Timer t= new Timer();

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    t.schedule(new TimerTask() {
           public void run() {
               Intent n= new Intent(Splash.this, YourNewActivity.class);
               startActivity(n);
           }
        }, 3000);
 }
 }

Use this instead of Timer

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

            @Override
            public void run() {
                //code for starting new activity

            }
        }, 5000);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top