2

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));
}
}
Jatin Bhatia
  • 248
  • 4
  • 15
  • Can you define limited connectivity? Do you mean the device is not connected to any Cellular/Wifi network or is connected to them but no data is received? or both – Ashish Krishnan Jun 13 '18 at 17:03
  • The device is connected to Wifi/Mobile Data but is not able to receive data as the internet is down. @AshishKrishnan – Jatin Bhatia Jun 13 '18 at 17:46
  • @JatinBhatia you have to make a request, check if it receive a timeout response, and display that message before re-trying the request :) You can request whichever resource you want :) – Luca Nicoletti Jun 14 '18 at 05:59
  • @jatinBhatia: If that's the case, you have to catch the IOException and then show a dialog. IOException is the super class for all the network errors which might generate at the network layer. For limited connectivity, you might get a SocketTimeoutExecption but is wise to handle IOException. You can read more here. https://developer.android.com/reference/java/io/IOException. Now coming back to your question, do you have a network layer something like OkHttp stack with Retrofit? It would have been really easy to model errors. – Ashish Krishnan Jun 14 '18 at 19:29
  • Although that's fine. All you should do is, hit a URL and then parse the response. Try to get an throwable to check if it's an `instanceOf` IOException. Handle this case to show an dialog/error. Let me know if you need the code. Will put it as an answer. – Ashish Krishnan Jun 14 '18 at 19:32

2 Answers2

2

Launch an emulator and then open settings, go to Cellular settings and change Signal strength to "Poor"

Here: enter image description here

Luca Nicoletti
  • 2,265
  • 2
  • 18
  • 32
0

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;
}