If you are using Internet Explorer, please remove blackberry.com from your compatibility view settings.

Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
arunbiet
Posts: 376
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer

Auto starting app when an sms comes

I am using this sample code to listen to incoming message.Everything is working fine.Now i want that even if i exit the app,and an sms comes,my application should start automatically instead of me starting the app.Please help.Below is my code

 

import java.io.IOException;
import javax.microedition.io.Connection;
import javax.microedition.io.Connector;
import javax.wireless.messaging.Message;
import javax.wireless.messaging.MessageConnection;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.container.MainScreen;

public class SMSReceiveTest extends UiApplication implements Runnable {

    private static final int SMS_PORT = 0;

    private final MainScreen _screen;
    private Connection _connection;

    public SMSReceiveTest() {
        this._screen = new MyMainScreen();
        this.pushScreen(this._screen);
    }

    private void log(final String message) {
        final RichTextField field = new RichTextField(message);
        this.invokeAndWait(new Runnable() {
            public void run() {
            	 UiApplication.getUiApplication().invokeAndWait(new Runnable() {
			 			public void run() {
			 				
			 				Dialog.alert(message);
			 			}});
                SMSReceiveTest.this._screen.add(field);
            }
        });
    }

    public void run() {
        while (!this.hasEventThread()) {
            Thread.yield();
        }

        this.log("Opening SMS connection on port " + SMS_PORT);
        final Connection con;
        try {
            con = Connector.open("sms://:" + SMS_PORT);
        } catch (final Exception e) {
            this.log("ERROR: Connector.open() threw " + e);
            return;
        }

        try {
            this.log("SMS connection opened successfully");
            this._connection = con;
            final MessageConnection mc = (MessageConnection) con;

            while (true) {
                this.log("Waiting to receive SMS message");
                final Message message;
                try {
                    message = mc.receive();
                } catch (final Exception e) {
                    this.log("ERROR: receive() threw " + e);
                    continue;
                }

                this.log("Received SMS from " + message.getAddress());
            }

        } finally {
            try {
                con.close();
            } catch (final IOException e) {
                // oh well
            }
        }
    }

    public static void main(String[] args) {
        final SMSReceiveTest app = new SMSReceiveTest();
        new Thread(app).start();
        app.enterEventDispatcher();
    }

    private class MyMainScreen extends MainScreen {

        public MyMainScreen() {
            this.setTitle("SMS Receive Test");
        }

        public void close() {
            if (SMSReceiveTest.this._connection != null) {
                try {
                    SMSReceiveTest.this._connection.close();
                } catch (final Exception e) {
                    // oh well
                }
            }
            super.close();
        }
    }

}

 

Please use plain text.
Developer
pradeep_ch
Posts: 543
Registered: ‎12-23-2010
My Device: Z10
My Carrier: Airtel

Re: Auto starting app when an sms comes

Hi Arun,

 

For this you have to do the following,

 

1. Create an application which extends from Application which listens for sms in background.

start this application when device starts(check auto run start up)

For this you have to use alternative entry point.

 

2. Now after receiving the sms you can open that Application from the above background thread.

 

Search in the forums for above two things.

 

Thanks.

 

 

Please use plain text.
Developer
arunbiet
Posts: 376
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer

Re: Auto starting app when an sms comes

thnk u pradeep.Will search the forum.If u have any code,kindly post.

Please use plain text.