开关数据连接方法:
获取数据连接状态:
Method getITelephonyMethod = null;
try{
telephonyManagerClass = Class.forName(telephonyManager.getClass().getName());
getITelephonyMethod = telephonyManagerClass.getDeclaredMethod("getITelephony");
getITelephonyMethod.setAccessible(true);
ITelephonyStub = getITelephonyMethod.invoke(telephonyManager);
ITelephonyClass = Class.forName(ITelephonyStub.getClass().getName());
if (mobileFlg) {
dataConnSwitchavenue = ITelephonyClass
.getDeclaredMethod("enableDataConnectivity");
Settings.System.putInt(context.getContentResolver(),
Settings.System.GPRS_CONNECTION_SIM_SETTING, true ? 1
: 0);
} else {
dataConnSwitchavenue = ITelephonyClass
.getDeclaredMethod("disableDataConnectivity");
Settings.System.putInt(context.getContentResolver(),
Settings.System.GPRS_CONNECTION_SIM_SETTING, false ? 1
: 0);
}
dataConnSwitchavenue.setAccessible(true);
dataConnSwitchavenue.invoke(ITelephonyStub);
}catch (ClassNotFoundException e) {
e.printStackTrace();
}catch (NoSuchMethodException e){
e.printStackTrace();
}catch (IllegalAccessException e) {
e.printStackTrace();
}catch (InvocationTargetException e){
e.printStackTrace();
}
获取数据连接状态:
public boolean getMobileState() {
boolean isEnabled = false;
if(telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED){
isEnabled = true;
}else{
isEnabled = false;
}
return isEnabled;
}