Code example for NetworkInfo

Methods: getTypeisConnectedisConnectedOrConnecting

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; 
Experience pair programming with AI  Get Codota for Java