android实现wifi与移动数据的切换

在网上搜索开启wifi后自动关闭移动数据,但搜到的都是如何打开关闭wifi/3g,如何监听wifi/3g的状态,没有完整的。于是自己整了一天,终于搞定了。


首先在服务中注册动态广播,在OnReceive方法中,获得网络连接服务,然后创建NetworkInfo对象获得网络连接类型,用Boolean类型的isWifiConnected做判断。当wifi在一定时间内没有连上,就关闭wifi并打开3g网,当wifi连上了就关闭3g网。

mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
boolean isWifiConnected = false;
boolean isMobileConnected = false;
ConnectivityManager connMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
isWifiConnected = networkInfo.isConnected();
//判断wifi是否开启
if (isWifiConnected) {
Toast.makeText(context, "wifi is on", 0).show();
Log.i("NetLisService", "wifi is "+isWifiConnected);

//wifi连接上就关闭3g网
NetLisService.this.setMobileDataStatus(NetLisService.this,false);
}else{
Toast.makeText(context, "wifi is off", 0).show();
Log.i("NetLisService", "wifi is "+isWifiConnected);
new Thread(){
public void run() {
try {
//当wifi断开十秒没连接上就关闭wifi
Thread.sleep(10000);
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
//关闭wifi
wifiManager.setWifiEnabled(false);
//开启3g
NetLisService.this.setMobileDataStatus(NetLisService.this,true);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}.start();
}

//网络连接改为3g模式
networkInfo = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
isMobileConnected = networkInfo.isConnected();
//判断3g是否开启
if (isMobileConnected) {
Toast.makeText(context, "3g is on", 0).show();
Log.i("NetLisService", "3g is "+isMobileConnected);
}else{
Toast.makeText(context, "3g is off", 0).show();
Log.i("NetLisService", "3g is "+isMobileConnected);
}
}
};
IntentFilter filter = new IntentFilter();

//添加过滤action
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(mReceiver, filter);


这是网上扒下来的开关移动数据的方法

public void setMobileDataStatus(Context context , boolean enabled) {
ConnectivityManager conMgr = (ConnectivityManager) this
.getSystemService(Context.CONNECTIVITY_SERVICE);
// ConnectivityManager类
Class conMgrClass = null;
// ConnectivityManager类中的字段
Field iConMgrField = null;
// IConnectivityManager类的引用
Object iConMgr = null;
// IConnectivityManager类
Class iConMgrClass = null;
// setMobileDataEnabled方法
Method setMobileDataEnabledMethod = null;
try {
// 取得ConnectivityManager类
conMgrClass = Class.forName(conMgr.getClass().getName());
// 取得ConnectivityManager类中的对象Mservice
iConMgrField = conMgrClass.getDeclaredField("mService");
// 设置mService可访问
iConMgrField.setAccessible(true);
// 取得mService的实例化类IConnectivityManager
iConMgr = iConMgrField.get(conMgr);
// 取得IConnectivityManager类
iConMgrClass = Class.forName(iConMgr.getClass().getName());
// 取得IConnectivityManager类中的setMobileDataEnabled(boolean)方法
setMobileDataEnabledMethod = iConMgrClass.getDeclaredMethod(
"setMobileDataEnabled", Boolean.TYPE);
// 设置setMobileDataEnabled方法是否可访问
setMobileDataEnabledMethod.setAccessible(true);
// 调用setMobileDataEnabled方法
setMobileDataEnabledMethod.invoke(iConMgr, enabled);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}

你可能感兴趣的:(android笔记之网络篇)