ConnectivityManager.getActiveNetworkInfo() / NetworkInfo 在 API 中已弃用 29. 有什么替代方案?
ConnectivityManager.getActiveNetworkInfo() / NetworkInfo is deprecated in API 29. What's an alternative?
似乎整个 NetworkInfo is deprecated 都在 API 29.
所以我正在寻找一种替代方法来检查网络是否已连接。
例如。替代
connectivityManager.activeNetworkInfo?.isConnected == true
旁注:我知道现在有回调,但我想同步获取此信息。另外,我当然知道这可能不是准确的信息,但我想知道它。
这正在 API 29:
connectivityManager.isDefaultNetworkActive
android.net.NetworkInfo
此 class 已在 API 级别 29 中弃用。
您应该改为使用 ConnectivityManager.NetworkCallback
API 来了解连接变化
参考Linkhttps://developer.android.com/reference/android/net/NetworkInfo.html
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builder builder = new NetworkRequest.Builder();
connectivityManager.registerNetworkCallback(
builder.build(),
new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
// Network Available
}
@Override
public void onLost(Network network) {
// Network Not Available
}
}
);
解决方法是这样的:
val capability = connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)
return capability?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) ?: false
解决方法是
public void registerNetworkCallback()
{
try {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builder builder = new NetworkRequest.Builder();
connectivityManager.registerNetworkCallback(builder.build(),new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
Variables.isNetworkConnected = true; // Global Static Variable
}
@Override
public void onLost(Network network) {
Variables.isNetworkConnected = false; // Global Static Variable
}
}
);
Variables.isNetworkConnected = false;
}catch (Exception e){
Variables.isNetworkConnected = false;
}
}
在此处查看完整代码:Gist
private boolean checkInternetConnection(Activity activity) {
ConnectivityManager connectivityManager = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Network network = null;
if (connectivityManager == null) {
return false;
} else {
network = connectivityManager.getActiveNetwork();
NetworkCapabilities networkCapabilities = connectivityManager.getNetworkCapabilities(network);
if (networkCapabilities == null) {
return false;
}
if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) ||
networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
return true;
}
}
} else {
if (connectivityManager == null) {
return false;
}
if (connectivityManager.getActiveNetworkInfo() == null) {
return false;
}
return connectivityManager.getActiveNetworkInfo().isConnected();
}
return false;
}
似乎整个 NetworkInfo is deprecated 都在 API 29.
所以我正在寻找一种替代方法来检查网络是否已连接。 例如。替代
connectivityManager.activeNetworkInfo?.isConnected == true
旁注:我知道现在有回调,但我想同步获取此信息。另外,我当然知道这可能不是准确的信息,但我想知道它。
这正在 API 29:
connectivityManager.isDefaultNetworkActive
android.net.NetworkInfo
此 class 已在 API 级别 29 中弃用。
您应该改为使用 ConnectivityManager.NetworkCallback
API 来了解连接变化
参考Linkhttps://developer.android.com/reference/android/net/NetworkInfo.html
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builder builder = new NetworkRequest.Builder();
connectivityManager.registerNetworkCallback(
builder.build(),
new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
// Network Available
}
@Override
public void onLost(Network network) {
// Network Not Available
}
}
);
解决方法是这样的:
val capability = connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)
return capability?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) ?: false
解决方法是
public void registerNetworkCallback()
{
try {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builder builder = new NetworkRequest.Builder();
connectivityManager.registerNetworkCallback(builder.build(),new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
Variables.isNetworkConnected = true; // Global Static Variable
}
@Override
public void onLost(Network network) {
Variables.isNetworkConnected = false; // Global Static Variable
}
}
);
Variables.isNetworkConnected = false;
}catch (Exception e){
Variables.isNetworkConnected = false;
}
}
在此处查看完整代码:Gist
private boolean checkInternetConnection(Activity activity) {
ConnectivityManager connectivityManager = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Network network = null;
if (connectivityManager == null) {
return false;
} else {
network = connectivityManager.getActiveNetwork();
NetworkCapabilities networkCapabilities = connectivityManager.getNetworkCapabilities(network);
if (networkCapabilities == null) {
return false;
}
if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) ||
networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
return true;
}
}
} else {
if (connectivityManager == null) {
return false;
}
if (connectivityManager.getActiveNetworkInfo() == null) {
return false;
}
return connectivityManager.getActiveNetworkInfo().isConnected();
}
return false;
}