BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 11-17-2008, 03: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, 03: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, 04: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, 04: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, 04: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





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