ziggestardust
ziggestardust

Reputation: 211

connectivitymanager - android.net.conn.CONNECTIVITY_CHANGE

I have created an filter for android.net.conn.CONNECTIVITY_CHANGE.
I do receive intents to my broadcastreceiver.

My question is regarding what raises the connectivity_change. The API description says:

A change in network connectivity has occurred. A connection has either been established or lost. The NetworkInfo for the affected network is sent as an extra; it should be consulted to see what kind of connectivity event occurred.

It seems my broadcastreceiver is only called when mobile network is connected/disconnected (pdp is broken). It is not raised when for example 2g switches to 3g.

Can I not catch 2g to 3g swaps with this broadcastreceiver?
Do I have to use a phonestatelistener instead to catch swaps from e.g, 2g to 3g?

Upvotes: 3

Views: 7144

Answers (1)

John
John

Reputation: 56

You need:

permission "android.permission.READ_PHONE_STATE" //to get connection changes (2G/3G/etc) permission "android.permission.ACCESS_COARSE_LOCATION" //to get Cell/Tower changes

and

//Make the listener
listener = new PhoneStateListener() { 
    public void onDataConnectionStateChanged(int state, int networkType) 
    { 
       //We have changed proticols, for example we have gone from HSDPA to GPRS
       //HSDPA is an example of a 3G connection 
       //GPRS is an example of a 2G connection
    }
    public void onCellLocationChanged(CellLocation location) {
       //We have changed to a different Tower/Cell
    }
};

//Add the listener made above into the telephonyManager
telephonyManager.listen(listener, 
        PhoneStateListener.LISTEN_DATA_CONNECTION_STATE //connection changes 2G/3G/etc
        | PhoneStateListener.LISTEN_CELL_LOCATION       //or tower/cell changes 
); 

Upvotes: 4

Related Questions