下面列出了android.net.NetworkInfo#isRoaming ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
}
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;
}
/**
* 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();
}
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;
}
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();
}
/**
* 判断网络是否可用
*
* @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();
}
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();
}
/**
* 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();
}
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;
}
}
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()));
}
}
}
}
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));
}
}
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()));
}
}
}
}
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());
}
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());
}
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());
}
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;
}
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;
}
}
}
}
}
}
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;
}
}
}
}
}
}
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;
}
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;
}
}
}
}
}
}