如何在黑莓浏览器上使用事件注入器来关闭浏览器。我想模拟浏览器加载时在手持设备上按下的 ESCAPE 键,以便应用退出浏览器并返回主屏幕。我自己尝试过这样做,但没有成功。非常感激任何的帮助。
问问题
174 次
2 回答
2
如果你真的想控制浏览器,你可以在你的应用程序中使用BrowserField
, BrowserField2
。
您还可以注入侦听器以进行按键或跟踪当前可见的应用程序。但这将非常棘手,因为用户经常在应用程序之间切换,而且现在有相当多的设备带有触摸界面(用户可以在没有 esc 按钮的情况下关闭页面)。
于 2012-06-14T07:52:01.550 回答
1
不确定您为什么要关闭浏览器,但我假设您知道这是正确的做法(此外,Eugen 已经建议您如何使用BrowserField
让用户从您的应用程序中浏览并避免此问题)。
无论如何,我有一些代码可以用来关闭相机(我的应用程序确实启动了,是故意的)。您可能会以同样的方式关闭浏览器。这是一个 hack,但当时,这是我解决问题的方式:
/** 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;
}
}
我_mainScreen
是Screen
应该通过关闭相机应用程序发现的,所以我用它来测试我是否成功关闭了相机。另外,在我的应用程序中,我重置
_keyEventCount = 0;
每次启动相机时(上面没有显示)。
更新:
此外,这是我的_mainScreen
对象需要跟踪它是否暴露的代码:
private boolean _isExposed = false;
protected void onExposed() {
super.onExposed();
_isExposed = true;
}
protected void onObscured() {
super.onObscured();
_isExposed = false;
}
public boolean isExposed() {
return _isExposed;
}
于 2012-06-14T10:00:57.753 回答