ppamorim

Untitled

Oct 27th, 2014
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.22 KB | None | 0 0
  1. package com.app.app_package.app.utils;
  2.  
  3. import android.content.Context;
  4. import android.net.ConnectivityManager;
  5. import android.net.NetworkInfo;
  6. import android.os.AsyncTask;
  7. import android.os.Build;
  8.  
  9. import org.apache.http.client.ClientProtocolException;
  10. import org.apache.http.client.methods.HttpGet;
  11. import org.apache.http.impl.client.DefaultHttpClient;
  12. import org.apache.http.params.BasicHttpParams;
  13. import org.apache.http.params.HttpConnectionParams;
  14. import org.apache.http.params.HttpParams;
  15.  
  16. import java.io.IOException;
  17.  
  18. public class NetworkUtil {
  19.  
  20.     private static final String URL = "http://www.google.com";
  21.  
  22.     public static final int TYPE_WIFI = 1;
  23.     public static final int TYPE_MOBILE = 2;
  24.     public static final int TYPE_NOT_CONNECTED = 0;
  25.  
  26.     private ResponseInternet mResponseInternet;
  27.  
  28.     public NetworkUtil() {}
  29.  
  30.     public void verifyInternetAsync(ResponseInternet responseInternet) {
  31.         mResponseInternet = responseInternet;
  32.         VerifyInternetConnectionAsyncNew verifyInternetConnectionAsync = new VerifyInternetConnectionAsyncNew();
  33.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
  34.             verifyInternetConnectionAsync.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
  35.         } else {
  36.             verifyInternetConnectionAsync.execute();
  37.         }
  38.  
  39.     }
  40.  
  41.     public int getConnectivity(Context context) {
  42.         ConnectivityManager cm = (ConnectivityManager) context
  43.                 .getSystemService(Context.CONNECTIVITY_SERVICE);
  44.  
  45.         NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
  46.         if (null != activeNetwork) {
  47.             if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
  48.                 return TYPE_WIFI;
  49.  
  50.             if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
  51.                 return TYPE_MOBILE;
  52.         }
  53.         return TYPE_NOT_CONNECTED;
  54.     }
  55.  
  56.     public static int getConnectivityStatus( Context context) {
  57.         ConnectivityManager cm = (ConnectivityManager) context
  58.                 .getSystemService(Context.CONNECTIVITY_SERVICE);
  59.  
  60.         NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
  61.         if (null != activeNetwork) {
  62.             if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
  63.                 return TYPE_WIFI;
  64.  
  65.             if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
  66.                 return TYPE_MOBILE;
  67.         }
  68.         return TYPE_NOT_CONNECTED;
  69.     }
  70.  
  71.     public static boolean getConnectivityStatusBoolean( Context context) {
  72.         int conn = NetworkUtil.getConnectivityStatus(context);
  73.         boolean status = false;
  74.         if (conn == NetworkUtil.TYPE_WIFI) {
  75.             status = true;
  76.         } else if (conn == NetworkUtil.TYPE_MOBILE) {
  77.             status = true;
  78.         } else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) {
  79.             status = false;
  80.         }
  81.         return status;
  82.     }
  83.  
  84.     public class VerifyInternetConnectionAsyncNew extends AsyncTask<Void, Void, Boolean> {
  85.  
  86.         @Override
  87.         protected Boolean doInBackground(Void... voids) {
  88.  
  89.             HttpGet requestTest = new HttpGet(URL);
  90.             HttpParams params = new BasicHttpParams();
  91.             HttpConnectionParams.setConnectionTimeout(params, 4500);
  92.             HttpConnectionParams.setSoTimeout(params, 5000);
  93.             DefaultHttpClient client = new DefaultHttpClient(params);
  94.             try {
  95.                 client.execute(requestTest);
  96.                 return true;
  97.             } catch (ClientProtocolException e) {
  98.                 e.printStackTrace();
  99.                 return false;
  100.             } catch (IOException e) {
  101.                 e.printStackTrace();
  102.                 return false;
  103.             }
  104.         }
  105.  
  106.         @Override
  107.         protected void onPostExecute(Boolean hasInternet) {
  108.             super.onPostExecute(hasInternet);
  109.  
  110.             if(mResponseInternet != null) {
  111.                 if (hasInternet) {
  112.                     mResponseInternet.isConnected();
  113.                 } else {
  114.                     mResponseInternet.notConnected();
  115.                 }
  116.             }
  117.         }
  118.     }
  119.  
  120.     public static interface ResponseInternet {
  121.         void isConnected();
  122.         void notConnected();
  123.     }
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment