Android onDataConnectionStateChanged – Detect Network Type


Android onDataConnectionStateChanged

While playing with network and phone states in android there could always be a situation where one would like to detect the network type and monitor the changes of data connection state. Both of these situations can be taken care off, with the help of Android PhoneStateListener onDataConnectionStateChanged method. As I mentioned in my previous tutorial that PhoneStateListener class works as an interface along with TelephonyManager class. Although this tutorial is focused on Android onDataConnectionStateChanged if you wish to read in detail about these classes please go through my complete series on PhoneStateListener.

1) Android PhoneStateListener Example
2) Android onDataConnectionStateChanged – Detect Network Type
3) Android onSignalStrengthsChanged – LTE Strength Measurement

Android onDataConnectionStateChanged

This is a method in PhoneStateListener class of Android, which is used to monitor the data connection state changes (only for network not wifi). Although if you want to monitor the changes in overall data connectivity you could go for the  ConnectivityManager class and use ConnectivityManager.CONNECTIVITY_ACTION. Since the connectivity manager can monitor the changes in both wifi and data, obviously its a better option. But at times there are situations when one needs to monitor several network related things and this can be done only through Android PhoneStateListener class. Threfore in such situation one can definitely use Android onDataConnectionStateChanged method to monitor network data changes.

Another situation when this Android onDataConnectionStateChanged method could be used is when, device is connected to the wifi network and you need to detect operator data connection state. To use this method we need to define following permission in the manifest:

Lets define an activity where PhoneStateListener class can be initialized:

In the above class we created an instance of TelephonyManager and used its method listen(PhoneStateListener listener, int events) to listen the specified PhoneStateListener LISTEN_ flags. Now if we closely observe the listen method, we passed new object of our custom PhoneStateListener and specified the PhoneStateListener.LISTEN_DATA_CONNECTION_STATE flag. Meaning that the specified object of PhoneStateListener would get callbacks of onDataConnectionStateChanged(int state, int networkType) method, which would solve our purpose here.

Lets define custom Android PhoneStateListener onDataConnectionStateChanged method:

After running this piece of code, you would observe that whenever there is a change in the of data connection, a callback is received in the Android PhoneStateListener onDataConnectionStateChanged(int state, int networkType) method.

With this method we can monitor two things

  1. Data Connection State (int state) – This parameter would give us the current state of data connection, it could range from TelephonyManager.DATA_DISCONNECTED to TelephonyManager.DATA_SUSPENDED.
  2. Network Type (int networkType) – This parameter would tell the us the type of device’s network connection. At present there are a total of 16 network types and it can range from TelephonyManager.NETWORK_TYPE_1xRTT to TelephonyManager.NETWORK_TYPE_UNKNOWN.

To conclude I would say that with this Android onDataConnectionStateChanged – Detect Network Type tutorial one can monitor changes in the data connection state and cell network type.

About Mohit Gupt

An android enthusiast, and an iPhone user with a keen interest in development of innovative applications.

Leave a comment

Your email address will not be published. Required fields are marked *