Advertisement
Guest User

NetwrokUtil.java

a guest
Aug 4th, 2015
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.91 KB | None | 0 0
  1. package com.rabinkaspal.mobikart.helpers;
  2.  
  3. import android.app.AlertDialog;
  4. import android.content.Context;
  5. import android.content.DialogInterface;
  6. import android.content.DialogInterface.OnClickListener;
  7. import android.net.ConnectivityManager;
  8. import android.net.NetworkInfo;
  9. import android.util.Log;
  10. import android.widget.Toast;
  11.  
  12. import com.rabinkaspal.mobikart.R;
  13.  
  14. public class NetworkUtil {
  15.  
  16.     public static int TYPE_WIFI = 1;
  17.     public static int TYPE_MOBILE = 2;
  18.     public static int TYPE_NOT_CONNECTED = 0;
  19.  
  20.     private static boolean isConnected = false;
  21.  
  22.     public static int getConnectivityStatus(Context context) {
  23.         ConnectivityManager cm = (ConnectivityManager) context
  24.                 .getSystemService(Context.CONNECTIVITY_SERVICE);
  25.  
  26.         NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
  27.         if (null != activeNetwork) {
  28.             if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
  29.                 return TYPE_WIFI;
  30.  
  31.             if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
  32.                 return TYPE_MOBILE;
  33.         }
  34.         return TYPE_NOT_CONNECTED;
  35.     }
  36.  
  37.     public static String getConnectivityStatusString(Context context) {
  38.         int conn = NetworkUtil.getConnectivityStatus(context);
  39.         String status = null;
  40.         if (conn == NetworkUtil.TYPE_WIFI) {
  41.             status = "Wifi enabled";
  42.         } else if (conn == NetworkUtil.TYPE_MOBILE) {
  43.             status = "Mobile data enabled";
  44.         } else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) {
  45.             status = "Not connected to Internet";
  46.         }
  47.         return status;
  48.     }
  49.  
  50.     public static void checkConnection(final Context context) {
  51.         if(!isNetworkAvailable(context)){
  52.            
  53.             AlertDialog.Builder alert = new AlertDialog.Builder(context);
  54.             alert.setTitle("Information");
  55.             alert.setIcon(R.drawable.ic_launcher);
  56.             alert.setMessage("Not connected to the internet.");
  57.            
  58.            
  59.             alert.setPositiveButton("Retry", new OnClickListener() {
  60.                
  61.                 @Override
  62.                 public void onClick(DialogInterface dialog, int which) {
  63.                     if(!isConnected){checkConnection(context);}
  64.                     else{
  65.                         Log.d("1", "asdf");
  66.                         Toast.makeText(context, "is connected", Toast.LENGTH_SHORT).show();
  67.                         dialog.cancel();}
  68.                 }
  69.             });
  70.             alert.show();
  71.         }else{Toast.makeText(context, "is connected", Toast.LENGTH_SHORT).show();}
  72.  
  73.     }
  74.  
  75.     public static boolean isNetworkAvailable(Context context) {
  76.         ConnectivityManager connectivity = (ConnectivityManager) context
  77.                 .getSystemService(Context.CONNECTIVITY_SERVICE);
  78.         if (connectivity != null) {
  79.             NetworkInfo[] info = connectivity.getAllNetworkInfo();
  80.             if (info != null) {
  81.                 for (int i = 0; i < info.length; i++) {
  82.                     if (info[i].getState() == NetworkInfo.State.CONNECTED) {
  83.                         if (!isConnected) {
  84.                             isConnected = true;
  85.                             // do your processing here ---
  86.                             // if you need to post any data to the server or get
  87.                             // status
  88.                             // update from the server
  89.                         }
  90.                         return true;
  91.                     }
  92.                 }
  93.             }
  94.         }
  95.         isConnected = false;
  96.         return false;
  97.     }
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement