6

I am developing an app, and I'm concerned as to the amount of data users can transfer among themselves. Since some users have limited mobile data plans and some don't, I would like to know if I could develop a switch to disable mobile data for my specific app. A bit like what Android's own Data usage -> mobile -> App ->"Restrict background data" does...

it says (and does) that it "disables background data on mobile data network only. Wi-Fi will be used if available.", I want that but not just on background.

I do know that I can't change the "Restrict background data" option, as it would make it useless if applications could untoggle it ... but is there a way i can programmatically say now my app is blocked access to mobile data, now it isn't?

as insight for why I want this, I am trying to not call remote access calls when the user selects a given option(which I did and works), but there are some data leakage (some kbs per day) that I cant seem to block ... either due to the system keeping http connections or some other obscure reason...

I'm seeing these data transfers on DDMS perspective on eclipse on the network statistics for my app.

Thanks for any help

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Marc
  • 838
  • 7
  • 11

2 Answers2

1

Instead of disabling, it will be good to check if active connection is wifi or not, if not wifi then don't do any server side communication.

check below thread for example.

How do I see if Wi-Fi is connected on Android?

ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

if (mWifi.isConnected()) {
    // Do whatever
}

remember to add

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

to your AndroidManifest.manifest for this to work.

Community
  • 1
  • 1
Rajen Raiyarela
  • 5,526
  • 4
  • 21
  • 41
  • Thanks, I already do something like that, when Wifi is connected or mobile data is connected and not on the restricted mode, I do not communicate with the server, I consider this to be the "not connected to the internet state", This reduced the data usage from possibly several Megabytes (depending of the usage) to several Kilobytes per day ... but there are still these kb that are bothering me ... as I can't call this switch a "Restrict mobile data" or "Wi-Fi only" mode. I was hopping that there might be a way to force only my app (that runs in the background too) to not use any mobile data. – Marc Jun 26 '14 at 14:18
  • Are you using any third party library in your project which requires internet then it will be difficult to restrict them. but if from your application you use internet, then definitely this should work as when device connected to network and if the network is not wifi, consider it as no connectivity. – Rajen Raiyarela Jun 26 '14 at 14:25
  • okay, but they won't be working by there on like advertising sdks work. As per my knowledge only by checking network type wifi or not your requirement can be achieved of whether you use data or not. – Rajen Raiyarela Jun 26 '14 at 14:40
0

Below code for disable or enable moblie data :-

java code

private void setMobileDataEnabled(Context context, boolean enabled) {
    final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    final Class conmanClass = Class.forName(conman.getClass().getName());
    final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
    iConnectivityManagerField.setAccessible(true);
    final Object iConnectivityManager = iConnectivityManagerField.get(conman);
    final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
    final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
    setMobileDataEnabledMethod.setAccessible(true);

    setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);
}

manifest

<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
duggu
  • 37,851
  • 12
  • 116
  • 113
  • very interesting, thanks! however this disables/enables the mobile data of the device for all apps ... and isn't targeted only at my app, as I would want to do... – Marc Jun 26 '14 at 13:46
  • 2
    @Marc when you close app then you enable it simple (it close for all application). – duggu Jun 26 '14 at 13:51
  • 1
    My app also runs on the background, and either way I wouldn't want to impose to the user no data connectivity while my app is active :( – Marc Jun 26 '14 at 14:19