0

I had the following code that worked perfect to detect if the user was connected to the internet but I have noticed that it is using deprecated API :

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

}

To let you understund I am using that function "isConnectingToInternet()" so that if it return true it will request an interstitial ad:

    private void CallNewInertial() {
        ConnectionDetector cd = new ConnectionDetector(Activityone.this);
        if (cd.isConnectingToInternet()) {
            requestNewInterstitial();
        }
    }

I have searched for API which is not deprecated and I have found the following 3: ConnectivityManager.NetworkCallback, ConnectivityManager#getNetworkCapabilities or ConnectivityManager#getLinkProperties.

I don´t know how to make the same function using those non-deprecated API. I have also searched for similar posts but all of them were for Kotlin not for Java that is my case.

  • "I had the following code that worked perfect to detect if the user was connected to the internet" -- it does not detect if the device is connected to the Internet. It detects if a device is connected to a network. That does not imply Internet access. – CommonsWare Aug 24 '22 at 21:12
  • Simple way is just to ping an url like google that never goes down,, if it returns 200, you have Internet connection – javdromero Aug 24 '22 at 21:14
  • @CommonsWare Wow, thanks for the rectification, then the code that I posted it was not as perfect as I thought, so now I can't think of anything to make it work. – Eldestornillador Aug 24 '22 at 21:27
  • IMHO, "connected to the Internet" doesn't really have any meaning. All we can say is if we can connect to some particular server for some particular protocol. For example, javdromero's suggestion assumes that the device can reach Google over HTTP(S), and that might not be the case due to firewall rules. So, I focus on trying to connect to a server that the app really needs, and using code like yours as part of diagnosing why that doesn't work (e.g., is the device in airplane mode? can I reach a different server but not mine?). – CommonsWare Aug 24 '22 at 21:47
  • @CommonsWare mmm, so to let you understund I am using that function "isConnectingToInternet()" so that if it return true it will request an intertitial ad. I am going to edit the question to let the people know for what I am using that function, thanks for the recommendation. – Eldestornillador Aug 24 '22 at 21:54

0 Answers0