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
Contributor
herahadi
Posts: 27
Registered: ‎02-20-2013
My Device: 9900

Receive SMS Message on blackberry and show it to screen

hello.. i'm new on bb development. i want to create app that listen to sms message and then show it to screen using labelfield.

 

i try to implement code from this link http://www.blackberry.com/developers/docs/7.1.0api/Messaging-summary.html#MG_3A

 

and here is my code:

 

package smslistening;

import javax.microedition.io.Connector;
import javax.wireless.messaging.Message;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;

import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;

public class PesanScreen extends MainScreen{
	LabelField pesan;
public PesanScreen(){
	
	super();
	setTitle("sms receiver");
	
	 
	
	MessageConnection _mc = (MessageConnection)Connector.open("sms://:0");  
	   
	for(;;) 
	{ 
	    Message m = _mc.receive(); 
	    String address = m.getAddress(); 
	    String msg = null; 
	 
	    if ( m instanceof TextMessage ) 
	    { 
	        TextMessage tm = (TextMessage)m; 
	        msg = tm.getPayloadText(); 
	        pesan = new LabelField(msg);
	    } 
	    else if (m instance of BinaryMessage) 
	    { 
	        StringBuffer buf = new StringBuffer(); 
	        byte[] data = ((BinaryMessage) m).getPayloadData(); 
	        // convert Binary Data to Text 
	        msg = new String(data, "UTF-8");    
	        pesan = new LabelField(msg);
	    } 
	    else 
	    {
	       // System.out.println("Invalid Message Format"); 
	       // System.out.println("Received SMS text from " + address  + " : " + msg); 
	        pesan = new LabelField("invalid format");
	    }
	}
	   
	
	
	
}

}

code above show me an error on eclipse that said "BinaryMessage" cannot be resolve to a type.

 

please help me how i can implement the sample code in this link http://www.blackberry.com/developers/docs/7.1.0api/Messaging-summary.html#MG_3A ??

Please use plain text.
Developer
simon_hain
Posts: 16,137
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Receive SMS Message on blackberry and show it to screen

you should not implement blocking code in the UI, see http://supportforums.blackberry.com/t5/Java-Development/What-is-the-Event-Thread/ta-p/446865

use a separate thread to execute the receiver, you can use invokelater to modify the UI from there.

you should be able to import BinaryMessage using organize imports, its in the standard api:
http://www.blackberry.com/developers/docs/7.1.0api/javax/wireless/messaging/BinaryMessage.html
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Contributor
herahadi
Posts: 27
Registered: ‎02-20-2013
My Device: 9900

Re: Receive SMS Message on blackberry and show it to screen

thank you for your reply.. but i still can't understand it.

 

should i put the code on separate class? and how to call it from main class? please help me with complete code

Please use plain text.
Developer
simon_hain
Posts: 16,137
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Receive SMS Message on blackberry and show it to screen

you can use code like that:
new Thread(){public void run(){xxx}}.start();
put the code you want to run instead of xxx
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
peter_strange
Posts: 19,604
Registered: ‎07-14-2008
My Device: Not Specified

Re: Receive SMS Message on blackberry and show it to screen

There is a bit of learning curve to understand Blackberry coding and the way you do things. 

 

I for one think sample code is useful to illustrate specific points, but in this case any sample code you got would most likely confuse rather than enlighten.  Giving you a full sample at this stage is a little like asking you to learn to drive in a Formula 1 car. 

 

So I think you need to get a basic application working, then extend it.

 

One of the main things you need to understand is Threading in general and the Blackberry Event Tread specifically.  Threading is a standard, so most books and courses will talk about it.  I presume that you know how to do Threading.

 

The Event Thread is a UI construct similar to that used in Swing (and in fact in Cascades for BB10 but that is a different story).  Here is a good introduction to it:

http://supportforums.blackberry.com/t5/Java-Development/What-is-the-Event-Thread/ta-p/446865

You will find more if you search.

 

Now in your case you have a background process that you want to use to update the user Interface.  The place I suggest you start doing this is httpDemo, which is a sample that should come with your SDK.  So you can review that to understand a little more about the Event Thread and Threading in general.  Then play with it to make changes. 

 

Once you have that under your belt, then you can look at replacing the network interaction with your SMS interaction. 

 

I recommend that you spend a lot of time reviewing the developer document too.  The new web site is actually very good, significantly better than when I started!  Start here

http://developer.blackberry.com/java/

 

 

Please use plain text.