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
New Developer
Posts: 3
Registered: ‎12-13-2009
My Device: Bold
Accepted Solution

How to detect an incomming SMS ??? - HELPPPP

Hello everybody,

 

It's my first message in this forum. Until now, it help me a lot. But since few days, i had a problem. I want to detect an incomming SMS. I read some message and i found some parts of codes which are interesting. But, I didn't manage to do it.

Here is some links I read :

- http://www.j2mepolish.org/javadoc/j2me/de/enough/polish/messaging/MessageListener.html

- http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800451/800563/What_...

- http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800451/800563/How_To_...

- http://supportforums.blackberry.com/t5/Java-Development/notifyIncomingMessage-only-called-for-first-...

- http://supportforums.blackberry.com/t5/Java-Development/How-to-listen-to-incoming-sms-message-only/m...

I will stop here with links.

 

Now, here is my code :

 

/*
 * hw.java
 *
 * © <your company here>, 2003-2008
 * Confidential and proprietary.
 */

import java.util.*;
import java.lang.*;
import java.io.*;
import java.io.IOException;
import javax.microedition.location.*;
import javax.microedition.io.*;
import javax.microedition.io.file.*;
import javax.microedition.pim.*;
import javax.wireless.messaging.*;
import net.rim.device.api.io.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
import net.rim.device.api.i18n.*;
import net.rim.blackberry.api.invoke.*;

/**
 * 
 */
class hw extends UiApplication {
    public static void main(String[] args)
    {
        hw instance = new hw();
        instance.enterEventDispatcher();
    }  
    public hw() 
    {
        pushScreen(new SalutationScreen());    
    }
}

class SalutationScreen extends MainScreen implements Runnable //, MessageListener
{
    private Thread t;
    private MessageConnection _mc;
	
    public SalutationScreen()
    {
        super();        
        LabelField applicationTitle = 
            new LabelField("Hello World Title");
        setTitle(applicationTitle);
        RichTextField helloWorldTextField = new RichTextField("Hello World!");
        add(helloWorldTextField);
		
        t = new Thread(this);
        t.start();
/*
        try {
			_mc = (MessageConnection)Connector.open("sms://:0");
			_mc.setMessageListener(this);
			helloWorldTextField = new RichTextField("setMessageListener : done");
			add(helloWorldTextField);                
		}
        catch (IOException e) {
            helloWorldTextField = new RichTextField("erreur constructeur : " + e);
            add(helloWorldTextField);                
        }
*/
    }
	
    public boolean onClose()
    {
        Dialog.alert("Bye World!");    
        System.exit(0);
                return true;
    }
    
    public void run()
    {            
        try{
			DatagramConnection _dc = (DatagramConnection)Connector.open("sms://:0");

			for(;;)
			{
				Datagram d = _dc.newDatagram(_dc.getMaximumLength());                        
				_dc.receive(d);
				byte[] bytes = d.getData();
				String address = d.getAddress();
				String msg = new String(bytes);
				
				helloWorldTextField = new RichTextField("  Message text received : " + msg );
				add(helloWorldTextField);            
			}
        }
        catch(Exception e){}
		
/* METHOD 2
		while(true)     
		{ 
			try 
			{
				DatagramConnection _dc = (DatagramConnection)Connector.open("sms://"); 
				Datagram d = _dc.newDatagram(_dc.getMaximumLength()); 
				_dc.receive(d);
				String address = new String(d.getAddress()); 
				String msg = new String(d.getData()); 
				
				helloWorldTextField = new RichTextField("Message received: " + msg + "\n" + address);
				add(helloWorldTextField);                                
				Alert.startVibrate(3000);
			} 
			catch (IOException e) 
			{
					System.err.println(e.toString()); 
			} 
		}
*/           
 
/*	METHOD 3
		try {
			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();
				}
				else if (m instanceof BinaryMessage) {
			
					StringBuffer buf = new StringBuffer();
					byte[] data = ((BinaryMessage) m).getPayloadData();
					// convert Binary Data to Text
					msg = new String(data, "UTF-8");
				}
				else
					System.out.println("Invalid Message Format");
					
				RichTextField helloWorldTextField = new RichTextField("Received SMS text from " + address + "\n" + msg);
				add(helloWorldTextField);
			}
	}
	catch (IOException e) 
	{
			System.err.println(e.toString()); 
	}
*/
    }
    
/*  METHOD 4      
        public void notifyIncomingMessage(MessageConnection conn) {
            RichTextField helloWorldTextField = new RichTextField("notifyIncomingMessage DEBUT");
            add(helloWorldTextField);                
            
            try {
                Message m = conn.receive();
                helloWorldTextField = new RichTextField("- Message recu");
                add(helloWorldTextField);                
                
                String address = m.getAddress();
                String msg = null;
				
				if ( m instanceof TextMessage )
				{
					TextMessage tm = (TextMessage)m;
					msg = tm.getPayloadText();
				}
				else if (m instanceof BinaryMessage) {
					StringBuffer buf = new StringBuffer();
					byte[] data = ((BinaryMessage) m).getPayloadData();
		
					// convert Binary Data to Text
					msg = new String(data, "UTF-8");
				}
				else
					System.out.println("Invalid Message Format");
                    
                helloWorldTextField = new RichTextField("- Message de : " + address + " // \""+ msg + "\"");
                add(helloWorldTextField);                                
            }        
            catch (IOException e) {
                helloWorldTextField = new RichTextField("erreur _mc.receive : " + e);
                add(helloWorldTextField);                
            }
            
        }
*/

/*  METHOD 5
		public void notifyIncomingMessage(MessageConnection messageConnection)
		{       
			new Thread()
			{
				MessageConnection conn;
				Thread set(MessageConnection conn)
				{
					this.conn = conn;
					return (this);
				}
				public void run()
				{
					try
					{
						Message m = conn.receive();
						String msg = null;
						
						if ( m instanceof TextMessage) 
						{
							TextMessage tm = (TextMessage) m;
							msg = tm.getPayloadText();
						
							RichTextField helloWorldTextField = new RichTextField("- Message de : " + m.getAddress() + " // \""+ msg + "\"");
							this.add(helloWorldTextField); 
						}
					}
					catch (Exception e)
					{
					}
				}
			}.set(messageConnection).start();
		}   
*/      
}

 

In my code, i tested 5 methods. But, i think each of them blocked with the line where there is "received()" method.

 

 

If there is someone who can help me to find my problem, it will be great. (Or if there is someone who want to send me a full code which works, it will be better than great :smileyvery-happy:)

 

Thank you for all answers.

New Developer
Posts: 3
Registered: ‎12-13-2009
My Device: Bold

Re: How to detect an incomming SMS ??? - HELPPPP

Hello,

 

Just to keep you informed, I found a solution.

 

Thanks at all.

Developer
Posts: 16,679
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How to detect an incomming SMS ??? - HELPPPP

please mark the thread as solved (and, if possible, tell a bit about your solution, just in case somebody else has the same question).

 

just reading your code: use invokelater to access the ui thread. _dc.received is blocking per design.

----------------------------------------------------------
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
New Developer
Posts: 3
Registered: ‎12-13-2009
My Device: Bold

Re: How to detect an incomming SMS ??? - HELPPPP

I agree with you. It's better if I post a solution.

 

Then, I re-wrote an other program which gets the phone number and the content of the incomming SMS.

 

Here's my code

 

 

public class smsReceived extends UiApplication 
{
    private LabelField _status;
    private DatagramConnection _dc;
    
    public static void main(String[] args)
    {
        new smsReceived().enterEventDispatcher();
    }

    public smsReceived()
    {
        super();      
        waitASms();
   }
    
    public void waitASms() 
    { 
		try 
		{ 
			_dc = (DatagramConnection)Connector.open("sms://"); 
			for(;;) 
			{ 
				Datagram d = _dc.newDatagram(_dc.getMaximumLength()); 
				_dc.receive(d);
				String address = new String(d.getAddress()); 
				String msg = new String(d.getData()); 
			} 
		} 
		catch (IOException e) 
		{
			System.err.println(e.toString()); 
		} 
    }
}

 

In this code, the string "address" is the phone number and the string "msg" is the content.

To display, I let you do an other method with this strings.

 

See you.

 

Developer
Posts: 16,679
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How to detect an incomming SMS ??? - HELPPPP

put networking on an own thread. if you don't want to do UI you can extend application instead.

----------------------------------------------------------
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
Developer
Posts: 109
Registered: ‎02-09-2009
My Device: Not Specified

Re: How to detect an incomming SMS ??? - HELPPPP

Do you have any code for how you can integrate this into an existing UiApplication?