概述
最近在项目中用到了:断网重连、判断是否有网络并且可以访问外网之类的很多坑,记下来,万一忘了呢,由于本人所在的公司,是在硬件上做开发的,所以很多都要结合音频提示用户当前状态,基本上很少涉及UI之类的,所以实时监听网络并提示用户实为重要,哥们研究了好久,而且在硬件上做开发的难度比手机上的坑多,我们基本上是连接wifi,当然这个模块也包含了4G网络的判断,不扯了,进入正题:
public WifiConnectUtil ConnectToNetworkWPA(final Context context, final String networkSSID, final String password) {
connecting = true;
if (mInstance == null) {
create(context);
}
clearWifiConfigs();
AudioManagment.getAudioManagerInstance(context).addListener(new AudioManagment.AudioListener() {
@Override
public void onCpmpleted() {
if (TextUtils.isEmpty(networkSSID.trim())) {
long time = 0;
while (!isConnected(context) && time <= 3000) {
try {
Thread.sleep(1000);
time += 1000;
} catch (InterruptedException e) {
}
}
AudioManagment.getAudioManagerInstance(context).getAudioManagerInstance(context)
.addListener(new AudioManagment.AudioListener() {
@Override
public void onCpmpleted() {
if (wifiConnectedListener != null) {
wifiConnectedListener.onNetworkConneced("网络连接失败", -1);
connecting = false;
}
}
}).play("net_connected_failed");
} else {
try {
if (TextUtils.isEmpty(password.trim())) {
//新建wifi配置
WifiConfiguration config = new WifiConfiguration();
config.allowedAuthAlgorithms.clear();
config.allowedGroupCiphers.clear();
config.allowedKeyManagement.clear();
config.allowedPairwiseCiphers.clear();
config.allowedProtocols.clear();
config.SSID = "\"" + networkSSID + "\"";
// 没有密码
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wifiManager.addNetwork(config);
wifiManager.saveConfiguration();
} else {
//新建wifi配置
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\"";
conf.preSharedKey = "\"" + password + "\"";
conf.status = WifiConfiguration.Status.ENABLED;
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
Log.e("xiaoman", conf.SSID + " connecting " + conf.preSharedKey);
int i1 = wifiManager.addNetwork(conf);
Log.e(TAG, "---" + i1);
Log.e("xiaoman", conf.SSID + " after connecting " + conf.preSharedKey);
}
//拿到所有的wifi配置信息,与之前新建的配置信息匹配,连接
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
if (list != null) {
for (WifiConfiguration i : list) {
//连接与指定名称相同的wifi
if (i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
wifiManager.saveConfiguration();
break;
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
long len = 0;
while (!isConnected(context) && len <= outTime) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
len += 1000;
}
//最后在判断一次,确保是否连上网络
if (!isConnected(context)) {
clearWifiConfigs();
AudioManagment.getAudioManagerInstance(context)
.addListener(new AudioManagment.AudioListener() {
@Override
public void onCpmpleted() {
if (wifiConnectedListener != null) {
wifiConnectedListener.onNetworkConneced("网络连接失败", -1);
connecting = false;
}
}
}).play("net_connected_failed");
} else {
if (wifiConnectedListener != null) {
wifiConnectedListener.onNetworkConneced("网络连接成功", 0);
connecting = false;
}
}
}
}
}).play("net_connecting");
return mInstance;
}
/**
* 判断wifi、移动网络是否已经连接上,脸上了不一定能上网,之前这里坑我一下,下一步需要判断是否可以访问外网
*
* @return
*/
public boolean isConnected(Context context) {
if (mInstance == null) {
create(context);
}
if (manager == null) {
manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
}
NetworkInfo activeNetwork = manager.getActiveNetworkInfo();
if (activeNetwork != null) { // connected to the internet
if (activeNetwork.isConnected()) {
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
return isDataConnected();
} else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
return isDataConnected();
}
} else {
return false;
}
} else {
return false;
}
return false;
}
这一步我直接ping一个网络地址
/**
* 判断网络是否可以同外网
*
* @return
*/
public Boolean isDataConnected() {
try {
Process p1 = Runtime.getRuntime().exec("ping -c 1 公司的网址");
int returnVal = p1.waitFor();
boolean reachable = (returnVal == 0);
Log.e(TAG, reachable + "");
return reachable;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
网络模块确实很多坑,特别是在满足项目需求的时候,各种坑;今天就到这里,待续;