sofs1
sofs1

Reputation: 4176

Notify user that the user is not connected to internet at any screen?

I am developing an app, where at any point in the app (in any fragment - correct me if I am wrong), if the user is not connected to internet, a pop-up (I guess I have to use toast) asking user to either

  1. How to show the pop up at any point in the app?
  2. Should I make the user to go to settings and turn on wifi or take corresponding action or should I stop with showing the message?

This app needs internet connection for sure. Thanks.

Upvotes: 1

Views: 1196

Answers (1)

San
San

Reputation: 2088

First and foremost, you have to check if your device is connected to the internet. The following code will help you to check that.

Method to check internet availablity:

 public boolean isOnline() {
        ConnectivityManager cm =
            (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        return netInfo != null && netInfo.isConnectedOrConnecting();
    }

If the above boolean is true then you can proceed with your operations or you would have to instruct the user to go to Settings and switch on either the mobile data or Wi-Fi connection. Please refer the following code to instruct the user. Call the following method if the above boolean returns false.

displayMobileDataSettingsDialog(your current activity,context);

Method to show alert:

public static AlertDialog displayMobileDataSettingsDialog(final Activity activity, final Context context){
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("No Internet");
        builder.setMessage("Please connect to your internet");
        builder.setPositiveButton("Wifi", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                context.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
                dialog.cancel();
            }
        });
        builder.setNegativeButton("Mobile Data", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                 Intent intent = new Intent();
                 intent.setComponent(new ComponentName("com.android.settings","com.android.settings.Settings$DataUsageSummaryActivity"));
                 dialog.cancel();
                 startActivity(intent);
                 activity.finish();

            }
        });
        builder.show();

        return builder.create();
    }

Optional: Sometimes your device might be connected to internet but there will be no data received, in those case you can make use of the following method to check if there is actually data being received to your device.

public boolean isInternetAvailable() {
        try {
            InetAddress ipAddr = InetAddress.getByName("google.com"); //You can replace it with your name
            return !ipAddr.equals("");

        } catch (Exception e) {
            return false;
        }

    }

Hope it helps you.

Upvotes: 1

Related Questions