1)监测网络状态
// 检查当前网络是够连接
public boolean isConnecting()
{
ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
boolean result = false;
State mobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState(); //3G连接状态
State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();//WIFI连接状态
result = (mobile == State.CONNECTED || wifi == State.CONNECTED);
return result;
}
网络状态有如下6种(定义在NetworkInfo.State中)
CONNECTED 已连接
CONNECTIONG 正在连接
DISCONNETED 已断开
DISCONNETING 正在断开
SUSPENDED 已挂起
UNKNOW 未知
2)调出网络设置界面
startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS)); //进入无线网络配置界面
startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS)); //进入手机中的wifi网络设置界面