Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 772
Marc Avatar asked Jun 26 '14 13:06

Marc


People also ask

Can you block an app from using mobile data?

In the Android Mobile network settings, tap on Data usage. Next, tap on Network access. Now you see a list of all your installed apps and checkmarks for their access to mobile data and Wi-Fi. To block an app from accessing the internet, uncheck both boxes next to its name.

What is enable and disable mobile data in android programmatically?

Android App Development for Beginners For your find Information, unless you have a rooted phone I don't think you can enable and disable data programmatically because in order to do so we have to include MODIFY_PHONE_STATE permission which is only given to system or signature apps.


1 Answers

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.

like image 76
Rajen Raiyarela Avatar answered Sep 30 '22 21:09

Rajen Raiyarela