Java 避免DD异常而不添加return语句
我有这段代码,我必须避免DD异常“最近重新定义了一个变量”。但我也必须避免创建另一个return语句。我不知道怎么做。如果有人能帮助我,那将是非常有帮助的 DD异常代码Java 避免DD异常而不添加return语句,java,android,Java,Android,我有这段代码,我必须避免DD异常“最近重新定义了一个变量”。但我也必须避免创建另一个return语句。我不知道怎么做。如果有人能帮助我,那将是非常有帮助的 DD异常代码 public static boolean hasActiveInternetConnection() { int returnVal = 1; try { final Process ping = java.lang.Runtime.getRuntime().exec("ping -c 1 ww
public static boolean hasActiveInternetConnection() {
int returnVal = 1;
try {
final Process ping = java.lang.Runtime.getRuntime().exec("ping -c 1 www.google.com");
returnVal = ping.waitFor();
} catch (final java.io.IOException | InterruptedException e) {
Log.e(TAG, e.getMessage());
}
return returnVal == 0;
}
双返回异常码
public static boolean hasActiveInternetConnection() {
try {
final Process ping = java.lang.Runtime.getRuntime().exec("ping -c 1 www.google.com");
final int returnVal = ping.waitFor();
return returnVal == 0;
} catch (final java.io.IOException | InterruptedException e) {
Log.e(TAG, e.getMessage());
}
return false;
}
谢谢
public boolean isNetworkActive() {
try {
Process ping = java.lang.Runtime.getRuntime().exec("ping -c 1 www.google.com");
return ping.waitFor()==0? true : false;
} catch (final java.io.IOException | InterruptedException e) {
return false;
}
}
顺便说一句,这不是在Android上测试网络连接的方式,这里有一个更有用的方法:
public static boolean checkNetworkStatus(Context context) {
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
boolean networkIsMobile = false;
boolean networkIsWiFi = false;
if (wifi != null && wifi.isAvailable() && wifi.isConnected()) {
networkIsWiFi = true;
}
if (mobile != null && mobile.isAvailable() && mobile.isConnected()) {
networkIsMobile = true;
}
boolean networkActive = networkIsMobile || networkIsWiFi;
return networkActive;
} // End of checkNetworkStatus
PS:我假设pingFor==0表示网络,pingFor==1表示无网络,如果是另一种情况,请切换它们对我有效,我非常感谢