reciveing value from the thread

F

focode

Dear friends , i have a small issue i am working on j2me code , i have
to develop a class that will receive the message . From the main
program when the user hits the required button a function from "
messagereciving " class will be called , the only thing is that ,
function receiving code is inside the run method , the value of the
recieved msg text is inside the variable "msgReceived" i have to get
this value in the main method , the source code of message receiving
class is as follows :

import javax.microedition.io.*;
import javax.wireless.messaging.*;
import java.io.IOException;

public class SMSMIDlet implements Runnable {
private Thread mReceiver = null;
private String mPort = "1234";
public String msgReceived = null;
public SMSMIDlet() {
}
private void startReceive() {
if (mReceiver != null)
return;
// start receive thread
mReceiver = new Thread(this);
mReceiver.start();
}
private boolean mEndNow = false;
private MessageConnection conn = null;
public void run() {
Message msg = null;

conn = null;
mEndNow = false;
/** Check for sms connection. */
try {
conn = (MessageConnection) Connector.open("sms://:" +
mPort);
msg = conn.receive();
while ((msg != null) && (!mEndNow)) {

if (msg instanceof TextMessage) {

msgReceived = ((TextMessage)msg).getPayloadText();
// i have to get the value of msgReceived in the
calling program
}
}
} catch (IOException e) {

}
}

}
 
M

markspace

focode said:
Dear friends , i have a small issue i am working on j2me code , i have
to develop a class that will receive the message . From the main
program when the user hits the required button a function from "
messagereciving " class will be called , the only thing is that ,
function receiving code is inside the run method , the value of the
recieved msg text is inside the variable "msgReceived" i have to get
this value in the main method ,


I don't do any J2ME but the obvious answer is to add a "get" method to
your class.

E.g.:

the source code of message receiving
class is as follows :

import javax.microedition.io.*;
import javax.wireless.messaging.*;
import java.io.IOException;

public class SMSMIDlet implements Runnable {
private Thread mReceiver = null;
private String mPort = "1234";
public String msgReceived = null;
public SMSMIDlet() {
}
private void startReceive() {
if (mReceiver != null)
return;
// start receive thread
mReceiver = new Thread(this);
mReceiver.start();
}


public String getMsgReceived() {
return msgReceived;
}
 
F

focode

I don't do any J2ME but the obvious answer is to add a "get" method to
your class.

E.g.:






       public String getMsgReceived() {
         return msgReceived;
       }

by this way i will have to call two methods of the same class

the way u tell me , this can be done by specifying the variable as
public static String and access it from the calling program
.. What i wanted was to call a single method that could send me the
message .
 
M

markspace

focode said:
the way u tell me , this can be done by specifying the variable as
public static String and access it from the calling program
.. What i wanted was to call a single method that could send me the
message .


If you do that (one method) you'll have to block the calling thread
until the SMSMIDlet completes. What is the point of using threads then?
Just call a method and don't bother with a Runnable.
 
R

Roedy Green

Dear friends , i have a small issue i am working on j2me code , i have
to develop a class that will receive the message

In Java SE, you do this with the concurrent package. It no such
features are available in ME, you might look at the SE source code for
inspiration.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"There is an evil which ought to be guarded against, in the indefinite accumulation of property,
from the capacity of holding it in perpetuity by... corporations.
The power of all corporations aught to be limited in this respect.
The growing wealth acquired by them never fails to be a source of abuses."
~ James Madison (born: 1751-03-16 died: 1836-06-28 at age: 85)
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top