dave.c
dave.c

Reputation: 10908

How to respect network use settings in Android

My app performs some backgound data collection and I'm adding support for the user network preferences, such as performing background updates and data roaming. I have the following checks already:

ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if(cm.getBackgroundDataSetting()) {
...
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected()) {

with the required entries in the manifest:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>

This all appears to be working fine, but I was wondering if I should be checking anything else? I was worried about checking for data roaming but the docs state that networkInfo.isAvailable() checks this for me. So are there any other checks I need to implement for network settings? Anything else in this area I should be aware of?

Upvotes: 11

Views: 3022

Answers (3)

Heiko Rupp
Heiko Rupp

Reputation: 30994

Like @inazaruk, I a also trying to prevent (for the user possibly expensive) network transfers when roaming or downloading images while on GPRS only. Therefore in Zwitscher I've implemented a NetworkHelper that checks against users preferences on romaing and minimal network state; https://github.com/pilhuhn/ZwitscherA/blob/master/src/de/bsd/zwitscher/helper/NetworkHelper.java The matching preferences are here: https://github.com/pilhuhn/ZwitscherA/blob/master/res/xml/preferences.xml#L30

Upvotes: 2

inazaruk
inazaruk

Reputation: 74790

Additionally to your checks I also do check for roaming state for 3g network:

NetworkInfo info = m_connectivityManager.getActiveNetworkInfo();
int netType = info.getType();
int netSubtype = info.getSubtype();

if (netType == ConnectivityManager.TYPE_WIFI || netType == ConnectivityManager.TYPE_WIMAX)  
{
   //no restrictions, do some networking
}
else if (netType == ConnectivityManager.TYPE_MOBILE && 
     netSubtype == TelephonyManager.NETWORK_TYPE_UMTS)
{
   //3G connection
   if(!m_telephonyManager.isNetworkRoaming())
   {
      //do some networking
   }      
}

My application uses a lot of data, so I do not allow data downloading on non-3g mobile networks.

Upvotes: 6

levis501
levis501

Reputation: 4207

The user may change the settings while your background app is running. The API recommends that you listen to the broadcast message:

ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED 

Perhaps you are checking cm.getBackgroundDataSetting() prior to sending data, and I suspect this would be sufficient. However, listening to the broadcast message will let you resume sending background data when the settings are changed.

I believe that either listening to the broadcast message or checking the settings before sending data will suffice. Android docs recommends the former.

Upvotes: 6

Related Questions