其他分享
首页 > 其他分享> > Android 开启/关闭 数据连接

Android 开启/关闭 数据连接

作者:互联网

import android.net.ConnectivityManager;
import java.lang.reflect.Method;

public class NetworkManager {    
   private static Method sMethodGetMobileDataEnabled;
   private static Method sMethodSetMobileDataEnabled;
   static {initReflectionMethod();}
   private static void initReflectionMethod()
   {
      Class<ConnectivityManager> clazz = ConnectivityManager.class;
      try {
         sMethodGetMobileDataEnabled = clazz.getMethod("getMobileDataEnabled", new Class[0]);
         sMethodGetMobileDataEnabled.setAccessible(true);
         sMethodSetMobileDataEnabled = clazz.getMethod("setMobileDataEnabled", new Class[] {Boolean.TYPE});
         sMethodSetMobileDataEnabled.setAccessible(true);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

   /**
     * Gets the value of the setting for enabling Mobile data.
     *
     * @param manager the {@link ConnectivityManager}
     *      
     * @return Whether mobile data is enabled.    
   */
   public static boolean getMobileDataEnabled(ConnectivityManager manager) {
      initReflectionMethod();
      try 
      {
         return (Boolean) sMethodGetMobileDataEnabled.invoke(manager, new Object[0]);
      } catch (Exception e) {

         e.printStackTrace();
      }
      return false;
   }
    
   /**
     * Sets the persisted value for enabling/disabling Mobile data.
     *
     * @param manager the {@link ConnectivityManager}
     * 
     * @param enabled Whether the mobile data connection should be     
     *            used or not.
     */
   public static void setMobileDataEnabled(ConnectivityManager manager, boolean enabled) 
   {
      initReflectionMethod();
      try 
      {
         sMethodSetMobileDataEnabled.invoke(manager, new Object[] {Boolean.valueOf(enabled)});
      } 
      catch (Exception e) 
      {  
         e.printStackTrace();
      }
   }
}

调用方法:

ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if(!Network.getMobileDataEnabled(connManager))
   Network.setMobileDataEnabled(connManager, true);
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />

以上方法在android5.0以后不适应了, android5.0以后使用以下方法:

public void setMobileDataState(Context context, boolean enabled) {
	TelephonyManager telephonyService = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
	try {
		Method setDataEnabled = telephonyService.getClass().getDeclaredMethod("setDataEnabled",boolean.class);
		if (null != setDataEnabled) {
			setDataEnabled.invoke(telephonyService, enabled);
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
}

public boolean getMobileDataState(Context context) {
	TelephonyManager telephonyService = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
	try {
		Method getDataEnabled = telephonyService.getClass().getDeclaredMethod("getDataEnabled");
		if (null != getDataEnabled) {
			return (Boolean) getDataEnabled.invoke(telephonyService);
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
	return false;
}
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />

添加系统签名

标签:return,ConnectivityManager,enabled,开启,manager,static,关闭,telephonyService,Android
来源: https://blog.51cto.com/u_15298588/3034038