Java Code Examples for android.net.NetworkInfo#isRoaming()

The following examples show how to use android.net.NetworkInfo#isRoaming() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Util.java    From Popeens-DSub with GNU General Public License v3.0 6 votes vote down vote up
public static boolean isNetworkConnected(Context context, boolean streaming) {
      ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo networkInfo = manager.getActiveNetworkInfo();
      boolean connected = networkInfo != null && networkInfo.isConnected();

if(streaming) {
	boolean wifiConnected = connected && networkInfo.getType() == ConnectivityManager.TYPE_WIFI;
	boolean wifiRequired = isWifiRequiredForDownload(context);

	boolean isLocalNetwork = connected && !networkInfo.isRoaming();
	boolean localNetworkRequired = isLocalNetworkRequiredForDownload(context);

	return connected && (!wifiRequired || wifiConnected) && (!localNetworkRequired || isLocalNetwork);
} else {
	return connected;
}
  }
 
Example 2
Source File: ConnectionsManager.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
public static boolean isRoaming()
{
    try
    {
        ConnectivityManager connectivityManager = (ConnectivityManager) ApplicationLoader.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = connectivityManager != null ? connectivityManager.getActiveNetworkInfo() : null;
        if (netInfo != null)
            return netInfo.isRoaming();
    }
    catch (Exception e)
    {
        FileLog.e(e);
    }

    return false;
}
 
Example 3
Source File: AndroidNetworkLibrary.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * Indicates whether the device is roaming on the currently active network. When true, it
 * suggests that use of data may incur extra costs.
 */
@CalledByNative
private static boolean getIsRoaming() {
    ConnectivityManager connectivityManager =
            (ConnectivityManager) ContextUtils.getApplicationContext().getSystemService(
                    Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (networkInfo == null) return false; // No active network.
    return networkInfo.isRoaming();
}
 
Example 4
Source File: NotificationConnectionHandler.java    From an2linuxclient with GNU General Public License v3.0 5 votes vote down vote up
static void sendToAllEnabledMobileServers(Notification n, Context c, List<MobileServer> enabledMobileServers){
    ConnectivityManager connMgr = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isConnected() && networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
        for(MobileServer mobileServer : enabledMobileServers){
            if (!networkInfo.isRoaming() || mobileServer.isRoamingAllowed()){
                ThreadPoolHandler.enqueueRunnable(new TcpNotificationConnection(c, n,
                        mobileServer.getCertificate(),
                        mobileServer.getIpOrHostname(),
                        mobileServer.getPortNumber()));
            }
        }
    }
}
 
Example 5
Source File: Net.java    From HttpInfo with Apache License 2.0 5 votes vote down vote up
public static boolean checkIsRoaming(Context context) {
    ConnectivityManager manager = (ConnectivityManager)
            context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (manager != null) {
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        if (networkInfo != null) {
            return networkInfo.isRoaming();
        }
    }
    return false;
}
 
Example 6
Source File: Requirements.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private boolean checkNetworkRequirements(Context context) {
  int networkRequirement = getRequiredNetworkType();
  if (networkRequirement == NETWORK_TYPE_NONE) {
    return true;
  }
  ConnectivityManager connectivityManager =
      (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
  if (networkInfo == null || !networkInfo.isConnected()) {
    logd("No network info or no connection.");
    return false;
  }
  if (!checkInternetConnectivity(connectivityManager)) {
    return false;
  }
  if (networkRequirement == NETWORK_TYPE_ANY) {
    return true;
  }
  if (networkRequirement == NETWORK_TYPE_NOT_ROAMING) {
    boolean roaming = networkInfo.isRoaming();
    logd("Roaming: " + roaming);
    return !roaming;
  }
  boolean activeNetworkMetered = isActiveNetworkMetered(connectivityManager, networkInfo);
  logd("Metered network: " + activeNetworkMetered);
  if (networkRequirement == NETWORK_TYPE_UNMETERED) {
    return !activeNetworkMetered;
  }
  if (networkRequirement == NETWORK_TYPE_METERED) {
    return activeNetworkMetered;
  }
  throw new IllegalStateException();
}
 
Example 7
Source File: PairingConnectionHandler.java    From an2linuxclient with GNU General Public License v3.0 5 votes vote down vote up
public void startMobilePairing(String ipOrHostname, int port, boolean allowRoaming, Context c){
    ConnectivityManager connMgr = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
    boolean wifiConnected = false;
    boolean mobileConnected = false;

    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

    if (networkInfo != null && networkInfo.isConnected()){
        if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) wifiConnected = true;
        else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) mobileConnected = true;

        boolean allowedToConnect = false;

        // allowing mobile pairing even when connected to wifi because why not
        if (wifiConnected){
            allowedToConnect = true;
        } else if (mobileConnected){
            if (!networkInfo.isRoaming() || allowRoaming){
                allowedToConnect = true;
            } else {
                notifyObservers(new PairingConnectionCallbackMessage(NOT_ALLOWED_TO_ROAM));
            }
        }

        if (allowedToConnect){
            pairingConnection = new TcpPairingConnection(ipOrHostname, port, c);
            pairingConnection.addObserver(this);

            new Thread(pairingConnection).start();
        }

    } else {
        notifyObservers(new PairingConnectionCallbackMessage(NOT_CONNECTED));
    }
}
 
Example 8
Source File: ConnectivityHelper.java    From WheelViewDemo with Apache License 2.0 5 votes vote down vote up
/**
 * 判断网络是否可用
 * 
 * @param context context
 * @return boolean
 */
public static boolean isConnectivityAvailable(Context context) {
	// 判断网络是否可用
	ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
	Objects.requireNonNull(cm);
       NetworkInfo info = cm.getActiveNetworkInfo();
	if (info == null || !info.isConnected()) {
		return false;
	}
	return info.isAvailable() || info.isRoaming();
}
 
Example 9
Source File: Requirements.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private boolean checkNetworkRequirements(Context context) {
  int networkRequirement = getRequiredNetworkType();
  if (networkRequirement == NETWORK_TYPE_NONE) {
    return true;
  }
  ConnectivityManager connectivityManager =
      (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
  if (networkInfo == null || !networkInfo.isConnected()) {
    logd("No network info or no connection.");
    return false;
  }
  if (!checkInternetConnectivity(connectivityManager)) {
    return false;
  }
  if (networkRequirement == NETWORK_TYPE_ANY) {
    return true;
  }
  if (networkRequirement == NETWORK_TYPE_NOT_ROAMING) {
    boolean roaming = networkInfo.isRoaming();
    logd("Roaming: " + roaming);
    return !roaming;
  }
  boolean activeNetworkMetered = isActiveNetworkMetered(connectivityManager, networkInfo);
  logd("Metered network: " + activeNetworkMetered);
  if (networkRequirement == NETWORK_TYPE_UNMETERED) {
    return !activeNetworkMetered;
  }
  if (networkRequirement == NETWORK_TYPE_METERED) {
    return activeNetworkMetered;
  }
  throw new IllegalStateException();
}
 
Example 10
Source File: Helper.java    From Learning-Resources with MIT License 5 votes vote down vote up
/**
 * Check if roaming connection active.
 *
 * @return
 */
private static boolean isConnectionRoaming(Context ctx) {
    ConnectivityManager cm = (ConnectivityManager) ctx
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getActiveNetworkInfo();

    return ni != null && ni.isRoaming();

}
 
Example 11
Source File: GcmPrefs.java    From android_packages_apps_GmsCore with Apache License 2.0 5 votes vote down vote up
public String getNetworkPrefForInfo(NetworkInfo info) {
    if (info == null) return PREF_NETWORK_OTHER;
    if (info.isRoaming()) return PREF_NETWORK_ROAMING;
    switch (info.getType()) {
        case ConnectivityManager.TYPE_MOBILE:
            return PREF_NETWORK_MOBILE;
        case ConnectivityManager.TYPE_WIFI:
            return PREF_NETWORK_WIFI;
        default:
            return PREF_NETWORK_OTHER;
    }
}
 
Example 12
Source File: NotificationConnectionHandler.java    From an2linuxclient with GNU General Public License v3.0 5 votes vote down vote up
static void sendToAllEnabledMobileServers(Notification n, Context c, List<MobileServer> enabledMobileServers){
    ConnectivityManager connMgr = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isConnected() && networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
        for(MobileServer mobileServer : enabledMobileServers){
            if (!networkInfo.isRoaming() || mobileServer.isRoamingAllowed()){
                ThreadPoolHandler.enqueueRunnable(new TcpNotificationConnection(c, n,
                        mobileServer.getCertificate(),
                        mobileServer.getIpOrHostname(),
                        mobileServer.getPortNumber()));
            }
        }
    }
}
 
Example 13
Source File: DownloaderService.java    From Alite with GNU General Public License v3.0 4 votes vote down vote up
private void updateNetworkState(NetworkInfo info) {
    boolean isConnected = mIsConnected;
    boolean isFailover = mIsFailover;
    boolean isCellularConnection = mIsCellularConnection;
    boolean isRoaming = mIsRoaming;
    boolean isAtLeast3G = mIsAtLeast3G;
    if (null != info) {
        mIsRoaming = info.isRoaming();
        mIsFailover = info.isFailover();
        mIsConnected = info.isConnected();
        updateNetworkType(info.getType(), info.getSubtype());
    } else {
        mIsRoaming = false;
        mIsFailover = false;
        mIsConnected = false;
        updateNetworkType(-1, -1);
    }
    mStateChanged = (mStateChanged || isConnected != mIsConnected
            || isFailover != mIsFailover
            || isCellularConnection != mIsCellularConnection
            || isRoaming != mIsRoaming || isAtLeast3G != mIsAtLeast3G);
    if (Constants.LOGVV) {
        if (mStateChanged) {
            Log.v(LOG_TAG, "Network state changed: ");
            Log.v(LOG_TAG, "Starting State: " +
                    (isConnected ? "Connected " : "Not Connected ") +
                    (isCellularConnection ? "Cellular " : "WiFi ") +
                    (isRoaming ? "Roaming " : "Local ") +
                    (isAtLeast3G ? "3G+ " : "<3G "));
            Log.v(LOG_TAG, "Ending State: " +
                    (mIsConnected ? "Connected " : "Not Connected ") +
                    (mIsCellularConnection ? "Cellular " : "WiFi ") +
                    (mIsRoaming ? "Roaming " : "Local ") +
                    (mIsAtLeast3G ? "3G+ " : "<3G "));

            if (isServiceRunning()) {
                if (mIsRoaming) {
                    mStatus = STATUS_WAITING_FOR_NETWORK;
                    mControl = CONTROL_PAUSED;
                } else if (mIsCellularConnection) {
                    DownloadsDB db = DownloadsDB.getDB(this);
                    int flags = db.getFlags();
                    if (0 == (flags & FLAGS_DOWNLOAD_OVER_CELLULAR)) {
                        mStatus = STATUS_QUEUED_FOR_WIFI;
                        mControl = CONTROL_PAUSED;
                    }
                }
            }

        }
    }
}
 
Example 14
Source File: Util.java    From NetGuard with GNU General Public License v3.0 4 votes vote down vote up
public static boolean isRoaming(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = (cm == null ? null : cm.getActiveNetworkInfo());
    return (ni != null && ni.isRoaming());
}
 
Example 15
Source File: PreferencesProviderWrapper.java    From CSipSimple with GNU General Public License v3.0 4 votes vote down vote up
private boolean isValidMobileConnectionFor(NetworkInfo ni, String suffix) {

        boolean valid_for_3g = getPreferenceBooleanValue("use_3g_" + suffix, false);
        boolean valid_for_edge = getPreferenceBooleanValue("use_edge_" + suffix, false);
        boolean valid_for_gprs = getPreferenceBooleanValue("use_gprs_" + suffix, false);
        boolean valid_for_roaming = getPreferenceBooleanValue("use_roaming_" + suffix, true);
        
        if(!valid_for_roaming && ni != null) {
            if(ni.isRoaming()) {
                return false;
            }
        }
        
        if ((valid_for_3g || valid_for_edge || valid_for_gprs) &&
                ni != null) {
            int type = ni.getType();

            // Any mobile network connected
            if (ni.isConnected() &&
                    // Type 3,4,5 are other mobile data ways
                    (type == ConnectivityManager.TYPE_MOBILE || (type <= 5 && type >= 3))) {
                int subType = ni.getSubtype();

                // 3G (or better)
                if (valid_for_3g &&
                        subType >= TelephonyManager.NETWORK_TYPE_UMTS) {
                    return true;
                }

                // GPRS (or unknown)
                if (valid_for_gprs
                        &&
                        (subType == TelephonyManager.NETWORK_TYPE_GPRS || subType == TelephonyManager.NETWORK_TYPE_UNKNOWN)) {
                    return true;
                }

                // EDGE
                if (valid_for_edge &&
                        subType == TelephonyManager.NETWORK_TYPE_EDGE) {
                    return true;
                }
            }
        }
        return false;
    }
 
Example 16
Source File: DownloaderService.java    From travelguide with Apache License 2.0 4 votes vote down vote up
private void updateNetworkState(NetworkInfo info) {
    boolean isConnected = mIsConnected;
    boolean isFailover = mIsFailover;
    boolean isCellularConnection = mIsCellularConnection;
    boolean isRoaming = mIsRoaming;
    boolean isAtLeast3G = mIsAtLeast3G;
    if (null != info) {
        mIsRoaming = info.isRoaming();
        mIsFailover = info.isFailover();
        mIsConnected = info.isConnected();
        updateNetworkType(info.getType(), info.getSubtype());
    } else {
        mIsRoaming = false;
        mIsFailover = false;
        mIsConnected = false;
        updateNetworkType(-1, -1);
    }
    mStateChanged = (mStateChanged || isConnected != mIsConnected
            || isFailover != mIsFailover
            || isCellularConnection != mIsCellularConnection
            || isRoaming != mIsRoaming || isAtLeast3G != mIsAtLeast3G);
    if (Constants.LOGVV) {
        if (mStateChanged) {
            Log.v(LOG_TAG, "Network state changed: ");
            Log.v(LOG_TAG, "Starting State: " +
                    (isConnected ? "Connected " : "Not Connected ") +
                    (isCellularConnection ? "Cellular " : "WiFi ") +
                    (isRoaming ? "Roaming " : "Local ") +
                    (isAtLeast3G ? "3G+ " : "<3G "));
            Log.v(LOG_TAG, "Ending State: " +
                    (mIsConnected ? "Connected " : "Not Connected ") +
                    (mIsCellularConnection ? "Cellular " : "WiFi ") +
                    (mIsRoaming ? "Roaming " : "Local ") +
                    (mIsAtLeast3G ? "3G+ " : "<3G "));

            if (isServiceRunning()) {
                if (mIsRoaming) {
                    mStatus = STATUS_WAITING_FOR_NETWORK;
                    mControl = CONTROL_PAUSED;
                } else if (mIsCellularConnection) {
                    DownloadsDB db = DownloadsDB.getDB(this);
                    int flags = db.getFlags();
                    if (0 == (flags & FLAGS_DOWNLOAD_OVER_CELLULAR)) {
                        mStatus = STATUS_QUEUED_FOR_WIFI;
                        mControl = CONTROL_PAUSED;
                    }
                }
            }

        }
    }
}
 
Example 17
Source File: DownloaderService.java    From play-apk-expansion with Apache License 2.0 4 votes vote down vote up
private void updateNetworkState(NetworkInfo info) {
    boolean isConnected = mIsConnected;
    boolean isFailover = mIsFailover;
    boolean isCellularConnection = mIsCellularConnection;
    boolean isRoaming = mIsRoaming;
    boolean isAtLeast3G = mIsAtLeast3G;
    if (null != info) {
        mIsRoaming = info.isRoaming();
        mIsFailover = info.isFailover();
        mIsConnected = info.isConnected();
        updateNetworkType(info.getType(), info.getSubtype());
    } else {
        mIsRoaming = false;
        mIsFailover = false;
        mIsConnected = false;
        updateNetworkType(-1, -1);
    }
    mStateChanged = (mStateChanged || isConnected != mIsConnected
            || isFailover != mIsFailover
            || isCellularConnection != mIsCellularConnection
            || isRoaming != mIsRoaming || isAtLeast3G != mIsAtLeast3G);
    if (Constants.LOGVV) {
        if (mStateChanged) {
            Log.v(LOG_TAG, "Network state changed: ");
            Log.v(LOG_TAG, "Starting State: " +
                    (isConnected ? "Connected " : "Not Connected ") +
                    (isCellularConnection ? "Cellular " : "WiFi ") +
                    (isRoaming ? "Roaming " : "Local ") +
                    (isAtLeast3G ? "3G+ " : "<3G "));
            Log.v(LOG_TAG, "Ending State: " +
                    (mIsConnected ? "Connected " : "Not Connected ") +
                    (mIsCellularConnection ? "Cellular " : "WiFi ") +
                    (mIsRoaming ? "Roaming " : "Local ") +
                    (mIsAtLeast3G ? "3G+ " : "<3G "));

            if (isServiceRunning()) {
                if (mIsRoaming) {
                    mStatus = STATUS_WAITING_FOR_NETWORK;
                    mControl = CONTROL_PAUSED;
                } else if (mIsCellularConnection) {
                    DownloadsDB db = DownloadsDB.getDB(this);
                    int flags = db.getFlags();
                    if (0 == (flags & FLAGS_DOWNLOAD_OVER_CELLULAR)) {
                        mStatus = STATUS_QUEUED_FOR_WIFI;
                        mControl = CONTROL_PAUSED;
                    }
                }
            }

        }
    }
}
 
Example 18
Source File: UploadService.java    From apigee-android-sdk with Apache License 2.0 4 votes vote down vote up
public boolean allowedToSendData() {
	// Prevent sending of data if this application is turned off.
	// TODO: Need to add code to look at isActive in CompositeApp

	ApigeeActiveSettings activeSettings = this.getActiveSettings();

	if (activeSettings == null) {
		Log.v(ClientLog.TAG_MONITORING_CLIENT,
				"Not sending data because App was not initialized");
		return false;
	}
	
	Boolean monitoringDisabled = activeSettings.getMonitoringDisabled();

	if (monitoringDisabled == null) {
		Log.v(ClientLog.TAG_MONITORING_CLIENT,
				"Not sending data because App was not properly initialized");
		return false;
	}

	if (monitoringDisabled) {
		Log.v(ClientLog.TAG_MONITORING_CLIENT,
				"Not sending data app is inactive");
		return false;
	}
	
	if (this.monitoringClient.isPaused()) {
		Log.v(ClientLog.TAG_MONITORING_CLIENT, "Not sending data -- monitoring is paused");
		return false;
	}

	try {
		// does not upload data if not connected to internet
		ConnectivityManager connectivityManager = (ConnectivityManager) getAppActivity()
			.getSystemService(Context.CONNECTIVITY_SERVICE);

		NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

		if (networkInfo == null || !networkInfo.isConnected()) {
			Log.v(ClientLog.TAG_MONITORING_CLIENT,
				"Not sending data because phone is not connected to internet");
			return false;
		}

		if (networkInfo.isRoaming()) {
			Log.v(ClientLog.TAG_MONITORING_CLIENT,
					"Not sending data because phone is on roaming");
			return false;
		}
	} catch (Exception e) {
	}

	Log.v(ClientLog.TAG_MONITORING_CLIENT,
			"Phone is in a state that it is allowed to send metrics");
	return true;
}
 
Example 19
Source File: Util.java    From tracker-control-android with GNU General Public License v3.0 4 votes vote down vote up
public static boolean isRoaming(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = (cm == null ? null : cm.getActiveNetworkInfo());
    return (ni != null && ni.isRoaming());
}
 
Example 20
Source File: Util.java    From kcanotify with GNU General Public License v3.0 4 votes vote down vote up
public static boolean isRoaming(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = (cm == null ? null : cm.getActiveNetworkInfo());
    return (ni != null && ni.isRoaming());
}