Code example for NetworkInfo

Methods: getType, isConnected, isConnectedOrConnecting

0
public class Helper { 
 
	public static boolean ensureWifi(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo netInfo = cm.getActiveNetworkInfo();
		if (netInfo == null || !netInfo.isConnectedOrConnecting())
			return false; 
		// always OK if we're on wifi 
		if (netInfo.getType() == ConnectivityManager.TYPE_WIFI)
			return true; 
		// check for wifi only pref 
		if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean("wifiPref", true))
		{ 
			Log.d("Podax", "Not downloading because Wifi is required and not connected");
			return false; 
		} 
		// check for 3g data turned off 
		if (!netInfo.isConnected()) {
			Log.d("Podax", "Not downloading because background data is turned off");
			return false; 
		} 
		 
		return true;