我遇到了有关设备是否可以上网的问题。让我简单解释一下我的查询示例: 我用一个用例来测试我的应用程序,即我在我的路由器wifi上,但是我从路由器上拔下以太网电缆并在wifi上连接我的设备并使用以下代码:
private boolean checkConnection() {
boolean connected = false;
ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm != null) {
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo) {
if ((ni.getTypeName().equalsIgnoreCase("WIFI")
|| ni.getTypeName().equalsIgnoreCase("MOBILE"))
& ni.isConnected() & ni.isAvailable()) {
connected = true;
}
}
}
return connected;
}
我将得到一个连接的布尔标志。这个代码返回交换机的设备状态意味着我的设备与WiFi连接或者没有,但我不会从服务器或主机得到任何响应,所以我需要检查互联网的可行性在我的设备上或不在我的设备上。请一些人帮助我同样的事情。我尝试了许多令人难以置信的方法或方法,但未能成功。
提前致谢。
答案 0 :(得分:0)
如果您需要检查连接是否确实提供了互联网,您可以尝试ping一些远程主机 - 例如:Google How to Ping External IP from Java Android
答案 1 :(得分:0)
On button click
if(isInternetON(MainActivity.this)){
mMyDownloadTask = new MyDownloadTask();
delayCancel = new DelayCancel(mMyDownloadTask);
handler.postDelayed(delayCancel,3*1000);
mMyDownloadTask.execute();
}
public static class MyDownloadTask extends AsyncTask<Boolean,Void,Boolean> {
public MyDownloadTask() {
}
protected Boolean doInBackground(Boolean... params) {
try {
URL url = new URL("http://www.google.com");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// connection.setRequestMethod("Head");
// connection.setConnectTimeout(2000);
connection.connect();
int code = connection.getResponseCode();
Log.e(TAG, "code : " + code);
if (code == 200) {
ThreadActivity.isAviliable = true;
MainActivity.value = "true";
return true;
} else {
ThreadActivity.isAviliable = false;
MainActivity.value = "false";
return false;
}
} catch (Exception e) {
ThreadActivity.isAviliable = false;
MainActivity.value = "false";
e.printStackTrace();
return false;
}
}
protected void onPostExecute(Boolean result) {
// dismiss progress dialog and update ui
Log.e(TAG,"result "+result);
if(result){
ThreadActivity.isAviliable = true;
MainActivity.value = "true";
}else{
ThreadActivity.isAviliable = false;
MainActivity.value = "false";
}
}
}
public static class DelayCancel implements Runnable{
private AsyncTask task;
public DelayCancel(AsyncTask task) {
this.task = task;
}
@Override
public void run() {
Log.e(TAG, "Running.....");
if (task.getStatus() == AsyncTask.Status.RUNNING) {
task.cancel(true);
Log.e(TAG, "Cancel 1.....");
ThreadActivity.isAviliable = false;
MainActivity.value = "false";
}
if (task.getStatus() == AsyncTask.Status.PENDING){
task.cancel(true);
ThreadActivity.isAviliable = false;
MainActivity.value = "false";
Log.e(TAG, "Cancel 2.....");
}
}
}
public static boolean isInternetON(Context ctx){
ConnectivityManager cm =(ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
/* Log.e("Common Class","activeNetwork.getState()"+activeNetwork.getState());
Log.e("Common Class","activeNetwork.isAvailable()"+activeNetwork.isAvailable());
Log.e("Common Class","activeNetwork.getReason()"+activeNetwork.getReason());
Log.e("Common Class","activeNetwork.getExtraInfo()"+activeNetwork.getExtraInfo());
Log.e("Common Class","activeNetwork.describeContents()"+activeNetwork.describeContents());
Log.e("Common Class","activeNetwork.isConnectedOrConnecting()"+activeNetwork.isConnectedOrConnecting());
Log.e("Common Class","activeNetwork.isFailover()"+activeNetwork.isFailover());*/
boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting() && activeNetwork.isAvailable();
return isConnected;
}