Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. /**
  2. * Verifies if the device is connected to the internet.
  3. * @return
  4. */
  5. public static boolean itsOnline(Context context) {
  6. try {
  7. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
  8. .permitAll().build();
  9.  
  10. StrictMode.setThreadPolicy(policy);
  11.  
  12. int timeoutMs = 2000;
  13. Socket sock = new Socket();
  14. SocketAddress sockaddr = new InetSocketAddress("8.8.8.8", 53);
  15.  
  16. sock.connect(sockaddr, timeoutMs);
  17. sock.close();
  18. Log.i("CONNECTION STATUS:", "connected");
  19.  
  20. return true;
  21. } catch (IOException ioException) {
  22. Log.i("CONNECTION STATUS:", "disconnected");
  23. return false;
  24. }
  25. return false;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement