Search code examples
androidsplash-screenbandwidth

How to get Bandwidth


I want to get the value of the bandwidth in my application.

At the launching of the app, I display a splash screen and data are loaded in AsyncTask. I use a Handler like this to display a progressBar :

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

            @Override
            public void run() {
                Intent i = new Intent(Activity_Splash_Screen.this, Activity_Main.class);
                i.putParcelableArrayListExtra("category", (ArrayList<? extends Parcelable>) ListOfCategory);
                startActivity(i);
                finish();
            }
        }, SPLASH_TIME_OUT);

But, if the bandwidth is not optimal, the time to load data is more than the time of the variable SPLASH_TIME_OUT.

Therefore, i want to get the value of the bandwidth to adapt the value of my variable SPLASH_TIME_OUT.

Thanks a lot for your help!


Solution

  • I don't think this is the best way to show a splash screen. The splash screen must be shown until all mandatory data needed for your app is loaded. The approach with a static timeout doesn't work properly (or doesn't work at all on some devices). Why don't you try something like this?

    public class SplashActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_splash);
    
            init();
        }
    
        private void init() {
            final AsyncTask<Void, Void, Void> asyncTask = new AsyncTask<Void, Void, Void>() {
    
                @Override
                protected Void doInBackground(Void... params) {
                    //Do your initial data loading here...
    
                    return null;
                }
    
                @Override
                protected void onPostExecute(Void result) {
                    startActivity(new Intent(SplashActivity.this, MainActivity.class));
                    finish();
                }
    
            };
            asyncTask.execute(null, null);
        }
    }