ConnectivityManager.CONNECTIVITY_ACTION从API> = 14中取消网络连接?

Nik*_*Nik 8 networking android connectivity

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

现在我使用:

NetworkInfo ni =intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
Run Code Online (Sandbox Code Playgroud)

并检查:

ni.isConnected()
Run Code Online (Sandbox Code Playgroud)

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

ConnectivityManager.EXTRA_NETWORK_INFO弃用的API 14.谷歌表示,使用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()));
Run Code Online (Sandbox Code Playgroud)

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

mobile: true
mobile: true
Run Code Online (Sandbox Code Playgroud)

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

WIFI: true
WIFI: true
WIFI: true
Run Code Online (Sandbox Code Playgroud)

getActiveNetworkInfo仅返回连接到now的网络,但不返回断开连接的网络.

如果我在日志中使用弃用的intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO),我会看到:

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

WIFI: false
mobile: true
Run Code Online (Sandbox Code Playgroud)

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

mobile: false
WIFI: true
Run Code Online (Sandbox Code Playgroud)

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

小智 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");
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 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
}
Run Code Online (Sandbox Code Playgroud)


Vin*_*yak 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");
                }
            }
        }

    }
Run Code Online (Sandbox Code Playgroud)

在清单中

<!-- Receiver registration in manifest -->
     <receiver android:name="com.xxx.yyy.NetworkStateReceiver" >
                <intent-filter>
                    <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                </intent-filter>
            </receiver>
Run Code Online (Sandbox Code Playgroud)

<!-- Internet permission for network comunication -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Run Code Online (Sandbox Code Playgroud)

  • 在Android N - FYI中不推荐使用`android.net.conn.CONNECTIVITY_CHANGE` (2认同)