Marc
Marc

Reputation: 838

How to disable mobile data programmatically for my app only

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

Upvotes: 6

Views: 9598

Answers (2)

Rajen Raiyarela
Rajen Raiyarela

Reputation: 5636

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.

Upvotes: 1

duggu
duggu

Reputation: 38429

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"/>

Upvotes: 0

Related Questions