2

Below is toggle android mobile data:

    Method dataConnSwitchmethod;
    Class telephonyManagerClass;
    Object ITelephonyStub;
    Class ITelephonyClass;

    TelephonyManager telephonyManager = (TelephonyManager) context
            .getSystemService(Context.TELEPHONY_SERVICE);

    if(telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED){
        isEnabled = true;
    }else{
        isEnabled = false;  
    }   

    telephonyManagerClass = Class.forName(telephonyManager.getClass().getName());
    Method getITelephonyMethod = telephonyManagerClass.getDeclaredMethod("getITelephony");
    getITelephonyMethod.setAccessible(true);
    ITelephonyStub = getITelephonyMethod.invoke(telephonyManager);
    ITelephonyClass = Class.forName(ITelephonyStub.getClass().getName());

    if (isEnabled) {
        dataConnSwitchmethod = ITelephonyClass
                .getDeclaredMethod("disableDataConnectivity");
    } else {
        dataConnSwitchmethod = ITelephonyClass
                .getDeclaredMethod("enableDataConnectivity");   
    }
    dataConnSwitchmethod.setAccessible(true);
    dataConnSwitchmethod.invoke(ITelephonyStub);

source from: How to disable Mobile Data on Android It works until Android L preview version of my nexus 5! Is there any way to toggle mobile data in android L or google just removed api? Thanks in advance.

Community
  • 1
  • 1
  • Toggling data connectivity requires the MODIFY_PHONE_STATE permission which can only be granted to system apps. – alanv Jul 16 '14 at 02:01
  • It works on 4.4, does it need more permission on android L? – user1345817 Jul 17 '14 at 04:28
  • Are you sure you were using this on 4.4? It was probably the answer below what you pasted, the one with ConnectivityManager. What you pasted only worked until 2.2. I too am trying to figure out how to make it work on Android L. – Flyview Aug 23 '14 at 03:56

0 Answers0