BlackBerry Forums Support Community
              

Closed Thread
 
LinkBack Thread Tools
Old 11-17-2008, 02:00 AM   #1 (permalink)
New Member
 
Join Date: Nov 2008
Location: Colombo
Model: 8300
PIN: N/A
Carrier: Dialog
Posts: 5
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default listening for SMS on a port

Please Login to Remove!

Hi guys,
i need to listen to a port for an SMS in a background thread. then push a screen when a meesage is recieved.

im developing a UiApplication, not a Midlet.

I tried the alternate entry point approach. I don't get any errors, but I don't know how to start the GUI part and push a screen to display the received message.

here's my UiApplication class:

Code:
public class App extends UiApplication {
    ViewMessage mMainMenu;
    //MessageThread myThread;
    App() {
        mMainMenu = ViewMessage.createMainMenuInstance();
        pushScreen(new HelloWorldScreen());
    }
    
    
    public static void main(String[] args) {
        if(args != null && args.length > 0 && args[0].equals("gui"))
        {
            App myApp = new App();
            myApp.enterEventDispatcher();
        }
        else
        {
            MessageThread myThread = new MessageThread();          
            if(myThread == null)
            {
                //impossible to create thread... so exit
                System.exit(-1);
            }
            else
            {
                try
                {
                    Thread.sleep(2000);
                    myThread.start();
                }
                catch(Throwable ie)
                {
                    
                }
            }
        }
    }
    
}
the background thread:
Code:
class MessageThread extends Thread {
    String url = "sms://:50001";
    MessageConnection currentMessageConnection;
    private boolean _stop = false;
    
    MessageThread() {
        try {   
            currentMessageConnection = (MessageConnection) Connector.open(url);
        }catch(Exception e) {
            System.out.println(e.toString());
        }
    }
    
    public synchronized void stop() {
    _stop = true;
    try {
        currentMessageConnection.close(); // Close the connection so the thread returns.
    } catch (Exception e) {
        System.err.println(e.toString());
    }
}

    public void run() {
        while(true) {
            try {
                if ( _stop ) {
                    return;
                } 
                TextMessage txtMsg = (TextMessage)currentMessageConnection.receive();
                String data = txtMsg.getPayloadText();
                String address = txtMsg.getAddress();
                address = address.substring(6);
                PixMeMessage pixMeMessage = (new PixMeMessage()).insert(data, address);
                //start the gui part and push a screen to display the message.
            }catch(Exception e) {
                System.out.println(e.toString());
                try {
                    //Thread.currentThread.wait(1000);
                }catch(Exception ex) {
                    System.out.println(ex.toString());
                }
            }
        }   
    }
}
any idea how to achieve this?
Offline  
Old 11-17-2008, 02:34 AM   #2 (permalink)
CrackBerry Addict
 
Join Date: Apr 2005
Location: hamburg, germany
Model: 8900
Carrier: o2
Posts: 838
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

there is a sample with the jde.
just looking over your code i see two possible issues:
- if you listen to a specific port you'll get only messages sent to that port, normal sms are not intercepted
- you may have to synchronize on the eventlock/use invokelater to update the UI
__________________
java developer, Devinto, hamburg/germany
Offline  
Old 11-17-2008, 03:10 AM   #3 (permalink)
Knows Where the Search Button Is
 
Join Date: Apr 2008
Model: 7100T
PIN: N/A
Carrier: optus
Posts: 24
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

such a common question I blogged it, just wrap the code in a thread and your done
Offline  
Old 11-17-2008, 03:40 AM   #4 (permalink)
New Member
 
Join Date: Nov 2008
Location: Colombo
Model: 8300
PIN: N/A
Carrier: Dialog
Posts: 5
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by simon.hain View Post
there is a sample with the jde.
just looking over your code i see two possible issues:
- if you listen to a specific port you'll get only messages sent to that port, normal sms are not intercepted
- you may have to synchronize on the eventlock/use invokelater to update the UI
hi simon,
yes, I only want the SMSs coming to this port. can you please elaborate on how to synchronize on the eventlock/use invokelater.


thanks
Offline  
Old 11-17-2008, 03:44 AM   #5 (permalink)
New Member
 
Join Date: Nov 2008
Location: Colombo
Model: 8300
PIN: N/A
Carrier: Dialog
Posts: 5
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by skynetchris View Post
such a common question I blogged it, just wrap the code in a thread and your done
hi skynetchris,
thanks for the reply. my requirement is a bit complex. I need to have a thread running in the background even when the application is not running, to listen to SMSs. when a SMS is received on the port, this background thread must start the application to display the SMS.
Offline  
Closed Thread


Thread Tools

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

OEM CPU FAN & HEATSINK 14R0011 TSQC0039003--IBM SURE POS 4840-563 TOUCH SCREEN
$3.99
OEM CPU FAN & HEATSINK 14R0011 TSQC0039003--IBM SURE POS 4840-563 TOUCH SCREEN pictureIBM Selectric 72 Typewriter Ribbon 1136138 OEM 8 Ribbon Cartridges
$84.99
IBM Selectric 72 Typewriter Ribbon 1136138 OEM 8 Ribbon Cartridges pictureOEM RIBBON for IBM 463 BLACK # 1 299 463
$5.0
OEM RIBBON for IBM 463  BLACK # 1 299 463 pictureNew OEM Genuine IBM 75P6877 Laser Infoprint 1585 Toner Cartridge
$85.0
New OEM Genuine IBM 75P6877 Laser Infoprint 1585 Toner Cartridge pictureNEW IBM SELECTRIC III TYPEWRITER PART, RED MARGIN POINTER OEM NOS
$4.99
NEW IBM SELECTRIC III TYPEWRITER PART, RED MARGIN POINTER OEM NOS  picture






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