Android关于网络连通状况的系统广播

连通状态改变(包括wifi,3G等移动网络):

android.net.conn.CONNECTIVITY_CHANGE

ConnectivityManager.CONNECTIVITY_ACTION

wifi连接改变:

android.net.wifi.RSSI_CHANGED

 

判断网络连通状态(包括wifi,3G等):

public boolean isNetworkConnected(Context context) { 
    if (context != null) { 
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context 
                .getSystemService(Context.CONNECTIVITY_SERVICE); 
        NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); 
        if (mNetworkInfo != null) { 
            return mNetworkInfo.isAvailable(); 
        } 
    } 
    return false; 
}

wifi连通状态:

public boolean isWifiConnected(Context context) { 
    if (context != null) { 
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context 
                .getSystemService(Context.CONNECTIVITY_SERVICE); 
        NetworkInfo mWiFiNetworkInfo = mConnectivityManager 
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
        if (mWiFiNetworkInfo != null) { 
            return mWiFiNetworkInfo.isAvailable(); 
        } 
    } 
    return false; 
}

3G等移动的连通状态:

public boolean isMobileConnected(Context context) { 
    if (context != null) { 
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context 
                .getSystemService(Context.CONNECTIVITY_SERVICE); 
        NetworkInfo mMobileNetworkInfo = mConnectivityManager 
                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 
        if (mMobileNetworkInfo != null) { 
            return mMobileNetworkInfo.isAvailable(); 
        } 
    } 
    return false; 
}
     

获取当前网络连接类型:

public static int getConnectedType(Context context) { 
    if (context != null) { 
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context 
                .getSystemService(Context.CONNECTIVITY_SERVICE); 
        NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); 
        if (mNetworkInfo != null && mNetworkInfo.isAvailable()) { 
            return mNetworkInfo.getType(); 
        } 
    } 
    return -1; 
}

 

你可能感兴趣的:(android)