How do I use an eventinjector on a browser in blackberry to close the browser. I want to simulate the ESCAPE key being pressed on the handheld when the browser loads so that the app exits the browser and goes back to the main screen. I tried doing this myself but have not been successful. Any help would be most appreciated.
问题:
回答1:
If you really want to control browser, you could use BrowserField
, BrowserField2
in your app.
You could also inject listener for key pressing or tracking which app is visible right now. But it will be really tricky because users are switching between apps often and there are quite many devices with touch interfaces now (user could close page without esc button).
回答2:
Not sure why you want to close the browser, but I'll assume you know that it's the right thing to do (also, Eugen already suggested how you could use the BrowserField
to let the user browse from within your app and avoid this issue).
Anyway, I have some code that I use to close the Camera (which my app did start, intentionally). You could probably close the browser the same way. This is a hack, but at the time, it was the way I solved the problem:
/** Delay required to keep simulated keypresses from occurring too fast, and being missed */
private static final int KEYPRESS_DELAY_MSEC = 100;
/** Max number of attempts to kill camera via key injection */
private static final int MAX_KEY_PRESSES = 10;
/** Used to determine when app has been exposed by killing Camera */
private MainScreen _mainScreen;
/** Counter for toggling key down/up */
private int _keyEventCount = 0;
public void run() {
// The picture has been taken, so close the camera app by simulating the ESC key press
if (!_mainScreen.isExposed()) {
int event = ((_keyEventCount % 2) == 0) ? EventInjector.KeyCodeEvent.KEY_DOWN :
EventInjector.KeyCodeEvent.KEY_UP;
EventInjector.KeyEvent injection = new EventInjector.KeyEvent(event, Characters.ESCAPE, 0);
// http://supportforums.blackberry.com/t5/Java-Development/How-to-use-EventInjector-to-inject-ESC/m-p/74096
injection.post();
injection.post();
// Toggle back and forth .. key up .. key down
_keyEventCount++;
if (_keyEventCount < MAX_KEY_PRESSES) {
// Keep scheduling this method to run until _mainScreen.isExposed()
UiApplication.getUiApplication().invokeLater(this, KEYPRESS_DELAY_MSEC, false);
} else {
// Give up and just take foreground ... user will have to kill camera manually
UiApplication.getUiApplication().requestForeground();
}
} else {
// reset flag
_keyEventCount = 0;
}
}
My _mainScreen
is the Screen
that should be uncovered by closing the Camera app, so I use it to test that I closed Camera successfully. Also, in my app, I reset
_keyEventCount = 0;
every time the Camera was launched (which is not shown above).
Update:
Also, this is the code my _mainScreen
object needs to keep track of whether it's exposed or not:
private boolean _isExposed = false;
protected void onExposed() {
super.onExposed();
_isExposed = true;
}
protected void onObscured() {
super.onObscured();
_isExposed = false;
}
public boolean isExposed() {
return _isExposed;
}