How to show splash image while loading activity

折月煮酒 提交于 2019-11-30 20:33:33
Dr.jacky

use AsyncTask

put splash in onPreExecute()

and do your work in doInBackground()

and close splash in onPostExecute()

Below is the simple code for creating splash screen using CountDownTimer class

public class SplashDialogActivity extends Activity {
@Override
    protected void onCreate(Bundle savedInstanceState) {
        // Be sure to call the super class.
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.layout);
      counter.start();
    }
    MyCount counter = new MyCount(5000, 1000);
 public class MyCount extends CountDownTimer{
            public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
            }

            @Override
            public void onFinish() {
                go_back();
            }

            @Override
            public void onTick(long millisUntilFinished) {



              }
     }

 public void go_back()
        {
          counter.cancel();

                    Intent i=new Intent(this,account.class);
                    i.putExtra("first_time", true);
                    startActivity(i);

            this.finish();
        }
}
K.Muthu

try this code for splash page

private Thread mSplashThread;    

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.splesh);

    final Splash sPlashScreen = this;   

     mSplashThread =  new Thread(){
        @Override
        public void run(){
            try {
                synchronized(this){
                    wait(5000);
                }
            }
            catch(InterruptedException ex){                    
            }

            finish();

            Intent intent = new Intent();
            intent.setClass(sPlashScreen,Login.class);
            startActivity(intent);
            stop();                    
        }
    };

    mSplashThread.start();        
 }

// Processes splash screen touch events
@Override
public boolean onTouchEvent(MotionEvent evt) {

     if(evt.getAction() == MotionEvent.ACTION_DOWN)
     {
         synchronized(mSplashThread){
             mSplashThread.notifyAll();
         }
     }
     return true;
}    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!