2012-06-14 49 views
0

如何在黑莓的瀏覽器上使用事件瀏覽器關閉瀏覽器。當瀏覽器加載時,我想模擬手持設備上按ESCAPE鍵,以便應用程序退出瀏覽器並返回主屏幕。我自己嘗試過這樣做,但沒有成功。非常感激任何的幫助。瀏覽器上的EventInjector

回答

2

如果你真的想要控制瀏覽器,你可以在你的應用中使用BrowserFieldBrowserField2

您還可以爲按鍵或追蹤哪些應用程序可見的注入偵聽器。但它會非常棘手,因爲用戶經常在應用程序之間切換,並且現在有相當多的帶有觸摸界面的設備(用戶可以在沒有esc按鈕的情況下關閉頁面)。

1

不確定爲什麼要關閉瀏覽器,但我會假設你知道這是正確的事情(另外,Eugen已經建議你如何使用BrowserField讓用戶在你的應用程序中瀏覽並避免這個問題)。

無論如何,我有一些代碼,我用來關閉相機(我的應用程序確實啓動,故意)。您可以用相同的方式關閉瀏覽器。這是一個黑客,但在當時,這是我解決了這個問題的辦法:

/** 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; 
} 
相關問題