Best way to have a splash screen in an Android application?

后端 未结 6 1290
别跟我提以往
别跟我提以往 2020-12-17 00:40

I need a splash screen for my application. Tried creating an activity having the image for my splash screen; and tried using for loop and the Timer class for introducing a t

6条回答
  •  时光取名叫无心
    2020-12-17 01:14

    Try this,

    protected int _splashTime = 15000; 
    
    private Handler handler;
    private Runnable runnable; 
    
    private Context context;
    
    
    @Override
    public void onCreate(Bundle savedInstance)
    {
        super.onCreate(savedInstance);
        setContentView(R.layout.splash);   
    
        final SplashScreen sPlashScreen = this; 
    
        handler = new Handler();
    
         runnable = new Runnable() {
             @Override
                public void run() { 
                 try {
                    handler.removeCallbacks(runnable);
                    handler.postDelayed(runnable, _splashTime);
                  }  
                finally {
                    finish(); 
                    //start a new activity
    
                    //mtdCheckLicense();
                    Intent main = new Intent();
                    main.setClass(sPlashScreen, YourMainActivity.class);
                    startActivity(main); 
                    handler.removeCallbacks(runnable);
    
                }
            }
        }; 
        handler.postDelayed(runnable, 2000);
    } 
    

    It will splash for some time and launch the main activity. In this code the splash screen wait for 2Seconds and then launches the main activity.

提交回复
热议问题