Android 如何在安卓系统中对谷歌进行ping
我试图找到一种方法来知道用户何时连接到Internet,因为现在我有了这种方法:Android 如何在安卓系统中对谷歌进行ping,android,Android,我试图找到一种方法来知道用户何时连接到Internet,因为现在我有了这种方法: public boolean isNetworkOnline() { boolean status=false; try{ ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo
public boolean isNetworkOnline() {
boolean status=false;
try{
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getNetworkInfo(1);
if (netInfo != null && netInfo.getState()==NetworkInfo.State.CONNECTED) {
status= true;
}
}catch(Exception e){
e.printStackTrace();
return false;
}
return status;
}
如果用户已连接
,此方法将返回true,但如果用户已INTERNET连接
,则不会返回true,因此我认为如果此方法返回true,请调用另一个方法检查用户是否已连接到INTERNET。例如,有人可以连接到路由器,但没有互联网连接,所以我想知道用户何时有互联网连接或没有
我读过这本书和这本书,但当我连接了互联网
时,他们都会返回我false
。。。。我认为制作一个ping到www.google.com
的方法是一个很好的方法,可以知道某人是否有互联网连接,所以我尝试了这种方法,但它对我不起作用
知道用户何时连接互联网有什么想法或好方法(如果比我的想法更好的话)?你能试试这个方法吗
public boolean checkInternectConnection() {
try {
InetAddress inAddress= InetAddress.getByName("http://google.com");
if (inAddress.equals("")) {
return false;
} else {
return true;
}
} catch (Exception e) {
return false;
}
}
你能试试这个方法吗
public boolean checkInternectConnection() {
try {
InetAddress inAddress= InetAddress.getByName("http://google.com");
if (inAddress.equals("")) {
return false;
} else {
return true;
}
} catch (Exception e) {
return false;
}
}
请检查答案,看看它是否有用
你可以试试这个代码
try {
URL url = new URL("http://"+params[0]);
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setRequestProperty("User-Agent", "Android Application:"+Z.APP_VERSION);
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1000 * 30); // mTimeout is in seconds
urlc.connect();
if (urlc.getResponseCode() == 200) {
Main.Log("getResponseCode == 200");
return new Boolean(true);
}
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
请检查答案,看看它是否有用
你可以试试这个代码
try {
URL url = new URL("http://"+params[0]);
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setRequestProperty("User-Agent", "Android Application:"+Z.APP_VERSION);
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1000 * 30); // mTimeout is in seconds
urlc.connect();
if (urlc.getResponseCode() == 200) {
Main.Log("getResponseCode == 200");
return new Boolean(true);
}
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
检查互联网连接的简单方法
public boolean isConnectingToInternet() {
if (networkConnectivity()) {
try {
Process p1 = Runtime.getRuntime().exec(
"ping -c 1 www.google.com");
int returnVal = p1.waitFor();
boolean reachable = (returnVal == 0);
if (reachable) {
System.out.println("Internet access");
return reachable;
} else {
return false;
}
} catch (Exception e) {
return false;
}
} else
return false;
}
private boolean networkConnectivity() {
ConnectivityManager cm = (ConnectivityManager) _context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isConnected();
}
如何称呼它
if (isConnectingToInternet()) {
// internet is connected and work properly...
}
else {
// something went wrong internet not working properly...
}
检查互联网连接的简单方法
public boolean isConnectingToInternet() {
if (networkConnectivity()) {
try {
Process p1 = Runtime.getRuntime().exec(
"ping -c 1 www.google.com");
int returnVal = p1.waitFor();
boolean reachable = (returnVal == 0);
if (reachable) {
System.out.println("Internet access");
return reachable;
} else {
return false;
}
} catch (Exception e) {
return false;
}
} else
return false;
}
private boolean networkConnectivity() {
ConnectivityManager cm = (ConnectivityManager) _context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isConnected();
}
如何称呼它
if (isConnectingToInternet()) {
// internet is connected and work properly...
}
else {
// something went wrong internet not working properly...
}
我就是这样修复的,并将其作为一种实用方法使用,可从任何活动/片段等调用:
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivityManager
= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}//check if Wifi or Mobile data is enabled
private static class NetworkAsync extends AsyncTask<Void, Void, Boolean> {
@Override
protected Boolean doInBackground(Void... voids) {
try {
HttpURLConnection urlConnection = (HttpURLConnection)
(new URL("http://clients3.google.com/generate_204")
.openConnection());
urlConnection.setRequestProperty("User-Agent", "Android");
urlConnection.setRequestProperty("Connection", "close");
urlConnection.setConnectTimeout(1500);
urlConnection.connect();
return (urlConnection.getResponseCode() == 204 &&
urlConnection.getContentLength() == 0);
} catch (IOException e) {
// Error checking internet connection
}
return false;
}
}//network calls shouldn't be called from main thread otherwise it will throw //NetworkOnMainThreadException
我就是这样修复的,并将其作为一种实用方法使用,可从任何活动/片段等调用:
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivityManager
= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}//check if Wifi or Mobile data is enabled
private static class NetworkAsync extends AsyncTask<Void, Void, Boolean> {
@Override
protected Boolean doInBackground(Void... voids) {
try {
HttpURLConnection urlConnection = (HttpURLConnection)
(new URL("http://clients3.google.com/generate_204")
.openConnection());
urlConnection.setRequestProperty("User-Agent", "Android");
urlConnection.setRequestProperty("Connection", "close");
urlConnection.setConnectTimeout(1500);
urlConnection.connect();
return (urlConnection.getResponseCode() == 204 &&
urlConnection.getContentLength() == 0);
} catch (IOException e) {
// Error checking internet connection
}
return false;
}
}//network calls shouldn't be called from main thread otherwise it will throw //NetworkOnMainThreadException
它的可能副本必须在线程上,对吗?因为它具有
setConnectionTimeout()
权限?它必须位于线程上,对吗?因为这有setConnectionTimeout()
对吗?让我检查一下。。。等等,我能把这个方法放到线程上吗?因为它不会给时间去返回真或假。。。。我应该怎么做?是的,如果可以使用AsyncTask
执行此检查,效果会更好。因为如果您尝试在主活动线程内执行,则会发生networkMainThreadException
。有关执行此操作的信息,请参阅。此解决方案产生UnknownHostException:无法解析主机“”:没有与主机名关联的地址以进行meLet me检查。。。等等,我能把这个方法放到线程上吗?因为它不会给时间去返回真或假。。。。我应该怎么做?是的,如果可以使用AsyncTask
执行此检查,效果会更好。因为如果您尝试在主活动线程内执行,则会发生networkMainThreadException
。有关执行此操作的信息,请参阅。此解决方案产生未知的后异常:无法解析主机“”:没有与Mesimple copy的主机名关联的地址,并通过上述两种方法检查Internet连接,然后检查以下条件(isConnectingToInternet()){}对不起,错了,我把代码放在单独的类中,然后用new调用它。我现在编辑我的答案,你可以试试it@Pierre.Vriens我现在编辑我的答案并检查它。只需复制并通过上面两个方法,然后检查条件(isConnectingToInternet()){}对不起,错了,我把代码放在单独的类中,然后用new调用它。我现在编辑我的答案,你可以试试it@Pierre.Vriens我现在编辑我的答案,检查一下。