Android 欢迎界面的三种实现方式

1. 使用Handler 对象

在Welcome Activity 的onCreate的方法中插入如下代码

new Handler().postDelayed(new Runnable(){
		  public void run(){
			if(isLogined()){
				//jump to main activity
				Intent intent = new Intent(WelcomeActivity.this,UserActivity.class);
				intent.putExtra(UserConfig.KEY_TOKEN, UserConfig.getCachedToken(WelcomeActivity.this));
				startActivity(intent);
			}else{
				//jump to login activity
				startActivity(new Intent(WelcomeActivity.this,LoginActivity.class));
			}
			finish();
		  }
	  }, 2900);//3 seconds later jump to main activity

2.使用TimerTask 对象

   与第一种的机制几乎一样

 

new Timer().schedule(new TimerTask(){

		@Override
		public void run() {
			// TODO Auto-generated method stub
			if(isLogined()){
				//jump to main activity
				Intent intent = new Intent(WelcomeActivity.this,UserActivity.class);
				intent.putExtra(UserConfig.KEY_TOKEN, UserConfig.getCachedToken(WelcomeActivity.this));
				startActivity(intent);
			}else{
				//jump to login activity
				startActivity(new Intent(WelcomeActivity.this,LoginActivity.class));
			}
			finish();
		  }
		  
	  }, 2900);

3. 使用 动画实现

    这种方式是在动画执行完后触发onAnimationEnd()方法,在onAnimatinoEnd()方法中插入Activity 跳转逻辑

Animation anim = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
	  anim.setDuration(3000);//last 3 seconds
	  anim.setFillAfter(true);//hold the finished state
	  anim.setAnimationListener(new AnimationListener(){

		@Override
		public void onAnimationStart(Animation animation) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onAnimationEnd(Animation animation) {
			// TODO Auto-generated method stub
			if(isLogined()){
				//jump to main activity
				Intent intent = new Intent(WelcomeActivity.this,UserActivity.class);
				intent.putExtra(UserConfig.KEY_TOKEN, UserConfig.getCachedToken(WelcomeActivity.this));
				startActivity(intent);
			}else{
				//jump to login activity
				startActivity(new Intent(WelcomeActivity.this,LoginActivity.class));
			}
			finish();
		}

		@Override
		public void onAnimationRepeat(Animation animation) {
			// TODO Auto-generated method stub
			
		}
		  
	  });
	  findViewById(R.id.logo).setAnimation(anim);



你可能感兴趣的:(Android 欢迎界面的三种实现方式)