如何在Android中区分互联网是wifi还是移动数据?

时间:2014-03-14 05:25:40

标签: android

实际上我需要在我的Android手机中实现无线互联网是否可用的代码。我有一个代码,这是可用的互联网连接。请帮助我。感谢

4 个答案:

答案 0 :(得分:1)

试试这个 -

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.isAvailable() && wifi.getDetailedState() == DetailedState.CONNECTED){
        Toast.makeText(this, "Wifi" , Toast.LENGTH_LONG).show();
    }
    else if( mobile.isAvailable() && mobile.getDetailedState() == DetailedState.CONNECTED ){
        Toast.makeText(this, "Mobile 3G " , Toast.LENGTH_LONG).show();
    }
    else
    {   
        Toast.makeText(this, "No Network " , Toast.LENGTH_LONG).show();
    }

使用这些权限 -

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

ConnectivityManager回答有关网络连接状态的查询的类。

答案 1 :(得分:0)

试试这个

public static boolean haveNetworkConnection(Context context) {
    boolean haveConnectedWifi = false;
    boolean haveConnectedMobile = false;

    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    for (NetworkInfo ni : netInfo) {
        if (ni.getTypeName().equalsIgnoreCase("WIFI"))
            if (ni.isConnected())
                haveConnectedWifi = true;
        if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
            if (ni.isConnected())
                haveConnectedMobile = true;
    }
    return haveConnectedWifi || haveConnectedMobile;
}

答案 2 :(得分:0)

这是我在进行一些研究后得到的东西。

这是为了获得Wifi状态:

public static boolean getWifiState(Context context) {
        ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        return mWifi.isConnected();

    }

这是为了获取gprs:

public static boolean getNetworkType(Context context) {
            TelephonyManager tm = (TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);
            int type = tm.getNetworkType();
            //Here you can actually get some info on the network type. Try it out. 
            if (type >= TelephonyManager.NETWORK_TYPE_GPRS) {
                return true;
            }
            return false;
        }

最后一件事是添加权限:

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

希望这会有所帮助.. :)

答案 3 :(得分:0)

您可以使用以下代码:

public static boolean isDataEnabled(Context context)
{
    return (isMobileDataEnabled(context) || isWifiEnabled(context));
}


public static boolean isMobileDataEnabled(Context context)
{
    if(context == null)
        return false;
    ConnectivityManager connectivityManager = (ConnectivityManager)
        context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = null;
    if (connectivityManager != null) {
        networkInfo =
            connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    }
    return networkInfo == null ? false : networkInfo.isConnected();
}

public static boolean isWifiEnabled(Context context)
{
    if(context == null)
        return false;
    ConnectivityManager connectivityManager = (ConnectivityManager)
        context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = null;
    if (connectivityManager != null) {
        networkInfo =
            connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    }
    return networkInfo == null ? false : networkInfo.isConnected();
}