Export to GitHub

android-developer-preview - issue #2103

Replacement for setMobileDataEnabled() api


Posted on May 29, 2015 by Happy Monkey

Please provide the following: * Which version of the SDK are you using? N/A * Which Android build are you using? (e.g. MPZ44Q) N/A * What is the desired behavior of the feature? (Be specific!) N/A * If relevant, why are current approaches or workarounds insufficient? NONE or root the device * If relevant, what new use cases will this feature will enable?

Everything pre lollipop. Developers were able to programmatically turn the mobile data on and off, maybe through a scheduler type app or through a widget on the home screen. This is no longer achievable as Google has remove the setMobileDataEnable() via reflection. I would want this back in the new SDK (and developers would love this back)

I have not tested this on the new build of M (are you on API Level 23M?) but please create an API for this, which can be managed via the new permission model for apps.

Defect for this has been raised on the main issue tracker: https://code.google.com/p/android/issues/detail?id=130608 https://code.google.com/p/android/issues/detail?id=81891

Comment #1

Posted on May 29, 2015 by Quick Monkey

I 'm looking forward to see this features on Android again so I can use Juice Defender to manage my data connections.

Comment #2

Posted on May 29, 2015 by Happy Wombat

Make sure you start this issue Google doesn't look at anything but stars

Comment #3

Posted on May 31, 2015 by Quick Kangaroo

Add my voice there !

It would be nice to have these features back. As well as gps toogle, reading log, battery stats...

All these useful apps now asking root acces...

Maybe an option to activate in dev setting, and having to explicitly allow it on a per app basis.

Denying permission is good enough, but what I really want is to be able to tell tasker "yes please, you can toogle the gps for me".

Comment #4

Posted on Jun 1, 2015 by Quick Elephant

@3 That's right. So many missing permissions to toggle stuff, that we need root for everything. This also includes airplane mode. Even accessing the SD card has become a huge annoyance (DocumentFile instead of normal File).

At least put all those toggles handling in a confirmation dialog like the admin one.

Comment #5

Posted on Jun 2, 2015 by Happy Wombat

Seems like Google has a split personality. On one hand they keep making it harder to root your phone and on the other hand they remove permissions that force you to root your phone. Dam Google play with you cars and drones and leave my f...ing phone alone.

On Mon, Jun 1, 2015 at 4:27 PM, wrote:

Comment #6

Posted on Jun 4, 2015 by Swift Elephant

Thank you for this suggestion. We will pass this along to the development team and provide updates as they are available.

Comment #7

Posted on Jun 4, 2015 by Happy Monkey

6 - Appreciated that this has been acknowledge. Thanks and hope this is introduced back in Android M release.

Comment #8

Posted on Jun 4, 2015 by Quick Rhino

This is essential for having complete control over everything. I sided with Android for a reason.

Comment #9

Posted on Jun 5, 2015 by Quick Kangaroo

So glad to see this issue acknowledged! Come on Google, show us why you are the best option!

Comment #10

Posted on Jun 5, 2015 by Massive Rhino

Please please please do something about it. This issue alone was enough for me to stick with Kitkat because it messes up my automation profiles and I would LOVE to upgrade to android M.

With the new permission system I think Google is on the right track to give power users what they want without compromising security. Everyone would be happy if we could get some kind of permission for data toggle from apps.

Keep up the good work by NOT restricting the OS like Apple does (that's what makes it different after all).

Comment #11

Posted on Jun 5, 2015 by Helpful Kangaroo

Would so appreciate this feature back ...

I use Nights Keeper to keep my phone quiet overnight but allow texts and calls from two disabled family members ... now I have to remember to turn off the mobile data so that anything internet related doesn't get through - and then turn it on again in the morning!

I also use Tasker to do similar things in meetings but this also no longer works with the mobile data as well.

Thanks

Comment #12

Posted on Jun 5, 2015 by Grumpy Cat

Comment deleted

Comment #13

Posted on Jun 5, 2015 by Quick Elephant

Even for airplane mode, it became an issue to be toggled, and it requires root. I use "Condi" to toggle it (with root) at night in order to preserve battery and avoid anything to interrupt my sleep.

Google, please, for all permissions that toggle problematic stuff (and I do agree they are problematic), add some sort of permissions confirmation instead of allowing it only for rooted devices.

Comment #14

Posted on Jun 9, 2015 by Happy Bear

setMobileDataEnable() is crucial for a lot of applications. Android developers are very angry with the fact that this function disappeared in Lollipop (Api 20 and later).

Comment #15

Posted on Jun 16, 2015 by Swift Rabbit

This is of great importance to a lot of people!!!

Comment #16

Posted on Jun 21, 2015 by Massive Ox

With many phone companies world wide charging large amounts for data this is a key requirement. please reinstate it.

Comment #17

Posted on Jun 25, 2015 by Quick Bear

Impossible to write applications that allow users to save money when mobile data cannot be always on. Please Goggle, give the function back and many developers will be happy again.

Comment #18

Posted on Jul 10, 2015 by Quick Hippo

setMobileDataEnabled() should be protected with a permission toggle even if Google want's to re-enable this function.

It is similar with WiFi/Bluetooth

Comment #19

Posted on Jul 12, 2015 by Happy Rhino

I have left Stackoverflow due to retarded Stackoverlords. So, I'll post the solution here to turn off mobile network function on Android 5+ rooted devices for everyone to use:

The proper way to switch off mobile network, including its corresponding subscription service via the SubscriptionManager class introduced in API 22, is:

public static void setMobileNetworkfromLollipop(Context context) throws Exception { String command = null; int state = 0; try { // Get the current state of the mobile network. state = isMobileDataEnabledFromLollipop(context) ? 0 : 1; // Get the value of the "TRANSACTION_setDataEnabled" field. String transactionCode = getTransactionCode(context); // Android 5.1+ (API 22) and later. if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) { SubscriptionManager mSubscriptionManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE); // Loop through the subscription list i.e. SIM list. for (int i = 0; i < mSubscriptionManager.getActiveSubscriptionInfoCountMax(); i++) {
if (transactionCode != null && transactionCode.length() > 0) { // Get the active subscription ID for a given SIM card. int subscriptionId = mSubscriptionManager.getActiveSubscriptionInfoList().get(i).getSubscriptionId(); // Execute the command via su to turn off // mobile network for a subscription service. command = "service call phone " + transactionCode + " i32 " + subscriptionId + " i32 " + state; executeCommandViaSu(context, "-c", command); } } } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) { // Android 5.0 (API 21) only. if (transactionCode != null && transactionCode.length() > 0) { // Execute the command via su to turn off mobile network.
command = "service call phone " + transactionCode + " i32 " + state; executeCommandViaSu(context, "-c", command); } } } catch(Exception e) { // Oops! Something went wrong, so we throw the exception here. throw e; }
}

To check if the mobile network is enabled or not:

private static boolean isMobileDataEnabledFromLollipop(Context context) { boolean state = false; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { state = Settings.Global.getInt(context.getContentResolver(), "mobile_data", 0) == 1; } return state; }

To get the value of the TRANSACTION_setDataEnabled field:

private static String getTransactionCode(Context context) throws Exception { try { final TelephonyManager mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); final Class mTelephonyClass = Class.forName(mTelephonyManager.getClass().getName()); final Method mTelephonyMethod = mTelephonyClass.getDeclaredMethod("getITelephony"); mTelephonyMethod.setAccessible(true); final Object mTelephonyStub = mTelephonyMethod.invoke(mTelephonyManager); final Class mTelephonyStubClass = Class.forName(mTelephonyStub.getClass().getName()); final Class mClass = mTelephonyStubClass.getDeclaringClass(); final Field field = mClass.getDeclaredField("TRANSACTION_setDataEnabled"); field.setAccessible(true); return String.valueOf(field.getInt(null)); } catch (Exception e) { // The "TRANSACTION_setDataEnabled" field is not available, // or named differently in the current API level, so we throw // an exception and inform users that the method is not available. throw e; } }

To execute command via su:

private static void executeCommandViaSu(Context context, String option, String command) { boolean success = false; String su = "su"; for (int i=0; i < 3; i++) { // Default "su" command executed successfully, then quit. if (success) { break; } // Else, execute other "su" commands. if (i == 1) { su = "/system/xbin/su"; } else if (i == 2) { su = "/system/bin/su"; }
try { // Execute command as "su". Runtime.getRuntime().exec(new String[]{su, option, command}); } catch (IOException e) { success = false; // Oops! Cannot execute su for some reason. // Log error here. } finally { success = true; } } }

Comment #20

Posted on Jul 12, 2015 by Quick Elephant

@19 Where were they rude for you for showing a good solution? I will put this for you if you don't mind. This can be useful for a lot of people.

Comment #21

Posted on Jul 12, 2015 by Happy Rhino

@20: I left because the Stackoverlords accused me of colluding with others to get 370 pathetic reputation points. The irony: I don't even give a s**t about reputation points. I joined Stackoverflow in the hope of helping other programmers with what I know- and get help myself on programming topics I don't know. I neither care for Stackoverflow's gamification system, badges, points, or otherwise. I guess the Stackoverlords don't understand that many joined Stackoverflow to help others and not interested in prestige or vanity.

Anyway, you can still see the reason for my leaving in my Stackoverflow profile here (http://stackoverflow.com/users/463053/chuongpham) and my original post regarding the issue with switching off mobile network function on Android 5+ devices (http://stackoverflow.com/questions/26539445/the-setmobiledataenabled-method-is-no-longer-callable-as-of-android-l-and-later).

There's no copyright in my solution, distribute it as you please. The more Android programmers can be helped with it the better, while we wait for Google to get their act together and provide us with an official API to do it.

Comment #22

Posted on Jul 12, 2015 by Quick Monkey

How sad that this has attracted rudeness! If "thinking outside the box" means "root your phone" I concur that the vast majority will reject it. This is not a forum for workarounds. It is for REQUESTING changes. Let's do it politely?

Comment #23

Posted on Jul 12, 2015 by Massive Giraffe

@22: "If thinking outside the box means root your phone..."- Your comment is nothing more than a rant. What does thinking outside the box got anything to do with rooting? Do you even understand the meaning of "outside the box" and "root"?

A solution provided is better than no solution. I don't see you posting a solution but whine instead!

Oh, and we ALL know what an Issue Tracker does...

Comment #24

Posted on Jul 12, 2015 by Quick Elephant

You guys, please relax. I was just happy to see a solution, even if it means it's only for rooted devices. Of course it would be better to see an official solution.

Comment #25

Posted on Jul 12, 2015 by Massive Rhino

Thanks for posting it, but we do actually want Google to provide a possiblity for non rooted phones ...

Comment #26

Posted on Jul 12, 2015 by Happy Rhino

I think Google should provide an official mean to switch off mobile network for non-rooted phones. I'm a programmer as well as an Android user. I have limited data plans on my telecom account with my provider, and assume for a minute I don't how to root my Android device, I would exceed my data plans easily for any given month due to my 3G/4G being constantly on. So, having the ability to completely switch off mobile network and use alternative WiFi connection when possible is a plus. This is the dilemma every Android users with a Lollipop devices faced at the moment. The sooner Google provides an official method for programmers to use, the sooner we can help the average Joe to save some money.

Comment #27

Posted on Jul 12, 2015 by Happy Wombat

Maybe Google has no interest in saving people money. It would be interesting to know who made the decision to close the back door to Mobile Data control and why? To continue that line of thought if they didn't like that people were using a back door approach to Mobile Data control why didn't they just give us a front door with any safeguards they thought were needed. The answer probably has something to do with arrogance or the bottom line or both.

On Sun, Jul 12, 2015 at 5:49 AM, wrote:

Comment #28

Posted on Jul 15, 2015 by Helpful Cat

I really don't understand their bottom-line mentality- while they might make some money from licensing Android to handset manufacturers, but that money is small compared to the percentage they got from each sale made in Google Play. If I run Google, I would fire the current CFO and the ones making the decision to keep mobile network on.

Google may also suffer from the "tall poppy" syndrome, too, judging by the number of arrogant decisions they have made in the past.

Comment #29

Posted on Oct 21, 2015 by Happy Kangaroo

Hey there -- Android Marshmallow has been released publicly and our M Developer Preview has ended. If you would like to provide feedback on public releases of Android, please file a bug at https://code.google.com/p/android/issues/list. Thanks for helping to make Android better by participating in our preview!

Comment #30

Posted on Nov 2, 2015 by Happy Dog

Please add this back in, or I predict the same issue Apple is having coming Google's way...http://www.cnet.com/news/apple-faces-lawsuit-over-data-bills-tied-to-wi-fi-assist/

Status: PreviousRelease

Labels:
Type-Feature Release-M Feature-19233605