I am trying to toggle android data connection programatically in android lollipop and above but it doesn't work and got exception always.
This is my code
public void setMobileDataState(boolean mobileDataEnabled)
{
   try
   {
       TelephonyManager telephonyService = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
       Method setMobileDataEnabledMethod = telephonyService.getClass().getDeclaredMethod("setDataEnabled", boolean.class);
       if (null != setMobileDataEnabledMethod)
       {
           setMobileDataEnabledMethod.invoke(telephonyService, mobileDataEnabled);
       }
   }
   catch (Exception ex)
   {
        Log.e(TAG, "Error setting mobile data state", ex);
   }
}
 
     
    