ConnectivityManager getActiveNetworkInfo - top ranked examples from Open Source projects

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

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
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
This code example shows how to use the following methods:getActiveNetworkInfo
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
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
This code example shows how to use the following methods:getActiveNetworkInfo
This code example shows how to use the following methods:getActiveNetworkInfo
 
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");
9
CodeRank
This code example shows how to use the following methods:getActiveNetworkInfo
 
        return downLoadOk(networkConfig, whenRoaming);
    } 
 
    private boolean downLoadOk(String networkConfig, boolean whenRoaming) {
        NetworkInfo info = cManager.getActiveNetworkInfo();
        if (info==null) {
            Log.i("NetworkHelper", "Can't get info about active network, blocking down load");
            return false; 
        } 
        int type = info.getType();
        int subType = info.getSubtype();
 
        int configType;
        try { 
            configType = Integer.parseInt(networkConfig);
        } catch (NumberFormatException e) {
            Log.w("NetworkHelper",e.getMessage());
            configType = 6;
        } 
9
CodeRank
This code example shows how to use the following methods:getActiveNetworkInfo
		mNotifMan.notify(NOTIF_CONNECTED, n);
	} 
	 
	// Check if we are online 
	private boolean isNetworkAvailable() { 
		NetworkInfo info = mConnMan.getActiveNetworkInfo();
		if (info == null) {
			return false; 
		} 
		return info.isConnected();
	} 
	 
	// This inner class is a wrapper on top of MQTT client. 
	private class MQTTConnection implements MqttSimpleCallback { 
		IMqttClient mqttClient = null;
		 
		// Creates a new connection given the broker address and initial topic 
		public MQTTConnection(String brokerHostName, String initTopic) throws MqttException {
			// Create connection spec 
	    	String mqttConnSpec = "tcp://" + brokerHostName + "@" + MQTT_BROKER_PORT_NUM;
9
CodeRank

Related ConnectivityManager getActiveNetworkInfo Questions & Answers:

getActiveNetworkInfo Method Overview
Returns details about the currently active default data network.
See Code Examples for other Android ConnectivityManager Methods: