0

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?

  • please search before you post: https://stackoverflow.com/questions/53532406/activenetworkinfo-type-is-deprecated-in-api-level-28 – user496854 Aug 24 '22 at 18:13
  • @user496854 I have seen that awnser but notice that the post you shared me is about kotlin, not java :/ – Eldestornillador Aug 24 '22 at 18:48
  • They both use the same APIs. the code itself is irrelevant -- they're telling you:Now we need to use ConnectivityManager.NetworkCallback API or ConnectivityManager#getNetworkCapabilities or ConnectivityManager#getLinkProperties – user496854 Aug 24 '22 at 19:00
  • Does this answer your question? [activeNetworkInfo.type is deprecated in API level 28](https://stackoverflow.com/questions/53532406/activenetworkinfo-type-is-deprecated-in-api-level-28) – Ultimo_m Oct 20 '22 at 21:55

0 Answers0