SHARE
TWEET

Utility

a guest Sep 17th, 2019 94 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.sabin.volleyexample;
  2.  
  3. import android.content.Context;
  4. import android.content.res.TypedArray;
  5.  
  6. import java.net.InetAddress;
  7. import java.net.UnknownHostException;
  8. import java.text.SimpleDateFormat;
  9. import java.util.Random;
  10. import java.util.concurrent.Callable;
  11. import java.util.concurrent.ExecutionException;
  12. import java.util.concurrent.Executors;
  13. import java.util.concurrent.Future;
  14. import java.util.concurrent.TimeUnit;
  15. import java.util.concurrent.TimeoutException;
  16.  
  17.  
  18. /**
  19.  * Created by sabin on 8/1/17.
  20.  */
  21.  
  22. public class Utility {
  23.  
  24.     public static boolean isDeviceOnline(Context context) {
  25.  
  26.         InetAddress inetAddress = null;
  27.         try {
  28.             Future<InetAddress> future = Executors.newSingleThreadExecutor().submit(new Callable<InetAddress>() {
  29.                 @Override
  30.                 public InetAddress call() {
  31.                     try {
  32.                         return InetAddress.getByName("google.com");
  33.                     } catch (UnknownHostException e) {
  34.                         return null;
  35.                     }
  36.                 }
  37.             });
  38.             inetAddress = future.get(5000, TimeUnit.MILLISECONDS);
  39.             future.cancel(true);
  40.         } catch (InterruptedException e) {
  41.         } catch (ExecutionException e) {
  42.         } catch (TimeoutException e) {
  43.         }
  44.         return inetAddress != null && !inetAddress.equals("");
  45.     }
  46.  
  47.  
  48. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top