我需要检测互联网连接是否可用并且在android中正常工作。在开始任何WebView活动之前,我需要这个条件检查才能调用。
到目前为止,我已设法实现此答案中的isAvailable()功能: Class which check for connectivity
但是,如果wifi打开但未连接以太网插头,则上述方法失败。 有人说使用ping是个好主意。但我无法成功实现此功能。
我在this link使用的代码: -
班级名称:AppStatus
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
public class AppStatus {
private static AppStatus instance = new AppStatus();
static Context context;
ConnectivityManager connectivityManager;
NetworkInfo wifiInfo, mobileInfo;
boolean connected = false;
public static AppStatus getInstance(Context ctx) {
context = ctx.getApplicationContext();
return instance;
}
public boolean isOnline() {
try {
connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
connected = networkInfo != null && networkInfo.isAvailable() &&
networkInfo.isConnected();
return connected;
} catch (Exception e) {
System.out.println("CheckConnectivity Exception: " + e.getMessage());
Log.v("connectivity", e.toString());
}
return connected;
}
此代码有助于检查互联网连接,但在前面提到的情况下失败: -
if (AppStatus.getInstance(this).isOnline()) {
Toast.makeText(this,"You are online!!!!",8000).show();
} else {
Toast.makeText(this,"Internet Connection is required !",8000).show();
Log.v("Home", "############################You are not online!!!!");
finish();
}
答案 0 :(得分:0)
public static boolean isNetworkAvailable(Context mContext) {
ConnectivityManager cm = (ConnectivityManager) mContext
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
Log.e("Network Testing", "***Available***");
return true;
}
Log.e("Network Testing", "***Not Available***");
return false;
}
答案 1 :(得分:0)
感谢@camelCaseCoder的评论。 我得到了一个我成功实施的代码,我想与大家分享,因此我回答了我自己的问题。
AppStatus成为(在评论出上一段代码后): -
public class AppStatus {
private static AppStatus instance = new AppStatus();
static Context context;
//ConnectivityManager connectivityManager;
//NetworkInfo wifiInfo, mobileInfo;
//boolean connected = false;
public static AppStatus getInstance(Context ctx) {
context = ctx.getApplicationContext();
return instance;
}
public boolean isOnline() {
/*try {
connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
connected = networkInfo != null && networkInfo.isAvailable() &&
networkInfo.isConnected();
return connected;
} catch (Exception e) {
System.out.println("CheckConnectivity Exception: " + e.getMessage());
Log.v("connectivity", e.toString());
}
return connected;
*/
Runtime runtime = Runtime.getRuntime();
try {
Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
int exitValue = ipProcess.waitFor();
return (exitValue == 0);
} catch (IOException e) { e.printStackTrace(); }
catch (InterruptedException e) { e.printStackTrace(); }
return false;
}
}
与以前类似地实施: -
//check for internet connectivity by calling isOnline-using Ping @google
if (AppStatus.getInstance(this).isOnline()) {
Toast.makeText(this, "You are online!!!!", 8000).show();
} else {
Toast.makeText(this,"Internet Connection is required !",8000).show();
Log.v("Home", "############################You are not online!!!!");
finish();
}