0

I am developing a social networking application for android in which I want to check whether the other person, whose profile is being viewed (and having my app installed) is connected to internet or not. He may or may not be running my app, it doesn't matter. My only concern is the internet connectivity. If the other person is not connected to internet, a message box get appeared to the user that the person is not available right now.

So I have two questions:

1) Is it possible to check the other person's internet connectivity?

2) If yes, what is the best approach?

Hashir Sarwar
  • 1,115
  • 2
  • 14
  • 28
  • 2
    Think about yourself, not about other person. Act as other person and do coding. Create a background service to send the status of logged in user in your database and check status of respective person from the database whenever it is needed. – Ravi Aug 08 '19 at 13:45
  • 1
    also, checking for internet connectivity might be irrelevant, you really just care for this user being connected to your server instead of the internet, potentially – a_local_nobody Aug 08 '19 at 13:53
  • @RaviRupareliya thanks for your comment. I had this in my mind but it have some concerns about this method. (1) user may be connected to the internet but the internet might not be working. In this case, broadcast will not be received. (2) background service can be killed by android anytime (specially if user has a phone with custom android). So i think background service is not the best option – Hashir Sarwar Aug 08 '19 at 13:57
  • @a_local_nobody ik it looks irrelevent but this check is required for some functionalities of my app – Hashir Sarwar Aug 08 '19 at 13:59
  • Specificaly about background service, you do not have any other option. Even for whatsapp if you will kill the service you will not be able to receive messages right? – Ravi Aug 08 '19 at 13:59
  • 1
    no problem, was just throwing it out there that your check for internet connectivity might not even be needed if you have a way of checking for connected devices to your specific server instead. your question is a bit broad though – a_local_nobody Aug 08 '19 at 14:00
  • @RaviRupareliya i will be able to handle this. What about the first problem? Do you know any solution for that. Thanks anyways – Hashir Sarwar Aug 08 '19 at 14:02
  • 1
    @HashirSarwar I guess this one will help you https://stackoverflow.com/a/9570292/3134215 – Ravi Aug 08 '19 at 14:27

1 Answers1

0

You can do something like this

InternetChecker.canReachServer(URL_SERVER, 10000, 10000, reachable -> {
                if(reachable){
                    //connected
                }
                else{
                    //no connected
                }
            });


public class InternetChecker {

public static void canReachServer(String Url, int dnsTimeout, int requestTimeout, Callback<Boolean> result) {
    httpThread.run(() -> {
        InetAddress ip = ResolveHost(Url, dnsTimeout);
        if (ip == null) {
            Log.d("INTERNET STEP 1", "FAILED");
            result.callback(false);
        } else {
            Log.d("INTERNET STEP 1", "SUCCESS: " + ip.getHostAddress());
            OkHttpClient client = new OkHttpClient.Builder()
                    .connectTimeout(requestTimeout, TimeUnit.MILLISECONDS)
                    .writeTimeout(requestTimeout, TimeUnit.MILLISECONDS)
                    .readTimeout(requestTimeout, TimeUnit.MILLISECONDS)
                    .build();

            Request request = new Request.Builder()
                    .url(Url)
                    .head()
                    .build();

            try {
                client.newCall(request).execute();
            } catch (IOException e) {
                Log.d("INTERNET STEP 2", "FAILED: " + e.getMessage());
                result.callback(false);
                return;
            }

            Log.d("INTERNET STEP 2", "SUCCESS");
            result.callback(true);
        }

    });
}

public static InetAddress ResolveHost(String sUrl, int timeout) {

    //Run the DNS resolve method manually to be able to time it out.
    URL url;
    try {
        url = new URL(sUrl);
    } catch (MalformedURLException e) {
        return null;
    }
    //Resolve the host IP
    DNSResolver dnsRes = new DNSResolver(url.getHost());
    Thread t = new Thread(dnsRes);
    t.start();
    try {
        t.join((long) (timeout));
    } catch (InterruptedException e) {
        //DNS interrupted
        return null;
    }
    InetAddress inetAddr = dnsRes.get();
    if (inetAddr == null) {
        //DNS timed out.
        return null;
    }
    //DNS resolved.
    return dnsRes.inetAddr;
}

public static class DNSResolver implements Runnable {
    private String domain;
    private InetAddress inetAddr;

    public DNSResolver(String domain) {
        this.domain = domain;
    }

    public void run() {
        try {
            InetAddress addr = InetAddress.getByName(domain);
            set(addr);
        } catch (UnknownHostException e) {
        }
    }

    public synchronized void set(InetAddress inetAddr) {
        this.inetAddr = inetAddr;
    }

    public synchronized InetAddress get() {
        return inetAddr;
    }
}
}
Amaury Ricardo
  • 267
  • 2
  • 5