Android的服务 - 平安网址 [英] Android Service - Ping URL
问题描述
我如何使用Android的服务做一个ping回调?我需要一个按钮,点击打开网页,但在后台,去ping另一个URL的统计信息收集。
How can I use Android Service to do a ping callback? I need to open a webpage on a button click, but in the background, go ping another url for stats collection.
code段将是很大的帮助。
Code snippets will be greatly helpful
谢谢 克里斯
推荐答案
我觉得如果你只是想ping一个URL,你可以使用这个code:
I think if you just want to ping an url, you can use this code :
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();
}
和,如果你想有一个平相当于,你可以前后调用后把System.currentTimeMillis的(),并计算出差异。
And, if you want to have the equivalent of a ping, you can put a System.currentTimeMillis() before and after the call, and calculate the difference.
希望这有助于
code段的<一个发现href="http://stackoverflow.com/questions/1560788/how-to-check-internet-access-on-android-inetaddress-never-timeouts">here
这篇关于Android的服务 - 平安网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!