Swing - Launch browser and listen when user closes it
I have this specific requirement. If user clicks on a button in GUI, I need to launch browser. I need to wait for two minutes. If user closes browser before two minutes, I need to launch window 'A'. If browser is kept open for more than two minutes, I need to launch window 'B'.
Presently I am able to launch browser window. But not able to listen when user closes it. Below is the snapshot of the code that I have written.
String url = "www.google.com";
Process browserProcess;
String commandLine = "rundll32 开发者_StackOverflow中文版url.dll,FileProtocolHandler " + url;
String[] args = commandLine.split(" ");
ProcessBuilder pb = new ProcessBuilder(args);
pb.redirectErrorStream(true);
browserProcess = pb.start();
in = new BufferedReader(new InputStreamReader(browserProcess.getErrorStream()));
String lineRead;
while ((lineRead = in.readLine()) != null) {
//do nothing
}
int exitVal;
System.out.println("Waiting...");
exitVal = browserProcess.waitFor();
System.out.println("Done");
Presently, waitFor() returns as soon as browser is launched. I need to wait till user closes the browser window. Any idea how it can be done?
First, I think you should take a look at JDIC, which I believe have been integrated into Java SE. Now, you have two solutions.
- Either you use a browser integrated into your application, then you'll be able to see if user spent those two minutes watching the flies fly by. in this case, it'll be easy, by using a time counter, as an example, to see which frame to open.
- Either you prefer (for maybe good reasons), to use an external web browser (Opera, Chromium, K-meleon, you name it). Then, you'll have to communicate with target web site to know when user closed frame. Indeed, it is possible for Javascript to see when page/tab is closed, and to send its own server a query to say "user 1258 just closed the window". Then, using client/server communication, your server will have to tell your application how much time the user looked at the window. Far more complicated, no ?
精彩评论