Search code examples
android3gandroid-networkingtelephonymanager

How to monitor network type and get notified if it changes


I'm trying to make an app to both show the state of the network type (2G, 3G, or 4G), which I have done by using TelephonyManager, and also notify the user whenever the network type changes. This is the part I have problem in, how can I monitor the network type and get notified whenever it changes?


Solution

  • After a while here is a working code:

    under onCreate :

        TelephonyManager TelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        TelephonyMgr.listen(new TeleListener(),
                PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
    

    and as a class :

    public class TeleListener extends PhoneStateListener {  
        public void onDataConnectionStateChanged (int state, int networkType){
            super.onDataConnectionStateChanged(state, networkType);  
       //Whatever you wanna do  
          }  
    }
    

    the method onDataConnectionStateChanged(int state, int networkType) gets triggered when either networkstype(2G,3G,4G) changes or the connection state (Connecting, disconnecting etc) changes