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
Highlighted
New Developer
Posts: 13
Registered: ‎12-14-2009
My Device: 9000
My Carrier: Idea

Can't get the error resolved for using Custom SMS application.

[ Edited ]

Hi I am trying to developed a custom SMS application. I got some code snippet from other posts but I am still facing problem implementing the code. I tried two methods

 

Method 1:

 public class EnterNumber extends MainScreen {
 LabelField titleEnterNumber = new LabelField("Enter Number",LabelField.ELLIPSIS);
 HorizontalFieldManager hfm2 = new HorizontalFieldManager();
 HorizontalFieldManager hfm3 = new HorizontalFieldManager();
 BasicEditField msgno = new BasicEditField("Enter No:",null,10,TextField.PHONENUMBER);
 public EnterNumber(){
  
  setTitle(titleEnterNumber);
    
  
  hfm2.add(msgno);
  
  
  add(new SeparatorField());
  add(hfm2);
  
 }
 protected void makeMenu(Menu menu, int instance){
  menu.add(sendSms);
 }
 
 
 private MenuItem sendSms = new MenuItem("Send",110,10){
   public void run(){
   Dialog.alert("inside run");
   sendSMS();  }
 };
 
 public void sendSMS(){
  Dialog.alert("inside sms");
  String address = "sms://"+msgno.getText();
  Dialog.alert(address);
  System.out.println(address);
    
   MessageConnection smsconn = null;  
    
   try {  
      smsconn = (MessageConnection) Connector.open(address);  
    
      TextMessage txtmessage = (TextMessage) smsconn.newMessage(MessageConnection.TEXT_MESSAGE);  
      txtmessage.setPayloadText("SMS sending suceessfull");  
     Dialog.alert("Messge is loaded");
     System.out.println("Messge is loaded");
      smsconn.send(txtmessage);  
     Dialog.alert("Message sent");
     System.out.println("Message sent");
   } catch (Exception t) {  
      System.out.println(t.getMessage());
      Dialog.alert(t.getMessage());
      System.out.println(t.getClass().getName());  
   }  
    
   if (smsconn != null) {  
      try {  
         smsconn.close();  
      } catch (IOException ioe) {  
         System.out.println(ioe.getMessage());         }  
   }  }

 

Using this I get an error alert after setPayloadText(): saying "operation prohibited bloacking operation on event despacher thread".

 

So I used the Method 2 trying to run it in a thread:

 

 public class EnterNumber extends MainScreen implements Runnable{
 LabelField titleEnterNumber = new LabelField("Enter Number",LabelField.ELLIPSIS);
 HorizontalFieldManager hfm2 = new HorizontalFieldManager();
 HorizontalFieldManager hfm3 = new HorizontalFieldManager();
 BasicEditField msgno = new BasicEditField("Enter No:",null,10,TextField.PHONENUMBER);
 Thread t;
 public EnterNumber(){
  t = new  Thread(this);
  setTitle(titleEnterNumber);
    
  
  hfm2.add(msgno);
  
  
  add(new SeparatorField());
  add(hfm2);
  
 }
 protected void makeMenu(Menu menu, int instance){
  menu.add(sendSms);
 }
 
 
 private MenuItem sendSms = new MenuItem("Send",110,10){
   public void run(){
   Dialog.alert("inside run");
   t.start();
  }
 };
 
 public void run(){
  Dialog.alert("inside sms");
  String address = "sms://"+msgno.getText();
  Dialog.alert(address);
  System.out.println(address);
    
   MessageConnection smsconn = null;  
    
   try {  
      smsconn = (MessageConnection) Connector.open(address);  
    
      TextMessage txtmessage = (TextMessage) smsconn.newMessage(MessageConnection.TEXT_MESSAGE);  
      txtmessage.setPayloadText("SMS sending suceessfull");  
     Dialog.alert("Messge is loaded");
     System.out.println("Messge is loaded");
      smsconn.send(txtmessage);  
     Dialog.alert("Message sent");
     System.out.println("Message sent");
   } catch (Exception t) {  
      System.out.println(t.getMessage());
      Dialog.alert(t.getMessage());
      System.out.println(t.getClass().getName());  
   }  
    
   if (smsconn != null) {  
      try {  
         smsconn.close();  
      } catch (IOException ioe) {  
         System.out.println(ioe.getMessage());         }  
   }  }}

 

I only get one alert: "Inside run". After that no sms is sent, no error, no alert, no exception please help.

 

I am using following things for developing the application:

 

  1. I am testing on BB 9000 handsets.
  2. Using blackberry 4.6 plugin for Eclipse.
  3. Error I am getting: Already mentioned with code.
  4. Code: Already given both the implementation techniques.
Contributor
Posts: 12
Registered: ‎09-16-2010
My Device: 9000
My Carrier: t-mobile

Re: Can't get the error resolved for using Custom SMS application.

try this code.. change the port number accordingly..

package com.he.message.interceptor;

/*

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

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

/**
 *
 */
public class SMSSender extends Thread {

    /*
     * public static void main(String args[]){ new SMSSender(); }
     */

    private String _mobileNo;
    private String _message;

    public SMSSender() {
        Thread t = new Thread(this);
        t.start();

    }

    public SMSSender(String mobileNo, String message) {
        this._mobileNo = mobileNo;
        this._message = message;
        Thread t = new Thread(this);
        t.start();

    }

    public void run() {
        try {

            /*
             * VerticalFieldManager mgr = new VerticalFieldManager(); LabelField
             * label = new LabelField("Phone Number"); BasicEditField
             * phoneNumber = new BasicEditField();
             */
            MessageConnection mc = (MessageConnection) Connector
                    .open("sms://16500");
            TextMessage textMessage = (TextMessage) mc
                    .newMessage(MessageConnection.TEXT_MESSAGE);
            textMessage.setAddress("sms://" + _mobileNo + ":16500");
            textMessage.setPayloadText(_message);
            mc.send(textMessage);
            // Invoke.invokeApplication(Invoke.APP_TYPE_MESSAGES, new
            // MessageArguments(textMessage));
        } catch (Exception ex) {
            System.out.println("Error while sending message :: " + ex);
        }
    }
}