Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. public static boolean isNetworkAvailable(Context context) {
  2. ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  3. NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
  4. if (activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting()) {
  5. new AsyncTask<Void, Void, Boolean>() {
  6. @Override
  7. protected Boolean doInBackground(Void... params) {
  8. boolean internetStatus = false;
  9. HttpURLConnection urlc = null;
  10. try {
  11. urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection());
  12. urlc.setRequestProperty("User-Agent", "Test");
  13. urlc.setRequestProperty("Connection", "close");
  14. urlc.setConnectTimeout(1500);
  15. urlc.connect();
  16. Log.d(TAG, "GOOGLE LOADED SUCESSFULLY");
  17. if (urlc.getResponseCode() == 200) {
  18. internetStatus = true;
  19. } else
  20. internetStatus = false;
  21. } catch (Exception e) {
  22. e.printStackTrace();
  23. }
  24. return internetStatus;
  25. }
  26. }.execute();
  27. }
  28. return false;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement