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:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
Lets define an activity where PhoneStateListener class can be initialized:
package com.truiton.phonestatelistener; import android.support.v7.app.ActionBarActivity; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class PhoneStateListenerActivity extends ActionBarActivity { TelephonyManager tManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_phone_state_listener); tManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); tManager.listen(new CustomPhoneStateListener(this), PhoneStateListener.LISTEN_DATA_CONNECTION_STATE); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.phone_state_listener, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
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:
package com.truiton.phonestatelistener; import android.content.Context; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.util.Log; public class CustomPhoneStateListener extends PhoneStateListener { Context mContext; public static String LOG_TAG = "CustomPhoneStateListener"; public CustomPhoneStateListener(Context context) { mContext = context; } @Override public void onDataConnectionStateChanged(int state, int networkType) { super.onDataConnectionStateChanged(state, networkType); switch (state) { case TelephonyManager.DATA_DISCONNECTED: Log.i(LOG_TAG, "onDataConnectionStateChanged: DATA_DISCONNECTED"); break; case TelephonyManager.DATA_CONNECTING: Log.i(LOG_TAG, "onDataConnectionStateChanged: DATA_CONNECTING"); break; case TelephonyManager.DATA_CONNECTED: Log.i(LOG_TAG, "onDataConnectionStateChanged: DATA_CONNECTED"); break; case TelephonyManager.DATA_SUSPENDED: Log.i(LOG_TAG, "onDataConnectionStateChanged: DATA_SUSPENDED"); break; default: Log.w(LOG_TAG, "onDataConnectionStateChanged: UNKNOWN " + state); break; } switch (networkType) { case TelephonyManager.NETWORK_TYPE_CDMA: Log.i(LOG_TAG, "onDataConnectionStateChanged: NETWORK_TYPE_CDMA"); break; case TelephonyManager.NETWORK_TYPE_EDGE: Log.i(LOG_TAG, "onDataConnectionStateChanged: NETWORK_TYPE_EDGE"); break; case TelephonyManager.NETWORK_TYPE_EVDO_0: Log.i(LOG_TAG, "onDataConnectionStateChanged: NETWORK_TYPE_EVDO_0"); break; case TelephonyManager.NETWORK_TYPE_GPRS: Log.i(LOG_TAG, "onDataConnectionStateChanged: NETWORK_TYPE_GPRS"); break; case TelephonyManager.NETWORK_TYPE_HSDPA: Log.i(LOG_TAG, "onDataConnectionStateChanged: NETWORK_TYPE_HSDPA"); break; case TelephonyManager.NETWORK_TYPE_HSPA: Log.i(LOG_TAG, "onDataConnectionStateChanged: NETWORK_TYPE_HSPA"); break; case TelephonyManager.NETWORK_TYPE_IDEN: Log.i(LOG_TAG, "onDataConnectionStateChanged: NETWORK_TYPE_IDEN"); break; case TelephonyManager.NETWORK_TYPE_LTE: Log.i(LOG_TAG, "onDataConnectionStateChanged: NETWORK_TYPE_LTE"); break; case TelephonyManager.NETWORK_TYPE_UMTS: Log.i(LOG_TAG, "onDataConnectionStateChanged: NETWORK_TYPE_UMTS"); break; case TelephonyManager.NETWORK_TYPE_UNKNOWN: Log.i(LOG_TAG, "onDataConnectionStateChanged: NETWORK_TYPE_UNKNOWN"); break; default: Log.w(LOG_TAG, "onDataConnectionStateChanged: Undefined Network: " + networkType); break; } } }
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
- Data Connection State (
int state
) – This parameter would give us the current state of data connection, it could range fromTelephonyManager.DATA_DISCONNECTED
toTelephonyManager.DATA_SUSPENDED
. - 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 fromTelephonyManager.NETWORK_TYPE_1xRTT
toTelephonyManager.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.
Born in New Delhi, India. A software engineer by profession, an android enthusiast and an evangelist. My motive here is to create a group of skilled developers, who can develop something new and good. Reason being programming is my passion, and also it feels good to make a device do something you want. In a very short span of time professionally I have worked with many tech firms. As of now too, I am employed as a senior engineer in a leading tech company. In total I may have worked on more than 20 projects professionally, and whenever I get spare time I share my thoughts here at Truiton.