ConnectivityManager.CONNECTIVITY_ACTION在API> = 14中断开网络连接?

时间:2012-11-08 16:26:02

标签: android networking connectivity

我需要从哪个设备断开网络。

现在我用:

NetworkInfo ni =intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);

并检查:

ni.isConnected()

如果返回false ni - 是设备断开连接的网络。

但是在API 14中,ConnectivityManager.EXTRA_NETWORK_INFO 已弃用。谷歌表示使用getActiveNetworkInfo()来获取网络信息。但getActiveNetworkInfo()始终返回设备现在连接的网络(isConnected()必须返回true)!

如何在不使用ConnectivityManager.EXTRA_NETWORK_INFO的情况下获取设备断开连接的网络的网络信息?

Sertorio Noronha,当我使用getActiveNetworkInfo()时,我只获得了我现在连接的网络!但我需要获得断开连接的网络。

ConnectivityManager cm = (ConnectivityManager)
    context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo n1 = cm.getActiveNetworkInfo();
Log.d("tets", String.format("%s: %s", n1.getTypeName(), n1.isConnected()));

当我从WI-FI断开连接并在日志中连接到3G时

mobile: true
mobile: true

当我从3G断开并连接到日志中的WI-FI时:

WIFI: true
WIFI: true
WIFI: true

getActiveNetworkInfo仅返回连接到现在的网络,但不返回断开连接的网络。

如果我在日志中使用弃用的intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO),请参阅:

当我断开WI-FI并连接到3G时:

WIFI: false
mobile: true

当我断开与3G的连接并连接到WI-FI时:

mobile: false
WIFI: true

但我不想使用弃用的api。如何使用现代api获取我断开连接的网络?

3 个答案:

答案 0 :(得分:11)

您可以使用以下

ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
int networkType = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_TYPE);
boolean isWiFi = networkType == ConnectivityManager.TYPE_WIFI;
boolean isMobile = networkType == ConnectivityManager.TYPE_MOBILE;
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(networkType);
boolean isConnected = networkInfo.isConnected();

if (isWiFi) {
    if (isConnected) {
        Log.i("APP_TAG", "Wi-Fi - CONNECTED");
    } else {
        Log.i("APP_TAG", "Wi-Fi - DISCONNECTED");
    }
} else if (isMobile) {
    if (isConnected) {
        Log.i("APP_TAG", "Mobile - CONNECTED");
    } else {
        Log.i("APP_TAG", "Mobile - DISCONNECTED");
    }
} else {
    if (isConnected) {
        Log.i("APP_TAG", networkInfo.getTypeName() + " - CONNECTED");
    } else {
        Log.i("APP_TAG", networkInfo.getTypeName() + " - DISCONNECTED");
    }
}

答案 1 :(得分:6)

您可以通过Context获取NetworkInfo的实例。

ConnectivityManager connectivityManager = ((ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE));
NetworkInfo currentNetworkInfo = connectivityManager.getActiveNetworkInfo();

if(currentNetworkInfo != null && currentNetworkInfo.isConnected()){
   // Your logic goes in here
}

答案 2 :(得分:6)

使用非弃用代码

/**Receiver*/   

 public class NetworkStateReceiver extends BroadcastReceiver {

        /*
         * @see android.content.BroadcastReceiver#onReceive(android.content.Context,
         * android.content.Intent)
         */
        @Override
        public void onReceive(Context context, Intent intent) {
            ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
            int networkType = intent.getExtras().getInt(ConnectivityManager.EXTRA_NETWORK_TYPE);
            boolean isWiFi = networkType == ConnectivityManager.TYPE_WIFI;
            boolean isMobile = networkType == ConnectivityManager.TYPE_MOBILE;
            NetworkInfo networkInfo = connectivityManager.getNetworkInfo(networkType);
            boolean isConnected = networkInfo.isConnected();

            if (isWiFi) {
                if (isConnected) {
                    Log.i("APP_TAG", "Wi-Fi - CONNECTED");
                } else {
                    Log.i("APP_TAG", "Wi-Fi - DISCONNECTED");
                }
            } else if (isMobile) {
                if (isConnected) {
                    Log.i("APP_TAG", "Mobile - CONNECTED");
                } else {
                    Log.i("APP_TAG", "Mobile - DISCONNECTED");
                }
            } else {
                if (isConnected) {
                    Log.i("APP_TAG", networkInfo.getTypeName() + " - CONNECTED");
                } else {
                    Log.i("APP_TAG", networkInfo.getTypeName() + " - DISCONNECTED");
                }
            }
        }

    }

并在清单中

<!-- Receiver registration in manifest -->
     <receiver android:name="com.xxx.yyy.NetworkStateReceiver" >
                <intent-filter>
                    <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                </intent-filter>
            </receiver>

<!-- Internet permission for network comunication -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />