I have a requirement in my application wherein my app should always be connected to the internet(2G/3G only).To achieve this one can create a service which keeps checking if the device is connected to the internet or not.If not, then we can programmatically connect to the internet(This being because my app will work in an environment with very less user interaction,so most things should happen automatically). The 'service' approach is fine but consumes considerable battery due to the constant polling,which I want to avoid.
I recently read about a broadcast called -CONNECTIVITY_ACTION
, which the system generates.As per the doc Changes to a device's connectivity can be very frequent—this broadcast is triggered every time you move between mobile data and Wi-Fi. .
In my requirement the device will ONLY be connected to 2G/3G and never to the wifi.So is there a broadcast from the system using which I can get to know the changes in connectivity(ON/OFF) to the 2G/3G alone? Because as per the above quote what I infer is that CONNECTIVITY_ACTION would be fired only when the state changes from mobile data(2g/3g) to wifi. Is it so?(Please correct me if I am wrong).
Also,please enlighten me with the most efficient approach that I should use to check that my device is connected to the internet(2g/3g) constantly(other than a service based approach),so that I can then take action to connect to the internet automatically? Thanks in advance !
The mentioned broadcast is sent when disconnected completely. It's even mentioned in the docs
Just listen to
CONNECTIVITY_ACTION
and do whatever is needed to reconnect your device.Complete doc: https://developer.android.com/reference/android/net/ConnectivityManager.html#CONNECTIVITY_ACTION