So I have the following code that is using NetworkInfo API but I need to change the API as it is deprecated. This is my actual code:
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class ConnectionDetector {
private Context _context;
public ConnectionDetector(Context context) {
this._context = context;
}
public boolean isConnectingToInternet() {
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
//noinspection deprecation
NetworkInfo[] info = connectivity.getAllNetworkInfo();
for (NetworkInfo anInfo : info)
if (anInfo.getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
return false;
}
}
I have seen in the android official documentation that I can use these 3 API: ConnectivityManager.NetworkCallback, ConnectivityManager#getNetworkCapabilities or ConnectivityManager#getLinkProperties. Could anyone help me to change the actual deprecated code using the best non deprecated API for my case?