/** * Checks if we have a valid Internet Connection on the device. * @param ctx * @return True if device has internet * * Code from: http://www.androidsnippets.org/snippets/131/ */ public static boolean haveInternet(Context ctx) { NetworkInfo info = (NetworkInfo) ((ConnectivityManager) ctx .getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo(); if (info == null || !info.isConnected()) { return false; } if (info.isRoaming()) { // here is the roaming option you can change it if you want to // disable internet while roaming, just return false return false; } return true; }
@SuppressWarnings("deprecation") private static boolean isRoaming(ConnectivityManager connectivityManager, NetworkInfo networkInfo) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) { return networkInfo.isRoaming(); } try { NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork()); return capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING); } catch (Exception e) { return networkInfo.isRoaming(); } }
@Test public void testNetworkStateRoaming() { NetworkInfo networkInfo = mock(NetworkInfo.class); when(networkInfo.isConnected()).thenReturn(true); when(networkInfo.isConnectedOrConnecting()).thenReturn(true); when(networkInfo.getType()).thenReturn(ConnectivityManager.TYPE_MOBILE); when(networkInfo.isRoaming()).thenReturn(true); ConnectivityManager connectivityManager = mock(ConnectivityManager.class); when(connectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo); Context context = mock(MockContext.class); when(context.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(connectivityManager); assertThat(Device.getNetworkType(context)).isEqualTo(JobRequest.NetworkType.CONNECTED); }
boolean roaming = networkInfo.isRoaming(); logd("Roaming: " + roaming); return !roaming;
private void setupNetworkRequirement(Job job, JobRequest.NetworkType requirement, boolean connected, int networkType, boolean roaming) { NetworkInfo networkInfo = mock(NetworkInfo.class); when(networkInfo.isConnected()).thenReturn(connected); when(networkInfo.isConnectedOrConnecting()).thenReturn(connected); when(networkInfo.getType()).thenReturn(networkType); when(networkInfo.isRoaming()).thenReturn(roaming); ConnectivityManager connectivityManager = mock(ConnectivityManager.class); when(connectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo); when(job.getContext().getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(connectivityManager); when(job.getParams().getRequest().requiredNetworkType()).thenReturn(requirement); }
@Test public void testNetworkStateWifiAndRoaming() { NetworkInfo networkInfo = mock(NetworkInfo.class); when(networkInfo.isConnected()).thenReturn(true); when(networkInfo.isConnectedOrConnecting()).thenReturn(true); when(networkInfo.getType()).thenReturn(ConnectivityManager.TYPE_WIFI); when(networkInfo.isRoaming()).thenReturn(true); ConnectivityManager connectivityManager = mock(ConnectivityManager.class); when(connectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo); Context context = mock(MockContext.class); when(context.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(connectivityManager); assertThat(Device.getNetworkType(context)).isEqualTo(JobRequest.NetworkType.UNMETERED); }
@Test public void testNetworkStateWifiAndMobile() { NetworkInfo networkInfo = mock(NetworkInfo.class); when(networkInfo.isConnected()).thenReturn(true); when(networkInfo.isConnectedOrConnecting()).thenReturn(true); when(networkInfo.getType()).thenReturn(ConnectivityManager.TYPE_WIFI); when(networkInfo.isRoaming()).thenReturn(false); ConnectivityManager connectivityManager = mock(ConnectivityManager.class); when(connectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo); Context context = mock(MockContext.class); when(context.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(connectivityManager); assertThat(Device.getNetworkType(context)).isEqualTo(JobRequest.NetworkType.UNMETERED); }
@Test public void testNetworkStateMeteredNotRoaming() { NetworkInfo networkInfo = mock(NetworkInfo.class); when(networkInfo.isConnected()).thenReturn(true); when(networkInfo.isConnectedOrConnecting()).thenReturn(true); when(networkInfo.getType()).thenReturn(ConnectivityManager.TYPE_MOBILE); when(networkInfo.isRoaming()).thenReturn(false); ConnectivityManager connectivityManager = mock(ConnectivityManager.class); when(connectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo); Context context = mock(MockContext.class); when(context.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(connectivityManager); assertThat(Device.getNetworkType(context)).isEqualTo(JobRequest.NetworkType.NOT_ROAMING); }
private static Connectivity create(NetworkInfo networkInfo) { return new Builder() .state(networkInfo.getState()) .detailedState(networkInfo.getDetailedState()) .type(networkInfo.getType()) .subType(networkInfo.getSubtype()) .available(networkInfo.isAvailable()) .failover(networkInfo.isFailover()) .roaming(networkInfo.isRoaming()) .typeName(networkInfo.getTypeName()) .subTypeName(networkInfo.getSubtypeName()) .reason(networkInfo.getReason()) .extraInfo(networkInfo.getExtraInfo()) .build(); }
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo ni = cm.getActiveNetworkInfo(); //[edit: check for null] if(ni != null) { //...Here check if this is connected and available... return ni.isRoaming(); }
/** * Checks if we have a valid Internet Connection on the device. * @param ctx * @return True if device has internet * * Code from: http://www.androidsnippets.org/snippets/131/ */ public static boolean haveInternet(Context ctx) { NetworkInfo info = (NetworkInfo) ((ConnectivityManager) ctx .getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo(); if (info == null || !info.isConnected()) { return false; } if (info.isRoaming()) { // here is the roaming option you can change it if you want to // disable internet while roaming, just return false return false; } return true; }
return false; if (info.isRoaming()) {
case ConnectivityManager.TYPE_MOBILE_SUPL: case ConnectivityManager.TYPE_WIMAX: ret.isRoaming = info.isRoaming(); ret.netType = ConnectivityManager.TYPE_MOBILE; ret.netEnabled = true;
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo ni = cm.getActiveNetworkInfo(); //[edit: check for null] if(ni != null) { //...Here check if this is connected and available... if (ni.isRoaming()) { // user has enabled data even while he is on Roaming!! } }
NetworkInfo info = ((ConnectivityManager) mContext .getSystemService(Context.CONNECTIVITY_SERVICE)) .getActiveNetworkInfo(); if (info == null || !info.isConnected()) { return false; } if (info.isRoaming()) { return true; }
private boolean haveInternet(){ NetworkInfo info = ((ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo(); if (info==null || !info.isConnected()) { return false; } if (info.isRoaming()) { // here is the roaming option you can change it if you want to disable internet while roaming, just return false return true; } return true; }
/*@return boolean return true if the application can access the internet*/ public static boolean haveInternet(Context context){ NetworkInfo info = ((ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo(); if (info==null || !info.isConnected()) { return false; } if (info.isRoaming()) { // here is the roaming option you can change it if you want to disable internet while roaming, just return false return true; } return true; }
public static boolean isOnline(Context ctx) { NetworkInfo info = (NetworkInfo) ((ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo(); if (info == null || !info.isConnected()) { return false; } if (info.isRoaming()) { // here is the roaming option you can change it if you want to // disable internet while roaming, just return false return false; } return true; }
/** * Check the network's state on the current device. */ public static boolean checkNetworkStatus(Context ctx){ ConnectivityManager manager = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = manager.getActiveNetworkInfo(); if(info == null) return false; if(info.isRoaming()) return false; return info != null && info.isConnected(); }
/** * Checks if user is on is in roaming. * * @return boolean variable, which describes if user is in roaming. */ public boolean isOnRoaming() { NetworkInfo networkInfo = getConfiguration().getConnectivityManager().getActiveNetworkInfo(); return networkInfo != null && networkInfo.isRoaming(); }