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" />

添加系统签名

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值