Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Verifies if the device is connected to the internet.
- * @return
- */
- public static boolean itsOnline(Context context) {
- try {
- StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
- .permitAll().build();
- StrictMode.setThreadPolicy(policy);
- int timeoutMs = 2000;
- Socket sock = new Socket();
- SocketAddress sockaddr = new InetSocketAddress("8.8.8.8", 53);
- sock.connect(sockaddr, timeoutMs);
- sock.close();
- Log.i("CONNECTION STATUS:", "connected");
- return true;
- } catch (IOException ioException) {
- Log.i("CONNECTION STATUS:", "disconnected");
- return false;
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement