监控 URL 是否在 Android 持续在线
Monitor if URL is online constantly in Android
我想创建 URL 监视器,如果 URL 在线,它将在后台每隔 x 秒进行一次监视。
ConnectivityManager
对我不利,因为我的应用程序是在受控环境中使用的,尽管互联网可以正常工作,但某些端口需要关闭。
所以我需要监控foo.com/9000
是否一直在线,当我请求isOnline
时,我想立即得到结果,所以监控应该在后台进行。
我将如何完成此操作,是否有可以执行此操作的库?
在 Actionscript 中我会调用 UrlMonitor
并传递它 url
你能用它来重复这个任务吗:
Repeat a task with a time delay?
这是任务:
HttpGet request = new HttpGet();
URI uri = new URI("your_url");
request.setURI(uri);
HttpResponse response = httpClient.execute(request);
if (response.getStatusLine().toString().equalsIgnoreCase("HTTP/1.1 200 OK")) {
// it's there
}
private static boolean internetConnectionAvailable;
private static ScheduledExecutorService scheduleTaskExecutor;
public static void stopInternetMonitor() {
if (scheduleTaskExecutor != null && !scheduleTaskExecutor.isShutdown()) {
scheduleTaskExecutor.shutdown();
}
}
public static void startInternetMonitor() {
Runnable runnable = new Runnable() {
@Override
public void run() {
isInternetConnectionAvailableSync();
}
};
if (scheduleTaskExecutor != null) {
if (scheduleTaskExecutor.isShutdown()) {
scheduleTaskExecutor.scheduleWithFixedDelay(runnable, 0, 30, TimeUnit.SECONDS);
}
} else {
scheduleTaskExecutor = Executors.newScheduledThreadPool(5);
scheduleTaskExecutor.scheduleWithFixedDelay(runnable, 0, 30, TimeUnit.SECONDS);
}
}
public static boolean isInternetConnectionAvailableCached() {
ConnectivityManager cm = (ConnectivityManager) FashionTrenderApplication.getInstance()
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnected() && internetConnectionAvailable) {
return true;
}
return false;
}
public static boolean isInternetConnectionAvailableSync() {
ConnectivityManager cm = (ConnectivityManager) FashionTrenderApplication.getInstance()
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnected()) {
try {
URL url = new URL(EnvironmentConfiguration.getInstance().getServerUrl());
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setRequestProperty("User-Agent", "test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1000); // mTimeout is in seconds
urlc.connect();
if (urlc.getResponseCode() == 200) {
internetConnectionAvailable = true;
return true;
} else {
internetConnectionAvailable = false;
return false;
}
} catch (IOException e) {
Log.i("warning", "Error checking internet connection", e);
return false;
}
}
return false;
}
我想创建 URL 监视器,如果 URL 在线,它将在后台每隔 x 秒进行一次监视。
ConnectivityManager
对我不利,因为我的应用程序是在受控环境中使用的,尽管互联网可以正常工作,但某些端口需要关闭。
所以我需要监控foo.com/9000
是否一直在线,当我请求isOnline
时,我想立即得到结果,所以监控应该在后台进行。
我将如何完成此操作,是否有可以执行此操作的库?
在 Actionscript 中我会调用 UrlMonitor
并传递它 url
你能用它来重复这个任务吗: Repeat a task with a time delay?
这是任务:
HttpGet request = new HttpGet();
URI uri = new URI("your_url");
request.setURI(uri);
HttpResponse response = httpClient.execute(request);
if (response.getStatusLine().toString().equalsIgnoreCase("HTTP/1.1 200 OK")) {
// it's there
}
private static boolean internetConnectionAvailable;
private static ScheduledExecutorService scheduleTaskExecutor;
public static void stopInternetMonitor() {
if (scheduleTaskExecutor != null && !scheduleTaskExecutor.isShutdown()) {
scheduleTaskExecutor.shutdown();
}
}
public static void startInternetMonitor() {
Runnable runnable = new Runnable() {
@Override
public void run() {
isInternetConnectionAvailableSync();
}
};
if (scheduleTaskExecutor != null) {
if (scheduleTaskExecutor.isShutdown()) {
scheduleTaskExecutor.scheduleWithFixedDelay(runnable, 0, 30, TimeUnit.SECONDS);
}
} else {
scheduleTaskExecutor = Executors.newScheduledThreadPool(5);
scheduleTaskExecutor.scheduleWithFixedDelay(runnable, 0, 30, TimeUnit.SECONDS);
}
}
public static boolean isInternetConnectionAvailableCached() {
ConnectivityManager cm = (ConnectivityManager) FashionTrenderApplication.getInstance()
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnected() && internetConnectionAvailable) {
return true;
}
return false;
}
public static boolean isInternetConnectionAvailableSync() {
ConnectivityManager cm = (ConnectivityManager) FashionTrenderApplication.getInstance()
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnected()) {
try {
URL url = new URL(EnvironmentConfiguration.getInstance().getServerUrl());
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setRequestProperty("User-Agent", "test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1000); // mTimeout is in seconds
urlc.connect();
if (urlc.getResponseCode() == 200) {
internetConnectionAvailable = true;
return true;
} else {
internetConnectionAvailable = false;
return false;
}
} catch (IOException e) {
Log.i("warning", "Error checking internet connection", e);
return false;
}
}
return false;
}