08-07-2009 12:32 PM
Hi dev community.
I'm being tossed into Blackberry development and trying to develop a technical proof of concept.
My toolset: I am using the Blackberry JDE Plugin for eclipse v1.0.0.0.67, and deploying to the default simulator for the plugin.
My Intent: I want to make an application which gets sent to the background, and then gets brought back to the foreground by a double-tap of the Power key.
My Problem: The code below appears to work very well. I can start the simulator, run the app, select the "Hide Application" menu item, and then double-tap the Power key The double-tap of the Power key does very briefly bring the application to the foreground (see bottom of my message for the console output proving that's true). But then it is immediately backgrounded again by something further down the event stack.
My Question: Can anyone please assist me in getting that .requestForeground() call on line 72 to happen after all the other events have triggered?
Thank you for reading (and hopefully responding to) my question.
My source code:
1: package com.example.ForeAndBackTest; 2: 3: import net.rim.device.api.system.KeyListener; 4: import net.rim.device.api.ui.*; 5: import net.rim.device.api.ui.component.*; 6: import net.rim.device.api.ui.container.*; 7: 8: 9: public class ForeAndBackApp extends UiApplication { 10: 11: private MainScreen mainScreen; 12: private RichTextField helloWorld; 13: private LabelField myLabel; 14: private KeyThread listener; 15: private HideMenuItem hideMenu; 16: 17: public static void main(String[] args) 18: { 19: 20: ForeAndBackApp app = new ForeAndBackApp(); 21: app.enterEventDispatcher(); 22: 23: } 24: 25: public ForeAndBackApp() 26: { 27: 28: listener = new KeyThread(this); 29: this.addKeyListener(listener); 30: 31: myLabel = new LabelField("ForeAndBackApp", LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH); 32: helloWorld = new RichTextField("Hello, World!", RichTextField.FIELD_VCENTER | RichTextField.FIELD_HCENTER | RichTextField.READONLY); 33: hideMenu = new HideMenuItem("Hide Application", 999, 999); 34: 35: mainScreen = new MainScreen(); 36: mainScreen.addMenuItem(hideMenu); 37: mainScreen.setTitle(myLabel); 38: mainScreen.add(helloWorld); 39: 40: pushScreen(mainScreen); 41: 42: } 43: 44: private class KeyThread extends Thread implements KeyListener 45: { 46: 47: private UiApplication hostApp; 48: private int lastDown; 49: 50: public KeyThread(UiApplication app) 51: { 52: hostApp = app; 53: } 54: 55: public boolean keyChar(char arg0, int arg1, int arg2) { 56: return false; 57: } 58: 59: public boolean keyDown(int arg0, int arg1) { 60: 61: // proceed if we're background and the Power button was pressed. 62: if ( (! hostApp.isForeground()) && (arg0 == 1179648)) 63: { 64: 65: System.out.println("### Power key caught in background mode. Last: " + lastDown + " || Current: " + arg1); 66: 67: // proceed if second press within a second 68: if ((arg1 - lastDown) < 1000) 69: { 70: 71: System.out.println("### Moving application to foreground"); 72: hostApp.invokeLater(new Runnable(){public void run(){UiApplication.getUiApplication().requestFore
ground();}}); 73: 74: } 75: 76: lastDown = arg1; 77: 78: return true; 79: 80: } 81: else 82: { 83: return false; 84: 85: } 86: 87: } 88: 89: public boolean keyRepeat(int arg0, int arg1) { 90: return false; 91: } 92: 93: public boolean keyStatus(int arg0, int arg1) { 94: return false; 95: } 96: 97: public boolean keyUp(int arg0, int arg1) { 98: return false; 99: } 100: 101: } 102: 103: private class HideMenuItem extends MenuItem 104: { 105: 106: public HideMenuItem(String arg0,int arg1,int arg2) 107: { 108: super(arg0, arg1, arg2); 109: } 110: 111: public void run() { 112: 113: System.out.println("### Moving application to background"); 114: UiApplication.getUiApplication().requestBackground (); 115: 116: } 117: 118: } 119: 120: }
My console output:
Starting ForeAndBackTest Started ForeAndBackTest(124) Foreground ForeAndBackTest(124) FocusHistory: Focus gained; App Home Screen; Component net.rim.device.apps.internal.ribbon.launcher.Ribbo
nIconField FocusHistory: Focus lost; App Home Screen; Component net.rim.device.apps.internal.ribbon.launcher.Ribbo nIconField FocusHistory: Focus gained; App ForeAndBackTest; Component net.rim.device.api.ui.component.RichTextField FocusHistory: Focus lost; App ForeAndBackTest; Component net.rim.device.api.ui.component.RichTextField FocusHistory: Focus gained; App ForeAndBackTest; Component net.rim.device.api.ui.menu.DefaultMenuListField FocusHistory: Focus lost; App ForeAndBackTest; Component net.rim.device.api.ui.menu.DefaultMenuListField ### Moving application to background Foreground net_rim_bb_ribbon_app(69) FocusHistory: Focus gained; App ForeAndBackTest; Component net.rim.device.api.ui.component.RichTextField FocusHistory: Focus gained; App Home Screen; Component net.rim.device.apps.internal.ribbon.launcher.Ribbo nIconField FocusHistory: Focus lost; App ForeAndBackTest; Component net.rim.device.api.ui.component.RichTextField FocusHistory: Focus lost; App Home Screen; Component net.rim.device.apps.internal.ribbon.launcher.Ribbo nIconField FocusHistory: Focus gained; App Home Screen; Component net.rim.device.api.ui.MediaController ### Power key caught in background mode. Last: 0 || Current: 82351 ### Power key caught in background mode. Last: 82351 || Current: 82492 ### Moving application to foreground Foreground ForeAndBackTest(124) FocusHistory: Focus gained; App ForeAndBackTest; Component net.rim.device.api.ui.component.RichTextField FocusHistory: Focus lost; App Home Screen; Component net.rim.device.api.ui.MediaController Foreground net_rim_bb_ribbon_app(69) FocusHistory: Focus lost; App ForeAndBackTest; Component net.rim.device.api.ui.component.RichTextField FocusHistory: Focus gained; App Home Screen; Component net.rim.device.api.ui.MediaController
Thanks again for your help.
Darryl.
08-10-2009 09:19 AM
Hi again,
I am reposting my source code without the line numbers. I find them helpful for discussion, but it makes cutting and pasting difficult. So, here it is again.
I appreciate any help you can offer.
package com.example.ForeAndBackTest; import net.rim.device.api.system.KeyListener; import net.rim.device.api.ui.*; import net.rim.device.api.ui.component.*; import net.rim.device.api.ui.container.*; public class ForeAndBackApp extends UiApplication { private MainScreen mainScreen; private RichTextField helloWorld; private LabelField myLabel; private KeyThread listener; private HideMenuItem hideMenu; public static void main(String[] args) { ForeAndBackApp app = new ForeAndBackApp(); app.enterEventDispatcher(); } public ForeAndBackApp() { listener = new KeyThread(this); this.addKeyListener(listener); myLabel = new LabelField("ForeAndBackApp", LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH); helloWorld = new RichTextField("Hello, World!", RichTextField.FIELD_VCENTER | RichTextField.FIELD_HCENTER | RichTextField.READONLY); hideMenu = new HideMenuItem("Hide Application", 999, 999); mainScreen = new MainScreen(); mainScreen.addMenuItem(hideMenu); mainScreen.setTitle(myLabel); mainScreen.add(helloWorld); pushScreen(mainScreen); } private class KeyThread extends Thread implements KeyListener { private UiApplication hostApp; private int lastDown; public KeyThread(UiApplication app) { hostApp = app; } public boolean keyChar(char arg0, int arg1, int arg2) { return false; } public boolean keyDown(int arg0, int arg1) { // proceed if we're background and the Power button was pressed. if ( (! hostApp.isForeground()) && (arg0 == 1179648)) { System.out.println("### Power key caught in background mode. Last: " + lastDown + " || Current: " + arg1); // proceed if second press within a second if ((arg1 - lastDown) < 1000) { System.out.println("### Moving application to foreground"); hostApp.invokeLater(new Runnable(){public void run(){UiApplication.getUiApplication().requestFore
ground();}}); } lastDown = arg1; return true; } else { return false; } } public boolean keyRepeat(int arg0, int arg1) { return false; } public boolean keyStatus(int arg0, int arg1) { return false; } public boolean keyUp(int arg0, int arg1) { return false; } } private class HideMenuItem extends MenuItem { public HideMenuItem(String arg0,int arg1,int arg2) { super(arg0, arg1, arg2); } public void run() { System.out.println("### Moving application to background"); UiApplication.getUiApplication().requestBackground (); } } }
Thank you again,
Darryl.