Android ConnectivityManager - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Android ConnectivityManager class.

This code example shows how to use the following methods:getActiveNetworkInfo
This code example shows how to use the following methods:getActiveNetworkInfo
     * @param context Context 
     * @return Network connection available 
     */ 
    public static boolean checkConnectivity(Context context) {
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        final NetworkInfo activeNetwork = manager.getActiveNetworkInfo();
        if (activeNetwork != null && activeNetwork.isConnected()) {
            return true; 
        } 
         
        return false; 
    } 
} 
8
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getActiveNetworkInfo, getBackgroundDataSetting, getNetworkInfo
 
		// Set connectivity manager object 
		ConnectivityManager myConMan = (ConnectivityManager) AppGlobals
				.getAppContext().getSystemService(Context.CONNECTIVITY_SERVICE);
		// Get connection information 
		NetworkInfo netInfo = myConMan.getActiveNetworkInfo();
 
		// Skip if no connection manager, or background data disabled 
		if (netInfo == null || !myConMan.getBackgroundDataSetting()) {
 
			// Looks like we have airplane mode on or background roaming is not 
			// allowed 
			return false; 
 
			// Else we ware good to check for connectivity 
		} else { 
			// Get 3G connectivity 
			Boolean is3g = myConMan.getNetworkInfo(
					ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
			// Get WiFi connectivity 

8
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getActiveNetworkInfo
 */ 
public class NetworkUtils { 
 
	public static boolean haveNetworkConnection(Context context) {
		ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
		if(activeNetwork == null) return false;
		return activeNetwork.isConnected();
	} 
} 
8
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getNetworkInfo
package edu.vanderbilt.clear.network; 
 
import android.content.Context; 
import android.net.ConnectivityManager; 
import android.net.NetworkInfo; 
 
public class InternetUtility { 
	public static boolean hasInternet(Context myContext) {
		ConnectivityManager connec = (ConnectivityManager) myContext.getSystemService(Context.CONNECTIVITY_SERVICE);
		boolean mobile = connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED;
		boolean wifi = connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED;
		return mobile || wifi;
	} 
} 

8
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getNetworkInfo
 
		EasyJSONObject object = new EasyJSONObject();
 
        if (PackageManager.PERMISSION_GRANTED == context.checkCallingOrSelfPermission(Manifest.permission.ACCESS_NETWORK_STATE)) {
            ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo wifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
             
            object.put("available", wifi.isAvailable());
            object.put("connected", wifi.isConnected());
        } 
		 
		return object;
	} 
 
} 
8
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getBackgroundDataSetting
   * data operations. This logic varies by OS version. 
   */ 
  protected boolean backgroundDataIsEnabled() { 
    ConnectivityManager connectivity = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
      return connectivity.getBackgroundDataSetting();
    } 
    NetworkInfo networkInfo = connectivity.getActiveNetworkInfo();
    if (networkInfo == null) {
      return false; 
    } 
    return networkInfo.isAvailable();
  } 
 
  protected static PendingIntent createPendingIntent(Context context, Class<? extends BroadcastReceiver> broadcastReceiverClass) {
    final Intent service = new Intent(context, broadcastReceiverClass);
    return PendingIntent.getBroadcast(context, 0, service, PendingIntent.FLAG_CANCEL_CURRENT);
  } 
 
  protected AlarmManager getAlarmManager() {
7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getActiveNetworkInfo
    private ShadowNetworkInfo networkInfo;
 
    @Before 
    public void setUp() throws Exception { 
        connectivityManager = (ConnectivityManager) Robolectric.application.getSystemService(Context.CONNECTIVITY_SERVICE);
        networkInfo = Robolectric.shadowOf(connectivityManager.getActiveNetworkInfo());
    } 
 
    @Test 
    public void getConnectivityManagerShouldNotBeNull() { 
        assertNotNull(connectivityManager);
        assertNotNull(connectivityManager.getActiveNetworkInfo());
    } 
 
    @Test 
    public void networkInfoShouldReturnTrueCorrectly() { 
        networkInfo.setConnectionStatus(true);
 
        assertTrue(connectivityManager.getActiveNetworkInfo().isConnectedOrConnecting());
        assertTrue(connectivityManager.getActiveNetworkInfo().isConnected());
9
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getNetworkPreference, setNetworkPreference
        assertEquals(connectivityManager.getNetworkPreference(), ConnectivityManager.DEFAULT_NETWORK_PREFERENCE);
    } 
     
    @Test 
    public void shouldGetSetNetworkPreference() throws Exception { 
        connectivityManager.setNetworkPreference(ConnectivityManager.TYPE_MOBILE);
        assertEquals(connectivityManager.getNetworkPreference(), connectivityManager.getNetworkPreference());
        connectivityManager.setNetworkPreference(ConnectivityManager.TYPE_WIFI);
        assertEquals(connectivityManager.getNetworkPreference(), ConnectivityManager.TYPE_WIFI);
    } 
} 
8
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getActiveNetworkInfo, getBackgroundDataSetting
 
        // Get hold of the ConnectivityManager 
        ConnectivityManager mConnectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
 
        // Get the Network information through the ConectivityManager 
        NetworkInfo info = mConnectivity.getActiveNetworkInfo();
 
        if (info == null || !mConnectivity.getBackgroundDataSetting()) {
            Log.d(TAG, "checkNetworkConnection() - getActiveNetworkInfo() is NULL or getBackgroundDataSetting() returned FALSE");
            return false; 
        } 
 
        int netType = info.getType();
 
        if (netType == ConnectivityManager.TYPE_WIFI || netType == ConnectivityManager.TYPE_MOBILE) {
            Log.d(TAG, "checkNetworkConnection() - ConnectivityManager type is WIFI(1) or MOBILE(0). Type=" + netType);
            return info.isConnected();
        } else { 
            Log.d(TAG, "checkNetworkConnection() - ConnectivityManager type is not WIFI(1) or MOBILE(0). Type=" + netType);
        } 
7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java

Related Android ConnectivityManager Questions & Answers:

Other Android ConnectivityManager Examples

13:	
14:		public boolean isConnectingToInternet(){
15:			ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
16:			  if (connectivity != null) 
17:			  {
-
13:	
14:		public boolean isConnectingToInternet(){
15:			ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
16:			  if (connectivity != null) 
17:			  {

Read More

16:	     * **/
17:	    public boolean isConnectingToInternet(){
18:	        ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
19:	          if (connectivity != null)
20:	          {
-
16:	     * **/
17:	    public boolean isConnectingToInternet(){
18:	        ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
19:	          if (connectivity != null)
20:	          {

Read More

16:	     * **/
17:	    public boolean isConnectingToInternet(){
18:	        ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
19:	          if (connectivity != null)
20:	          {
-
16:	     * **/
17:	    public boolean isConnectingToInternet(){
18:	        ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
19:	          if (connectivity != null)
20:	          {

Read More

See Code Examples for Android ConnectivityManager Methods: