public
void
switchMobileData(
boolean
on)
throws
Exception{
try
{
Method dataConnSwitchMethod;
Class telephonyManagerClass;
Object ITelephonyStub;
Class ITelephonyClass;
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
switch
(telephonyManager.getDataState()){
case
TelephonyManager.DATA_CONNECTED:
System.out.println(
"DATA_CONNECTED"
);
break
;
case
TelephonyManager.DATA_DISCONNECTED:
System.out.println(
"DATA_DISCONNECTED"
);
break
;
}
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
(on){
System.out.println(
"on is true"
);
dataConnSwitchMethod = ITelephonyClass.getDeclaredMethod(
"enableDataConnectivity"
);
}
else
{
System.out.println(
"on is false"
);
dataConnSwitchMethod = ITelephonyClass.getDeclaredMethod(
"disableDataConnectivity"
);
}
dataConnSwitchMethod.setAccessible(
true
);
dataConnSwitchMethod.invoke(ITelephonyStub);
}
catch
(Exception e) {
Log.e(
"Error"
, e.toString());
}
}