简体   繁体   中英

Check whether mobile is using WIFI or Data/3G

Assuming that both WIFI and Data/3G are enabled on a device, how do I check if the user is currently using the internet over the wifi or the data plan assuming they are both enabled. So I don't need to check if they are enabled or disabled, I would like to detect which one is the user actually using.

I've been doing the following, is this the only solution?

WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
if (WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState()) == NetworkInfo.DetailedState.CONNECTED) {
    String ssid = wifiInfo.getSSID();
}
void chkStatus() {
    final ConnectivityManager connMgr = (ConnectivityManager)
    this.getSystemService(Context.CONNECTIVITY_SERVICE);
    final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (wifi.isConnectedOrConnecting ()) {
        Toast.makeText(this, "Wifi", Toast.LENGTH_LONG).show();
    } else if (mobile.isConnectedOrConnecting ()) {
        Toast.makeText(this, "Mobile 3G ", Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(this, "No Network ", Toast.LENGTH_LONG).show();
    }
}

You can use the following method to check network status every time:

public static String checkNetworkStatus(final Context context) {

        String networkStatus = "";

        // Get connect mangaer
        final ConnectivityManager connMgr = (ConnectivityManager)  
                context.getSystemService(Context.CONNECTIVITY_SERVICE);

        // check for wifi
        final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        // check for mobile data
        final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if( wifi.isAvailable() ) {
            networkStatus = "wifi";
        } else if( mobile.isAvailable() ) {
            networkStatus = "mobileData";
        } else {
            networkStatus = "noNetwork";
        }

        return networkStatus;

}  // end checkNetworkStatus 

//vKj

 public boolean CheckConnectivity(final Context c) {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (mConnectivityManager.getActiveNetworkInfo() != null
                && mConnectivityManager.getActiveNetworkInfo().isAvailable()
                && mConnectivityManager.getActiveNetworkInfo().isConnected()) {
            return true;
        } else {
            return false; // make it false
        }
    }

getNetworkInfo is a deprecated method. I suggest to use getActiveNetworkInfo

Example

private String checkNetworkStatus(Context context) {

    String networkStatus = "";
    final ConnectivityManager manager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

    if( manager.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
        networkStatus = "wifi";
    } else if(manager.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_MOBILE){
        networkStatus = "mobileData";
    } else{
        networkStatus="noNetwork";
    }
    return networkStatus;
}

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