1

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

  • turn on wi-fi (if wi-fi is off and no data connection as well) or
  • connect to wi-fi (if wi-fi is on, but not connected) or
  • turn on data in phone.
  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.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
sofs1
  • 3,834
  • 11
  • 51
  • 89
  • You should use an AlertDialog. That has buttons. A Toast does not. See here. http://stackoverflow.com/questions/25685755/ask-user-to-connect-to-internet-or-quit-app-android?rq=1 – OneCricketeer Oct 05 '16 at 22:31
  • When you try to access server, i.e., in your code at the point where you send/receive a data from server then you have to check for internet availability and show your alert for the user.. – San Oct 05 '16 at 22:31

1 Answers1

1

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.

San
  • 2,078
  • 1
  • 24
  • 42
  • Thank you very much for detailed explanation and code. May I know where should this code be placed. If I am putting this within a new class, where should that class be under a package? – sofs1 Oct 07 '16 at 07:29
  • 2) What if the text for AlertDialog should be in another language? Can I use strings in strings.xml and use them here? – sofs1 Oct 07 '16 at 07:39
  • "2) What if the text for AlertDialog should be in another language? Can I use strings in strings.xml and use them here?" - Yes you can – San Oct 07 '16 at 07:40
  • This code has to be placed under your class where you update your data to server... Better post your code where you THINK that you should place this code and I will let you know.. – San Oct 07 '16 at 07:43
  • Thanks for replying. Ok, In other words, in my app, lets say there are 10 screens and in each screen there is a transaction with remote server or web services. So I need to check for internet connectivity in each screen and if not I should let user know. Rather than placing the redundant code in 10 different places, I am trying to put this in one class and call these methods in 10 different places. Could you direct me how to do it? As it has getSystemService(Context.CONNECTIVITY_SERVICE); and other activity or context related members, I am struck. Please help. – sofs1 Oct 07 '16 at 07:53
  • Define a new class, say "InternetCheck" and place all the above methods in that class. In your 10 different classes where you access the server, use your InternetCheck class to access all these methods. In all your 10 classes, you can first check if you have internet or not by using 'InternetCheck.isOnline' and if it returns true then call your server and if it returns false then call 'InternetCheck.displayMobileDataSettingsDialog(your current activity,context);' . – San Oct 07 '16 at 08:18
  • a) InternetCheck.displayMobileDataSettingsDialog(your current activity,context) should the context be ApplicationContext or BaseContext? b) What permissions should be added for this code in AndroidManifest.xml? I got an error and added android.permission.ACCESS_NETWORK_STATE – sofs1 Oct 07 '16 at 09:09
  • Please add base context and also add permission of "android.permission.ACCESS_NETWORK_STATE" in your manifest.. That's used to access the network from your app, I wonder how you didn't add it so far cause this error would've been thrown the very first time you try to access the network from your app.. – San Oct 07 '16 at 18:33