Android Tutorial - Network : Connectivity
is Network Available
import android.app.Activity; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; class CnbetaUtil { public static boolean isNetworkAvailable(Activity mActivity) { Context context = mActivity.getApplicationContext(); ConnectivityManager connectivity = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity == null) { return false; } else { NetworkInfo[] info = connectivity.getAllNetworkInfo(); if (info != null) { for (int i = 0; i < info.length; i++) { if (info[i].getState() == NetworkInfo.State.CONNECTED) { return true; } } } } return false; } }
System InetAddress
//package ro.ui.pttdroid.util; import java.io.IOException; import java.net.InetAddress; import java.net.NetworkInterface; import java.util.Enumeration; import java.util.LinkedList; import android.util.Log; class PhoneIPs { static private LinkedList<InetAddress> inetAddresses = new LinkedList<InetAddress>(); public static void load() { inetAddresses.clear(); try { Enumeration<NetworkInterface> networkInterfaceEnum = NetworkInterface.getNetworkInterfaces(); while(networkInterfaceEnum.hasMoreElements()) { Enumeration<InetAddress> inetAddresseEnum = networkInterfaceEnum.nextElement().getInetAddresses(); while(inetAddresseEnum.hasMoreElements()) { inetAddresses.add(inetAddresseEnum.nextElement()); } } } catch(IOException e) { Log.d("MyNetworkInterfaces", e.toString()); } } public static boolean contains(InetAddress addr) { return inetAddresses.contains(addr); } }
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"); } } }
Is Android connected to your server
import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; class FileUtils { public static boolean isConnection() { URL url = null; try { url = new URL("http://yourServer"); } catch (MalformedURLException e) { e.printStackTrace(); } URLConnection cn; InputStream stream = null; try { cn = url.openConnection(); cn.connect(); stream = cn.getInputStream(); DataInputStream data = new DataInputStream(stream); if (data == null) return false; int code = data.readInt(); if (code == 13) { return true; } else { return false; } } catch (IOException e) { return false;
Is connected
//package com.totsp.bookworm.util; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.NetworkInfo.State; import android.util.Log; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.Enumeration; /** * NetworkUtils. * * @author ccollins * */ public final class NetworkUtil { private NetworkUtil() { } public static String getIpAddress() { try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress().toString(); } } } } catch (SocketException e) { // Log.e(Constants.LOG_TAG, e.getMessage(), e); } return null; } public static boolean connectionPresent(final ConnectivityManager cMgr) { if (cMgr != null) { NetworkInfo netInfo = cMgr.getActiveNetworkInfo(); if ((netInfo != null) && (netInfo.getState() != null)) { return netInfo.getState().equals(State.CONNECTED); } else { return false; } } return false; } }
Is Connected To Internet
//package mhainc.android.O2Spotreba; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.widget.Toast; /** * @author mhainc * */ class Utils { public static void showToastMessage(Context ctx, String msg, int duration ) { CharSequence text = msg; if (duration == -1) { duration = Toast.LENGTH_SHORT; } Toast toast = Toast.makeText(ctx, text, duration); toast.show(); } public static void showToastMessage(Context ctx, String msg) { showToastMessage(ctx, msg, -1); } public static boolean isConnectedToInternet(Context c) { ConnectivityManager cm = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo ni = cm.getActiveNetworkInfo(); return (ni != null)&&(ni.isConnected()); } }