android 关闭数据连接方法
16lz
2022-01-23
开关数据连接方法:
获取数据连接状态:
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;}
更多相关文章
- android中数据存储的contentprovider的使用方法
- Android之SharedPreferences轻量数据存储
- Android(安卓)使用内容解析者往短信数据库里插入一条数据
- Android(安卓)Handler不同界面发送数据
- Android(安卓)SQLite服务--创建、增删改查
- android 利用ksoap2方式连接webservice
- android 利用Gson生成json数据
- 关于Android(安卓)中sqlite 报can not open database错误
- mybatisplus的坑 insert标签insert into select无参数问题的解决