Detect if connection is wifi, 3G or EDGE in android?

前端 未结 3 457
面向向阳花
面向向阳花 2020-12-23 22:22

I need to detect in android if there is any connection Wifi or 3G ( or 3G+) or EDGE in android. When there is an connecti

3条回答
  •  攒了一身酷
    2020-12-23 22:39

    First get a reference to the ConnectivityManager and then check the Wifi and 3G status of the device. You'll need the ACCESS_NETWORK_STATE permission to use this service.

        ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
        NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mMobile = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    
        if (mWifi.isAvailable() == true) {
            return "Connected to WiFi";
        } else if (mMobile.isAvailable() == true) {
            return "Connected to Mobile Network";
        } else return "No internet Connection"
    

提交回复
热议问题