How can i turn off 3G/Data programmatically on Android?

匿名 (未验证) 提交于 2019-12-03 02:33:02

问题:

How can I turn off 3G/Data programmatically on Android?

Not Wifi, but 3G/Data.

回答1:

There is no official way to do this. However, it can be achieved unofficially with reflection.

For Android 2.3 and above:

private void setMobileDataEnabled(Context context, boolean enabled) {     final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);     final Class conmanClass = Class.forName(conman.getClass().getName());     final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");     iConnectivityManagerField.setAccessible(true);     final Object iConnectivityManager = iConnectivityManagerField.get(conman);     final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());     final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);     setMobileDataEnabledMethod.setAccessible(true);      setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled); }

This also requires the following permission.

 

For Android 2.2 and below:

Method dataConnSwitchmethod; Class telephonyManagerClass; Object ITelephonyStub; Class ITelephonyClass;  TelephonyManager telephonyManager = (TelephonyManager) context         .getSystemService(Context.TELEPHONY_SERVICE);  if(telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED){     isEnabled = true; }else{     isEnabled = false;   }     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 (isEnabled) {     dataConnSwitchmethod = ITelephonyClass             .getDeclaredMethod("disableDataConnectivity"); } else {     dataConnSwitchmethod = ITelephonyClass             .getDeclaredMethod("enableDataConnectivity");    } dataConnSwitchmethod.setAccessible(true); dataConnSwitchmethod.invoke(ITelephonyStub);

This required the following permission:

Note that both of these are unofficial and may no longer work. No more proof of this kind of thing breaking should be needed, as the 2.2 and below method broke on 2.3.



回答2:

Surround the code with try/catch blocks

public void mobiledataenable(boolean enabled) {  try {          final ConnectivityManager conman = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);         final Class conmanClass = Class.forName(conman.getClass().getName());         final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");         iConnectivityManagerField.setAccessible(true);         final Object iConnectivityManager = iConnectivityManagerField.get(conman);         final Class iConnectivityManagerClass         
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!