2

I want to create URL monitor that will monitor in background each x seconds if the URL is online.

ConnectivityManager is not good for me because my app is used in controlled environment and although internet works some ports need to be closed.

So I need to monitor if foo.com/9000 is online all the time and when I request isOnline I want to get the result immediately, so monitoring should be done in background.

How would I accomplish this and is there a library that does this?

In Actionscript I would call UrlMonitor and pass it url

Tree
  • 29,135
  • 24
  • 78
  • 98

2 Answers2

1

Could you use this to repeat the task: Repeat a task with a time delay?

This being the task:

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
}
Community
  • 1
  • 1
Al Lelopath
  • 6,448
  • 13
  • 82
  • 139
0
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;
}
Tree
  • 29,135
  • 24
  • 78
  • 98