How to test Limited Internet Access in Android Studio


Ask A Question





Login To post a question

How do I show, limited access to internet on the Splash Screen? My code currently works when the mobile data or Wifi is turned off, but i want to show an alert box if there is limited access of internet. Please someone out there help me.

Code for Splash Screen:

public class SplashScreen extends AppCompatActivity {
TextView versionName;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Fabric.with(this, new Crashlytics());

    if (!NetworkUtil.isConnected(SplashScreen.this))
        buildDialog(SplashScreen.this).show();
    else {
        setContentView(R.layout.activity_splash_screen);
        animation();
        getVersionInfo();
    }
}
private void animation() {
    final ImageView imageView = findViewById(R.id.spin_loader);
    final Animation animation = AnimationUtils.loadAnimation(getBaseContext(), R.anim.rotate);

    imageView.startAnimation(animation);
    animation.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }
        @Override
        public void onAnimationEnd(Animation animation) {
            finish();
            Intent intent = new Intent(SplashScreen.this, HomeScreenActivity.class);
            startActivity(intent);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });
}
public AlertDialog.Builder buildDialog(Context c) {
    AlertDialog.Builder builder = new AlertDialog.Builder(c);
    builder.setTitle("No Internet Connection");
    builder.setMessage("You need to have Mobile Data or WIFI to access the App. Press OK to Exit");
    builder.setPositiveButton("OK", (dialog, which) -> {
        dialog.dismiss();
        finish();
    });
    return builder;
}
private void getVersionInfo() {
    TextView textViewVersionInfo = findViewById(R.id.versionName);
    textViewVersionInfo.setText(String.format("Version %s", BuildConfig.VERSION_NAME));
}
}

For more info please click herehttps://stackoverflow.com/q/50842329



Have a question or need a custom quote?
info@smartinfocare.com