8

I need to get the network from which device was disconnected.

Now I use:

NetworkInfo ni =intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);

And check:

ni.isConnected()

if this returns false ni - is the network from which the device was disconnected.

But ConnectivityManager.EXTRA_NETWORK_INFO is deprecated in API 14. Google says use getActiveNetworkInfo() to get network information. But getActiveNetworkInfo() always returns network with which the device is connected now (isConnected() must return true)!

How do I get the network info for the network the device disconnected from without using ConnectivityManager.EXTRA_NETWORK_INFO?

Sertorio Noronha, when I use getActiveNetworkInfo() I only get the network to which I am connected now! But I need to get the network from which I was disconnected.

ConnectivityManager cm = (ConnectivityManager)
    context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo n1 = cm.getActiveNetworkInfo();
Log.d("tets", String.format("%s: %s", n1.getTypeName(), n1.isConnected()));

When I disconnect from WI-FI and connect to 3G in log:

mobile: true
mobile: true

When I disconnect from 3G and connect to WI-FI in log:

WIFI: true
WIFI: true
WIFI: true

getActiveNetworkInfo returns only the network connected to now, but does not return the network from which I was disconnected.

If I use deprecated intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO) in log I see:

When I disconnect from WI-FI and connect to 3G:

WIFI: false
mobile: true

When I disconnect from 3G and connect to WI-FI:

mobile: false
WIFI: true

But I do not want to use deprecated api. How to use modern api to get network from which I was disconnected?

3 Answers 3

11

You can use the following

ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
int networkType = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_TYPE);
boolean isWiFi = networkType == ConnectivityManager.TYPE_WIFI;
boolean isMobile = networkType == ConnectivityManager.TYPE_MOBILE;
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(networkType);
boolean isConnected = networkInfo.isConnected();

if (isWiFi) {
    if (isConnected) {
        Log.i("APP_TAG", "Wi-Fi - CONNECTED");
    } else {
        Log.i("APP_TAG", "Wi-Fi - DISCONNECTED");
    }
} else if (isMobile) {
    if (isConnected) {
        Log.i("APP_TAG", "Mobile - CONNECTED");
    } else {
        Log.i("APP_TAG", "Mobile - DISCONNECTED");
    }
} else {
    if (isConnected) {
        Log.i("APP_TAG", networkInfo.getTypeName() + " - CONNECTED");
    } else {
        Log.i("APP_TAG", networkInfo.getTypeName() + " - DISCONNECTED");
    }
}
1
  • use extras.getInt for EXTRA_NETWORK_TYPE also this field was introduced in API 17 Nov 25, 2014 at 7:47
6

You can get the instance of NetworkInfo through the Context.

ConnectivityManager connectivityManager = ((ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE));
NetworkInfo currentNetworkInfo = connectivityManager.getActiveNetworkInfo();

if(currentNetworkInfo != null && currentNetworkInfo.isConnected()){
   // Your logic goes in here
}
4
  • 1
    Please use getTypeName() from the class NetworkInfo. developer.android.com/reference/android/net/NetworkInfo.html Feb 25, 2013 at 12:36
  • Your response does not answer my question! Look at topic header, I provide additional info.
    – Nik
    Feb 27, 2013 at 9:11
  • A very easy solution will be to store the previous value in a local String. Your problem can be solved with simple logic. Feb 27, 2013 at 9:44
  • this was the solution for me, since i needed api level 14-16 too
    – keybee
    Jan 4, 2015 at 6:41
6

Working non deprecated code

/**Receiver*/   

 public class NetworkStateReceiver extends BroadcastReceiver {

        /*
         * @see android.content.BroadcastReceiver#onReceive(android.content.Context,
         * android.content.Intent)
         */
        @Override
        public void onReceive(Context context, Intent intent) {
            ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
            int networkType = intent.getExtras().getInt(ConnectivityManager.EXTRA_NETWORK_TYPE);
            boolean isWiFi = networkType == ConnectivityManager.TYPE_WIFI;
            boolean isMobile = networkType == ConnectivityManager.TYPE_MOBILE;
            NetworkInfo networkInfo = connectivityManager.getNetworkInfo(networkType);
            boolean isConnected = networkInfo.isConnected();

            if (isWiFi) {
                if (isConnected) {
                    Log.i("APP_TAG", "Wi-Fi - CONNECTED");
                } else {
                    Log.i("APP_TAG", "Wi-Fi - DISCONNECTED");
                }
            } else if (isMobile) {
                if (isConnected) {
                    Log.i("APP_TAG", "Mobile - CONNECTED");
                } else {
                    Log.i("APP_TAG", "Mobile - DISCONNECTED");
                }
            } else {
                if (isConnected) {
                    Log.i("APP_TAG", networkInfo.getTypeName() + " - CONNECTED");
                } else {
                    Log.i("APP_TAG", networkInfo.getTypeName() + " - DISCONNECTED");
                }
            }
        }

    }

And in manifest

<!-- Receiver registration in manifest -->
     <receiver android:name="com.xxx.yyy.NetworkStateReceiver" >
                <intent-filter>
                    <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                </intent-filter>
            </receiver>

and

<!-- Internet permission for network comunication -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
4
  • 2
    android.net.conn.CONNECTIVITY_CHANGE is deprecated in Android N - FYI
    – behelit
    Jul 28, 2016 at 3:58
  • 1
    Thanks for comment this has been written ver long days back will update as per new Android N soon
    – Vinayak
    Jul 28, 2016 at 12:40
  • @behelit I thought CONNECTIVITY_ACTION was depcrecated in Android N ? developer.android.com/preview/behavior-changes.html
    – RmK
    Aug 5, 2016 at 20:04
  • Its not deprecated, it will only trigger apps that are foreground and listening. If your app registered for this event and is stopped or in background it wont be notified.
    – Chapz
    Aug 24, 2016 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.