0

I am using this code for checking internet connectivity. It works fine and when the device is offline, the screen just blanks out. Is there a method to show some screen during the wait?

public boolean isConnected() throws InterruptedException, IOException {
    String command = "ping -c 1 google.com";
    return Runtime.getRuntime().exec(command).waitFor() == 0;
}

The source of this code is taken from

https://stackoverflow.com/questions/9570237/android-check-internet-connection

I have increased the number of ping packets and found that it is proportionally blanking out, nevertheless, the main screen appears after that. I'm using this code at the beginning (Main Activity) and it is sufficient for my app to check online connectivity at the beginning.

SNAD
  • 33
  • 6
  • Add a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). Why the screen _blanks out_? – Estevanbs Jul 08 '22 at 17:43
  • if you call this method on the main thread- and I'm pretty sure you are - the main thread blocked. if you want to ensure the user can't interact with your app if he doesn't have an internet connection, you can show him the loading screen and dismiss it after the function call. your second choice but the user can interact with the application is to call this function on the background thread. good luck! – Mostafa Onaizan Nov 09 '22 at 19:23
  • Thank you. Since I'm going to check this at the start of the App, would like to use the first option you have suggested. Could you please provide an outline of coding. – SNAD Nov 17 '22 at 14:19
  • @Estevanbs: It blanks out as I'm using the option-1 - in the main thread. What I need is a code snippet so that a wait_for_completion screen can be shown – SNAD Nov 18 '22 at 13:27

0 Answers0