How to check internet access is enabled in android?

余生长醉 提交于 2019-12-06 04:12:49

In an application of mine, I have a class defined like this:

    public class ConnectionDetector {

        private Context _context;

        public ConnectionDetector(Context context){
            this._context = context;
        }

        /**
         * Checking for all possible internet providers
         * **/
        public boolean isConnectingToInternet(){
            ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
              if (connectivity != null)
              {
                  NetworkInfo[] info = connectivity.getAllNetworkInfo();
                  if (info != null)
                      for (int i = 0; i < info.length; i++)
                          if (info[i].getState() == NetworkInfo.State.CONNECTED)
                          {
                              return true;
                          }

              }
              return false;
        }
    }

And in the Activity that I need to check the connectivity status, I use this:

    // DEFINE THIS AS A GLOBAL VARIABLE
    AlertDialogManager alert = new AlertDialogManager();

In the onCreate():

    ConnectionDetector cd = new ConnectionDetector(getApplicationContext());
    // Check if Internet present
    if (!cd.isConnectingToInternet()) {
        // Internet Connection is not present
        alert.showAlertDialog(MainActivity.this, "Internet Connection Error",
            "Please connect to working Internet connection", false);
        // stop executing code by return
        return;
    }

Oh. Almost forgot. If you also need to re-direct the user to the settings panel to activate Internet, you can use an Intent like this:

You could prompt the user in the AlertDialog and let them choose if they want to. If yes, run this piece of code.

Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
startActivity(intent);

EDIT:

I missed the obvious (Commonsware pointed that one out in his comment).

You will need to add the ACCESS_NETWORK_STATE permission to your Manifest file.

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

You can use this method:

public boolean isConn() {
        ConnectivityManager connectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity.getActiveNetworkInfo() != null) {
            if (connectivity.getActiveNetworkInfo().isConnected())
                return true;
        }
        return false;
    }

And add this permission to manifest file:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

This is the function I use to check internet connectivity

// Checks Internet Status
public boolean isOnline() {
    if(cm == null)
        return false;

    final NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (activeNetwork != null && activeNetwork.getState() == NetworkInfo.State.CONNECTED) 
        return true;

    return false;
}

Also add this permission to your manifest file

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

To send the user to WiFi settings page use

startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!