8

我需要获取断开设备连接的网络。

现在我使用:

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

并检查:

ni.isConnected()

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

但在 API 14ConnectivityManager.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 获取我断开连接的网络?

4

3 回答 3

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");
    }
}
于 2013-12-15T01:07:12.437 回答
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" />
于 2014-08-08T13:00:04.147 回答
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
}
于 2013-02-22T11:55:31.190 回答