Cómo respetar la configuración de uso de la red en 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 documentos Establece eso, afirma eso 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?

preguntado el 08 de enero de 11 a las 15:01

Good question. Does network info also not have a isRoaming method. (And are you developing app to only North America area, if not this could be a problem, since the network protocol could be different). -

Si, hay un isRoaming() method. I've not used it so far as networkInfo.isAvailable() appears to return false if the network is roaming and the user has disabled Data Roaming. The app will be UK-only - I don't pensar this has an effect on the code I have so far, but if I were to implement some of the checks in the answers I would have to check a different NETWORK_TYPE. -

I was curious. I browsed thro the source code and documentation. isNetworkAvilable is reliable as any, since it checks if you are in home network or not. ( after all if it fails, your application is not the only one in trouble ;)) -

3 Respuestas

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. Documentos de Android recommends the former.

Respondido el 24 de enero de 11 a las 09:01

I have a class that extends BroadcastReceiver to perform the downloads. I then set an alarm using the AlarmManager to trigger a background download. Each time the receiver is triggered it performs the checks in my post. I suppose I could also listen to the broadcast message you suggest so that if background updates have been off and have just been turned back on I can trigger an immediate download. Or I could just wait for the next scheduled download to occur. I guess I have to make a design decision... thanks for the post. - dave.c

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.

Respondido el 26 de enero de 11 a las 02:01

Interesting approach, thanks for the post. Wouldn't !m_telephonyManager.isNetworkRoaming() prevent a download even if the user has ticked the box to allow Data Roaming? - dave.c

If you haven't ticked 'Data roaming' in settings, no one will be able to use mobile networks while in roaming. But in my case I needed to prevent heavy network usage even if roaming is enabled ( for 'light' requests I didn't do this roaming check ). So, yes, this will disable application's network activity even if device-wise roaming is enabled. - inazaruk

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

Respondido el 27 de enero de 11 a las 09:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.