Connectivity Tester : Connectivity « Network « Android






Connectivity Tester

    
//package eecs.umich.threegtest;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.telephony.TelephonyManager;


public class ConnectivityTester {
  //holds the threegtest that extends Activity
  private Activity activity;
  
  //WIFI or MOBIlE variables
  private ConnectivityManager connectivityManager;
  private int networkType;
  private String networkTypeName;
  
  //Mostly MOBILE variables
  private TelephonyManager telephonyManager; 
  private int mobileNetworkType;
  private String mobileNetworkTypeName;
  private String networkOperatorName;
  private String deviceID;
  
  private boolean networkStatus;
  
  public ConnectivityTester(Activity activity){
    this.activity = activity;
    System.out.println("ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ");
    setup();
    /*
    getNetworkTypeName();
    getMobileNetworkTypeName();
    
    System.out.print("Connected to: ");
    if (networkTypeName == null)
      System.out.println("Nothing");
    else
      System.out.println(networkTypeName);
    
    if (mobileNetworkTypeName != null) {
      System.out.println("Mobile Network Type: " + mobileNetworkTypeName);
    }
    System.out.println("Carrier: " + networkOperatorName);
    setNetworkStatus(true);
    System.out.println("Network status: " + networkStatus);
    setNetworkStatus(false);
    System.out.println("Network status: " + networkStatus);
    
    System.out.println("Done");
    //getNetworkTypeName();
     * 
     */
    
    
  }
  
  //initializes variables
  private void setup() {
    networkType = -1;
    mobileNetworkType = -1;
    
    networkTypeName = null;
    mobileNetworkTypeName = null;
    networkOperatorName = null; 
    
    networkStatus = false;
    deviceID = null;
    
    connectivityManager = ( ConnectivityManager ) activity.getSystemService( Context.CONNECTIVITY_SERVICE );
    telephonyManager = ( TelephonyManager ) activity.getSystemService( Context.TELEPHONY_SERVICE );
  }
  
  //returns true if currently connected and false otherwise
  public boolean isConnected() {
    try {
      return connectivityManager.getActiveNetworkInfo().isConnected();
    }
    catch (NullPointerException npe) {
      return false;
    }
  }
  //returns an integer indicating the current network type
  //returns -1 if not connected
  public int getNetworkType() {
    if (networkType != -1) { 
      return networkType;
    }
    else {
      if (isConnected()) {
        networkType = connectivityManager.getActiveNetworkInfo().getType();
      }
      else {
        networkType = -1;
      }
    }
    return networkType;
  }
  
  //Returns network type name String ("WIFI" OR "MOBILE")
  //if not connected, return null
  public String getNetworkTypeName() {
    System.out.println("A bay bay");
    if (networkTypeName != null) {
      return networkTypeName;
    }
    else {
      System.out.println("Hey");
      if (isConnected()) {
        networkTypeName = connectivityManager.getActiveNetworkInfo().getTypeName();
      }
      else {
        networkTypeName = null; 
      }
    }
    return networkTypeName;
  }
    
  //returns an integer corresponding to appropriate mobile network type
  //return -1 if not connected
  public int getMobileNetworkType() {
    if (isConnected()) {
      if (mobileNetworkType == -1) {
        mobileNetworkType = telephonyManager.getNetworkType();
      }
    }
    else {
      mobileNetworkType = -1;
    }
    return mobileNetworkType;
  }
  
  
  //returns String containing the mobile network name
  //returns null if not connected or no mobile connection
  public String getMobileNetworkTypeName() {
    getNetworkType();
    if (isConnected() && networkType == ConnectivityManager.TYPE_MOBILE ) { 
      if (mobileNetworkTypeName == null) {
        getMobileNetworkType();
        
        switch (mobileNetworkType) {
              case TelephonyManager.NETWORK_TYPE_UNKNOWN:
                mobileNetworkTypeName = "UNKNOWN";
                break;
              case TelephonyManager.NETWORK_TYPE_GPRS:
                mobileNetworkTypeName = "GPRS";
                break;
              case TelephonyManager.NETWORK_TYPE_EDGE:
                mobileNetworkTypeName = "EDGE";
                break;
              case TelephonyManager.NETWORK_TYPE_UMTS:
                mobileNetworkTypeName = "UMTS";
                break;
              default:
                mobileNetworkTypeName = "MOBILE";
            }
      }
    }
    else {
      mobileNetworkTypeName = null;
    }
    return mobileNetworkTypeName;
  }
  
  //returns carrier type
  public String getNetworkOperator() {
    if (networkOperatorName == null) {
      networkOperatorName = telephonyManager.getNetworkOperatorName();
    }
    return networkOperatorName;
  }
  
  public boolean getNetworkStatus() {
    return networkStatus;
  }
  
  public void setNetworkStatus (boolean status) {
    networkStatus = status;
  }
  
  public String getDeviceID () {
    if (deviceID == null) {
      deviceID = telephonyManager.getDeviceId();
    }
    return deviceID;
  }
    //determines if currently connected to the internet
    //if user is not connected, he/she is asked to connect to the internet via an alert
    //dialog box and the app closes
    public void detectInternetConnection () {
       if (!isConnected()) {
           System.out.println("NOT CONNECTED");
           AlertDialog.Builder builder = new AlertDialog.Builder(activity);
           builder
             .setMessage("No Internet Connection Detected! Please fix and try again.")
             .setCancelable(false)
               .setPositiveButton("OK", 
                   new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog, int id) {
                            activity.finish();
                          }
                      }
               )
               .setTitle("Internet Connection Problem")
               .show();
           
         }
         else {
           System.out.println("Internet Connection Detected");
         }
    }
  
}

   
    
    
    
  








Related examples in the same category

1.is Network Available
2.System InetAddress
3.is Net Connected
4.Is Android connected to your server
5.Is connected
6.Is Connected To Internet