简体   繁体   中英

How to check My test android device is connected to internet or not?

protected boolean isOnline()            
{
      ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo info = cm.getActiveNetworkInfo();
      if (info != null && info.isConnectedOrConnecting()) {
        return true;
      }else
      {
        return  false;
      }
}
public void requestData()
{
    if (isOnline())
    {
        Toast.makeText(getApplicationContext(), "connected", Toast.LENGTH_LONG).show();
    }
    else
    {
        Toast.makeText(getApplicationContext(), "Niot Connected", Toast.LENGTH_LONG).show();
    }
}

I have written this code but I am getting An error something like "Unfortunately your program has stopped".. Please help me.. Thanks In Advance

您需要将此添加到您的androidmanifest.xml

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

Please Check This Link http://www.androidhive.info/2012/07/android-detect-internet-connection-status/

its the Demo of Android Detect Internet Connection Status

To access internet we need INTERNET Permission

To detect network status we need ACCESS_NETWORK_STATE Permission

Call this method

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager 
          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

and declare permission

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

Here's the code that works fine for me. Define this class in your utils.

    public class InternetConnection {

    public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager connectivityManager
                = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null;
    }
}

Then, inside the activity where you want to check the connection status, use,

if(!(InternetConnection.isNetworkAvailable(this))){
            internetActive = false;
            Toast.makeText(this, "Please try again with active Internet connection", Toast.LENGTH_LONG).show();
        }
        else {
// your code if device is connected to internet
}

Make sure you have

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

permission in your manifest.

you will check both WIFI and Mobile data connected or not by using the following two methods:

public boolean checkWIFIInternetConn() {
        //Create object for ConnectivityManager class which returns network related info
        ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
        //If connectivity object is not null
        if (connectivity != null) {
            //Get network info - WIFI internet access
            NetworkInfo info = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI);


            if (info != null) {
                //Look for whether device is currently connected to WIFI network
                if (info.isConnected()) {
                    return true;
                }
            }
        }
        return false;
    }

     public boolean checkMobileInternetConn() {
            //Create object for ConnectivityManager class which returns network related info
            ConnectivityManager connectivity = (ConnectivityManager) _context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            //If connectivity object is not null
            if (connectivity != null) {
                //Get network info - Mobile internet access
                NetworkInfo info = connectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

                if (info != null) {
                    //Look for whether device is currently connected to Mobile internet
                    if (info.isConnected()) {
                        return true;
                    }
                }
            }
            return false;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM