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));
}
}
Hope this helps:
public static boolean isConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netinfo = cm != null ? cm.getActiveNetworkInfo() : null;
if (netinfo != null && netinfo.isConnectedOrConnecting()) {
android.net.NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
return (mobile != null && mobile.isConnectedOrConnecting()) || (wifi != null && wifi.isConnectedOrConnecting());
} else
return false;
}
public static boolean isOnline(Context context) {
Runtime runtime = Runtime.getRuntime();
try {
Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
int exitValue = ipProcess.waitFor();
return (exitValue == 0);
}
catch (IOException e) { e.printStackTrace(); }
catch (InterruptedException e) { e.printStackTrace(); }
return false;
}
public AlertDialog.Builder buildDialog(Context c) {
AlertDialog.Builder builder = new AlertDialog.Builder(c);
builder.setTitle("No Internet Connection");
builder.setMessage("Seems like your Internet connection is down. Please Check");
builder.setPositiveButton("OK", (dialog, which) -> {
dialog.dismiss();
finish();
});
return builder;
}