BlackBerry Forums Support Community
              

Closed Thread
 
Thread Tools
Old 01-08-2009, 01:54 PM   #1
hijame
New Member
 
Join Date: Jan 2009
Model: 8100
PIN: N/A
Carrier: VodaPhone
Posts: 6
Question How to Listen to Key Pressed in background Application- Help

Please Login to Remove!

When I hide my UIApplication (by press Red Phone Key or invoke requestBackground() ), I cannot catch the event when keypressed. I implemented KeyListener from MainScreen and it works fine when the Application is not in background mode, but not after I hide my App.
Any solutions for this ?. I'll appreciate any help.
Offline  
Old 01-08-2009, 02:26 PM   #2
bemshaswing
Talking BlackBerry Encyclopedia
 
Join Date: Oct 2006
Model: 7103
Carrier: Verizon
Posts: 259
Default

override this method in your application to catch the event.
Offline  
Old 01-08-2009, 02:49 PM   #3
hijame
New Member
 
Join Date: Jan 2009
Model: 8100
PIN: N/A
Carrier: VodaPhone
Posts: 6
Default

Could you please explain it in detail? I overrided deactivate(), but I have no idea how implement it to catch event. My Application source code is below, it's pretty simple. Have I missed something ?

PHP Code:
package AppHello;

import net.rim.device.api.system.KeyListener;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Keypad;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.container.MainScreen;

public class 
HelloWorld extends UiApplication {
    private 
BackGroundApp backGroundApp;
    private 
HelloWorldScreen mScreen;
    private 
HelloWorld() {

        
mScreen = new HelloWorldScreen();  
        
pushScreen(mScreen);
    }
    
     
     }
    
/**
     * @param args
     */
    
public static void main(Stringxxx91;xxx93; args) {
        
// TODO Auto-generated method stub
        
HelloWorld app = new HelloWorld();
        
app.enterEventDispatcher();
        
    }
    public 
void deactivate() {
        
// TODO Auto-generated method stub
        
super.deactivate();
    }

}
final class 
HelloWorldScreen extends MainScreen
{
    
HelloWorldScreen()
    {
        
setTitle(new LabelField("BBerry",LabelField.ELLIPSIS));
        
add(new RichTextField("Config Screen",Field.NON_FOCUSABLE));
    }

    public 
void close() {
        
// TODO Auto-generated method stub
        
Dialog.alert("Bye");
        
System.exit(0);
        
super.close();
    }

    protected 
boolean keyDown(int arg0int arg1) {
        
// TODO Auto-generated method stub

        
Dialog.alert(String.valueOf(Keypad.key(arg0)));
        
System.out.println(String.valueOf(Keypad.key(arg0)));
        return 
super.keyDown(arg0arg1);
    }

    protected 
boolean keyChar(char cint statusint time) {
        
// TODO Auto-generated method stub

        
return super.keyChar(cstatustime);    
    }
    

Offline  
Old 01-08-2009, 02:54 PM   #4
bemshaswing
Talking BlackBerry Encyclopedia
 
Join Date: Oct 2006
Model: 7103
Carrier: Verizon
Posts: 259
Default

well it depends on what you would want to do with it

public void deactivate() {
//You would 'catch' and do whatever you want right here
super.deactivate();
}
Offline  
Old 01-08-2009, 03:01 PM   #5
hijame
New Member
 
Join Date: Jan 2009
Model: 8100
PIN: N/A
Carrier: VodaPhone
Posts: 6
Default

Well, I know I must catch events in that deactivate block, but im only beginner with Java and I really don't know how to 'catch' within a method? For example, if I want to print to Console output the key which user pressed in App Home Screen, then what should I write in 'deactivate()'. Could you provide me a sample ?
Offline  
Old 01-08-2009, 04:05 PM   #6
Dougsg38p
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
Default

A key listener attached to the Screen is absolutely not going to "hear" any keys when the screen is not being displayed.

There is also a addKeyListener() method on the Application class - I have not tried this but looks like it is worth a try.
Offline  
Old 01-08-2009, 08:57 PM   #7
hijame
New Member
 
Join Date: Jan 2009
Model: 8100
PIN: N/A
Carrier: VodaPhone
Posts: 6
Default

I've tried that already, the problem still exist. When the application is sent to background, not only the mainscreen is hiddden, also the main application is deactivated.
So I think adding a KeyListener() to the main app is not going to work.
Even I try to make a background thread which only 'hear' for the key events, It still not work when the app is minimized (ONLY when it's minimized).
Anyone knows why? Maybe the AppHomeScreen also has a listener and it intercept my app from catching the events ?
Offline  
Old 01-08-2009, 09:59 PM   #8
hijame
New Member
 
Join Date: Jan 2009
Model: 8100
PIN: N/A
Carrier: VodaPhone
Posts: 6
Default

After debugging, I found out that my background thread is still running and listening for key pressed. But it only catch the key "Red Phone". When I press any other key, the focus will change to Phone App, that means the Phone App keeping my thread from listening to events ? And because the "Red Phone" key does not have any function in Phone App so my thread was able to catch it.
any ideas ?
Offline  
Old 01-09-2009, 03:34 AM   #9
simon.hain
CrackBerry Addict
 
Join Date: Apr 2005
Location: hamburg, germany
Model: 8900
Carrier: o2
Posts: 838
Default

a background app does not get any navigation events.
__________________
java developer, Devinto, hamburg/germany
Offline  
Old 01-09-2009, 10:19 AM   #10
hijame
New Member
 
Join Date: Jan 2009
Model: 8100
PIN: N/A
Carrier: VodaPhone
Posts: 6
Default

so is there any approach to this problem ? Or the Blackberry JDE is not sophisticated enough to make this happens ? It seems not simple as I thought. :(
Offline  
Closed Thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


Lot of 6 Vintage Apple Drawstring Bags For iPhone, Mac, Apple Watch, iPod picture

Lot of 6 Vintage Apple Drawstring Bags For iPhone, Mac, Apple Watch, iPod

$45.00



Retro 1951 Apple Computer Logo 0.7mm Ballpoint Black Ink Pen Gold Barrel picture

Retro 1951 Apple Computer Logo 0.7mm Ballpoint Black Ink Pen Gold Barrel

$56.41



Vintage Red Apple Six Drawer Storage Organizer File Cabinet  picture

Vintage Red Apple Six Drawer Storage Organizer File Cabinet

$35.00



Vintage  Rainbow Logo Apple Post-It Sticky Notes 4

Vintage Rainbow Logo Apple Post-It Sticky Notes 4" Rare computer collectible

$39.99



C2102A-4 INTEL VINTAGE 1K SRAM 1024Bit CERAMIC GOLD PINS APPLE 1, DC1976 Mimeo-1 picture

C2102A-4 INTEL VINTAGE 1K SRAM 1024Bit CERAMIC GOLD PINS APPLE 1, DC1976 Mimeo-1

$31.15



VINTAGE 1994 Apple Black Toner Laser Writer Cartridge M 2473 G/A NEW SEALED picture

VINTAGE 1994 Apple Black Toner Laser Writer Cartridge M 2473 G/A NEW SEALED

$25.95







Copyright © 2004-2016 BlackBerryForums.com.
The names RIM © and BlackBerry © are registered Trademarks of BlackBerry Inc.