问题
Seem like whole NetworkInfo is deprecated on API 29.
So I am looking for an alternative to check if the network is connected. E.g. alternative to
connectivityManager.activeNetworkInfo?.isConnected == true
Sidenote: I know there is a callback now, but I'd like to get this info synchronously. Also, of course I'm aware that it may not be a precise info though I'd like to have it.
回答1:
The solution is this:
val capability = connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)
return capability?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) ?: false
回答2:
This is working on API 29:
connectivityManager.isDefaultNetworkActive
回答3:
android.net.NetworkInfo
This class was deprecated in API level 29.
You should instead use the ConnectivityManager.NetworkCallback
API to know about connectivity changes
Referrence Link https://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
}
}
);
回答4:
The solution is
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;
}
}
Check full code here : Gist
回答5:
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;
}
来源:https://stackoverflow.com/questions/56984016/connectivitymanager-getactivenetworkinfo-networkinfo-is-deprecated-in-api-29