Stack Exchange
Toggle mobile data programmatically
I have a method to enable or disable the mobile data
public void toggleMobileData(boolean enabled){
        try {
            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);
        }catch (Exception e){
            e.printStackTrace();
            Toast.makeText(context.getApplicationContext(),context.getResources().getString(R.string.no_change_ntework),Toast.LENGTH_SHORT).show();
        }
    }

It works fine, but i'm trying it with a Samsung S5 and I'm getting this error:
02-13 17:00:59.680    1928-1928/com.app.innergy W/System.err﹕ java.lang.NoSuchMethodException: setMobileDataEnabled [boolean]
02-13 17:00:59.690    1928-1928/com.app.innergy W/System.err﹕ at java.lang.Class.getDeclaredMethod(Class.java:635)
02-13 17:00:59.690    1928-1928/com.app.innergy W/System.err﹕ at com.app.innergy.SystemManager.toggleMobileData(SystemManager.java:122)
02-13 17:00:59.690    1928-1928/com.app.innergy W/System.err﹕ at com.app.innergy.SingleOptionAdapter$2.onClick(SingleOptionAdapter.java:138)
02-13 17:00:59.690    1928-1928/com.app.innergy W/System.err﹕ at android.view.View.performClick(View.java:5162)
02-13 17:00:59.690    1928-1928/com.app.innergy W/System.err﹕ at android.widget.CompoundButton.performClick(CompoundButton.java:128)
02-13 17:00:59.690    1928-1928/com.app.innergy W/System.err﹕ at android.view.View$PerformClick.run(View.java:20873)
02-13 17:00:59.690    1928-1928/com.app.innergy W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:739)
02-13 17:00:59.690    1928-1928/com.app.innergy W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
02-13 17:00:59.690    1928-1928/com.app.innergy W/System.err﹕ at android.os.Looper.loop(Looper.java:145)
02-13 17:00:59.690    1928-1928/com.app.innergy W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5834)
02-13 17:00:59.690    1928-1928/com.app.innergy W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
02-13 17:00:59.690    1928-1928/com.app.innergy W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
02-13 17:00:59.690    1928-1928/com.app.innergy W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
02-13 17:00:59.690    1928-1928/com.app.innergy W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)

EDIT: This is where I call the method (is from an adapter of a custom listview in a fragment, i'm not posting all the code because it would be too long I guess)
//...

if (soa.getTitle().equals(activity.getResources().getString(R.string.mobile_data))) {
                            fileContent = !soa.isToggle() ? activity.getResources().getString(R.string.activated) : activity.getResources().getString(R.string.deactivated);
                            systemManager.toggleMobileData(!soa.isToggle());
                            soa.setToggle(!soa.isToggle());
                            fileManager.createFile();
                            fileManager.writeToFile(activity.getResources().getString(R.string.mobile_data)+": "+ fileContent);
//...


Show the code where you call the method

@Doruko Have you resolved this?

@JaredBurrows No, I haven't... It seems that it won't work in android 5 or above

This question is a duplicate but there is no real answer(You have gotten your code from the same links below). Most of these top answers have successfully turned off Mobile data through reflection:
https://stackoverflow.com/a/12535246/950427
and:
https://stackoverflow.com/a/11555457/950427
Since this is not supported by the Android API, there is no guarantee to make it work on all devices through Reflection.
