参考链接:
https://blog.csdn.net/mp624183768/article/details/72900064 跳转移动数据设置界面的方法这里看来的
https://blog.csdn.net/llj0201/article/details/539918619 可以参考一下反射的具体写法
https://www.cnblogs.com/genggeng/p/7306566.html 网络连接类型判断可以参看这里
(链接搬运工在线搬运)
简单总结一下,5.0前后要用不同的反射的方法(具体参看上面链接,我就不cv了),但是5.0之后的要有系统权限,所以我实际使用的时候选择了跳设置页
public int getStat() { int stat = APN_OFF; try{ NetworkInfo mobileInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); boolean connect = mobileInfo.isConnected(); if(connect){ stat = APN_ON; }else{ stat = APN_OFF; } }catch(Exception e){ e.printStackTrace(); } return stat; }
public void setStat(int stat) { if (Build.VERSION.SDK_INT <= 19) { ConnectivityManager connectivitymanager = (ConnectivityManager) getContext() .getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivitymanager != null) { try { Class<? extends ConnectivityManager> connectClass = connectivitymanager.getClass(); Method method = connectClass.getMethod("setMobileDataEnabled", Boolean.TYPE); method.invoke(connectivitymanager, stat == APN_ON); } catch (Exception e) { e.printStackTrace(); } } } else { LauncherApplication.getContext().startActivity(new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS)); } }