wireless messaging api in j2me


F

focode

dear every one , i am in critical state , i had to write a application
that can send and receive sms in the same application i am able to
send the sms ( got the java file from the book ) i ahve written an
another application to recive the sms in the same application but
program is not executing , it says that their is some excetion in
startApp fuction , my boss needs it uergently , iahve trusted this
group since my college days and i have laways got the solution to my
problem kindly help me out

here the source code i have written to recive sms in application ,
first i have send the sms "hello" to my mobile the i want to recive it
on the same

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.wireless.messaging.*;
import javax.microedition.io.*;


public class main extends MIDlet implements CommandListener,Runnable {

Sender give;
String mPort="50000";
String msgReceived = null;
private MessageConnection conn = null;
private boolean mEndNow = false;
Thread reciver = null;
Form mForm,reciveForm;
Command ok;
Command Exit;
public void startApp() {
mForm = new Form( "Hello");
mForm.append("hello virtu");
ok = new Command("ok",Command.OK,0);
Exit = new Command("Exit",Command.EXIT,0);
mForm.addCommand(ok);
mForm.addCommand(Exit);
mForm.setCommandListener(this);
Display mDisplay = null;
mDisplay.getCurrent();
mDisplay.setCurrent(mForm);

// give.sendMsg("9990753070","5000" ,"hello" );


}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {

}
public void commandAction(Command c , Displayable d)
{
if (c == ok)
{
give.sendMsg("9990753070","5000" ,"hello" );
reciver.start();
}
}
public void run()
{
Message msg = null;
String msgReceived = null;
conn=null;
mEndNow = false ;
/** Check for sms connection **/
try{
conn = (MessageConnection) Connector.open("sms://:" +
mPort);
msg = conn.receive();
while(msg !=null)
{
if(msg instanceof TextMessage)
{
msgReceived = ((TextMessage)msg).getPayloadText
();

Display vDisplay = null;
reciveForm = new Form (" msg Arrived");
reciveForm.append(msgReceived);
vDisplay.getCurrent();
vDisplay.setCurrent(mForm);


}
}
}
catch(Exception e)
{

}

}


}

relpy me as soon as possible
 
Ad

Advertisements

Joined
Dec 31, 2011
Messages
1
Reaction score
0
Hi I'm new to velocityreviews & J2ME bt i think i can help you for your problem,
this is the source code for your problem, I think it will work

import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;

/**
* @author Anupa Amal
*/
public class Messenger extends MIDlet implements CommandListener {

private Display dis;
private Form f;
private Command c1;
public TextField tf1;
public TextField tf2;
public TextField tf3;

public Messenger() {
dis = dis.getDisplay(this);

f = new Form("Messenger");

c1 = new Command("Send", Command.OK, 0);
f.addCommand(c1);

tf1 = new TextField("Phone Number", "", 10, TextField.PHONENUMBER);
tf2 = new TextField("Message Out", "", 50, TextField.ANY);
tf3 = new TextField("Message In", "", 50, TextField.ANY);
f.append(tf1);
f.append(tf2);
f.append(tf3);

f.setCommandListener(this);

}

public void startApp() {
dis.setCurrent(f);
recivemessage();
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

public void commandAction(Command c, Displayable d) {

if (c == c1) {
sendmessage();
}

}

void sendmessage() {
SendMessageThread smt = new SendMessageThread(this);
Thread t = new Thread(smt);
t.start();
}

void recivemessage() {
ReciveMessageThread rmt = new ReciveMessageThread(this);
Thread t = new Thread(rmt);
t.start();
}
}

class SendMessageThread implements Runnable {

Messenger m;

public SendMessageThread(Messenger m1) {
this.m = m1;
}

public void run() {
try {
MessageConnection mc = (MessageConnection) Connector.open("sms://" + m.tf1.getString() + ":1234");
TextMessage tm = (TextMessage) mc.newMessage(mc.TEXT_MESSAGE);
tm.setPayloadText(m.tf2.getString());
mc.send(tm);
mc.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

class ReciveMessageThread implements Runnable {

Messenger mm;

public ReciveMessageThread(Messenger m2) {
this.mm = m2;
}

public void run() {
while (true) {
try {
MessageConnection mc = (MessageConnection) Connector.open("sms://:1234");
TextMessage tm = (TextMessage) mc.receive();
mm.tf3.setString(tm.getPayloadText());
mc.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top