ConnectivityManager.CONNECTIVITY_ACTION获得网络从API&GT disconected = 14?网络、CONNECTIVITY_ACTION、Connectiv

2023-09-05 05:01:41 作者:少女杀手

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

I need to get the network from which device was disconnected.

现在我用的:

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

和检查:

ni.isConnected()

如果这个返回false妮 - 是从该装置断开网络

if this returns false ni - is the network from which the device was disconnected.

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

But ConnectivityManager.EXTRA_NETWORK_INFO is deprecated in API 14. Google says use getActiveNetworkInfo() to get network information. But getActiveNetworkInfo() always returns network with which the device is connected now (isConnected() must return true)!

我如何为网络从设备断开连接,而无需使用ConnectivityManager.EXTRA_NETWORK_INFO网络信息?

How do I get the network info for the network the device disconnected from without using ConnectivityManager.EXTRA_NETWORK_INFO?

Sertorio迪诺罗尼亚,当我使用getActiveNetworkInfo()我只得到网络而我现在连!但是,我需要从我被断开网络。

Sertorio Noronha, when I use getActiveNetworkInfo() I only get the network to which I am connected now! But I need to get the network from which I was disconnected.

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日志:

When I disconnect from WI-FI and connect to 3G in log:

mobile: true
mobile: true

当我从3G断开和连接到Wi-Fi日志:

When I disconnect from 3G and connect to WI-FI in log:

WIFI: true
WIFI: true
WIFI: true

getActiveNetworkInfo仅返回连接到现在的网络,但不返回从中我被断开网络

getActiveNetworkInfo returns only the network connected to now, but does not return the network from which I was disconnected.

如果我用pcated intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO)去$ P $在日志中我看到的:

If I use deprecated intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO) in log I see:

当我从Wi-Fi断开,并连接到3G:

When I disconnect from WI-FI and connect to 3G:

WIFI: false
mobile: true

当我从3G断开和连接到Wi-Fi:

When I disconnect from 3G and connect to WI-FI:

mobile: false
WIFI: true

但我不希望使用德precated API。如何使用现代的API来获得网络从我被中断?

But I do not want to use deprecated api. How to use modern api to get network from which I was disconnected?

推荐答案

您可以使用以下

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");
    }
}