首页>代码>android网络设置(wifi、gprs)>/NetWorkSetting/src/com/tyh/APNSetting.java
package com.tyh;

import java.lang.reflect.Method;
import android.content.Context;
import android.net.ConnectivityManager;
import android.widget.Toast;

public class APNSetting {
	private boolean isMobileDataEnable;
	private ConnectivityManager mConnectivityManager;
	private NetWorkSettingActivity network;
	/**
	 * @author tyh
	 * qq:2576318211 欢迎交流
	 * */
	public APNSetting(NetWorkSettingActivity networxt){
		this.network = networxt;
		mConnectivityManager = (ConnectivityManager) networxt.getSystemService(Context.CONNECTIVITY_SERVICE);  
		try {
			Object[] arg = null;
			isMobileDataEnable = (Boolean) invokeMethod("getMobileDataEnabled", arg);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//开启网络
	public void APNchange(){
		try {
			invokeBooleanArgMethod("setMobileDataEnabled", !isMobileDataEnable);			
			isMobileDataEnable = !isMobileDataEnable;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Object invokeMethod(String methodName,
			Object[]  arg) throws Exception {
		Class ownerClass = mConnectivityManager.getClass();
		Class[]  argsClass = null;
		if (arg != null) {
			argsClass = new Class[1];
			argsClass[0] = arg.getClass();
		}
		Method method = ownerClass.getMethod(methodName, argsClass);
		return method.invoke(mConnectivityManager, arg);
	}
	 
	 public Object invokeBooleanArgMethod(String methodName,
				boolean value) throws Exception {
			Class ownerClass = mConnectivityManager.getClass();
			Class[]  argsClass = new Class[1];
				argsClass[0] = boolean.class;
			Method method = ownerClass.getMethod(methodName,argsClass);
			return method.invoke(mConnectivityManager, value);
	}
}
最近下载更多
微笑刺客  LV21 2023年1月13日
张恺祺  LV6 2022年5月21日
cnfdmh  LV1 2019年12月12日
q326063404  LV1 2018年11月27日
驱蚊器无qweqw  LV4 2018年2月6日
最代码官方  LV168 2017年7月24日
最近浏览更多
13133117021  LV5 2024年12月29日
2636804923  LV6 2024年6月20日
skook7  LV2 2023年6月28日
妖刀飞姬杯  LV2 2023年6月9日
微笑刺客  LV21 2023年1月13日
炫瓶百事可乐  LV1 2022年12月1日
wuziayng1232  LV11 2022年7月28日
18650066502  LV12 2022年6月20日
张恺祺  LV6 2022年5月21日
cxywt12  LV2 2022年5月20日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友