Start an Application Using PushRegistry (Receiving SMS)
The following example waits for the sms to receive through the specified port no. When it receives the sms the application automatically starts and check the message content. If the message content is a required one then it will send a message to the same number.
The following attributes should be added in the jar file.
MIDlet-Push-1: sms://:5001,SendApprooval,*
SMS-Port: 5001
The following example waits for the sms to receive through the specified port no. When it receives the sms the application automatically starts and check the message content. If the message content is a required one then it will send a message to the same number.
The following attributes should be added in the jar file.
MIDlet-Push-1: sms://:5001,SendApprooval,*
SMS-Port: 5001
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.wireless.messaging.*;
import java.util.Date;
import java.io.*;
/**
* @author test
*/
public class SendApprooval extends MIDlet implements Runnable, CommandListener, MessageListener {
private Display display;
Date todaydate;
private Command exitCommand;
private Alert content, alert;
Thread thread;
String[] connections;
boolean done;
String smsPort, senderAddress, mess;
MessageConnection smsconn = null, clientConn = null;
Message msg;
Displayable resumeScreen;
public SendApprooval() {
display = Display.getDisplay(this);
smsPort = getAppProperty("SMS-Port");
content = new Alert("");
content.setString("Waiting for Authentication Request");
content.setTimeout(Alert.FOREVER);
exitCommand = new Command("Exit", Command.EXIT, 2);
content.addCommand(exitCommand);
content.setCommandListener(this);
resumeScreen = content;
}
public void startApp() {
String smsConnection = "sms://:" + smsPort;
if (smsconn == null) {
try {
smsconn = (MessageConnection) Connector.open(smsConnection);
smsconn.setMessageListener(this);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
connections = PushRegistry.listConnections(true);
if ((connections == null) || (connections.length == 0)) {
content.setString("Waiting for Authentication Request");
}
done = false;
thread = new Thread(this);
thread.start();
display.setCurrent(resumeScreen);
}
public void run() {
try {
msg = smsconn.receive();
if (msg != null) {
senderAddress = msg.getAddress();
int k, j = 0;
for (k = 0; k <= senderAddress.length() - 1; k++) {
if (senderAddress.charAt(k) == ':') {
j++;
if (j == 2) {
break;
}
}
}
senderAddress = senderAddress.substring(0, k + 1);
content.setString(senderAddress);
senderAddress = senderAddress + smsPort;
if (msg instanceof TextMessage) {
mess = ((TextMessage) msg).getPayloadText();
}
else {
StringBuffer buf = new StringBuffer();
byte[] data = ((BinaryMessage) msg).getPayloadData();
for (int i = 0; i < data.length; i++) {
int intData = (int) data[i] & 0xFF;
if (intData < 0x10) {
buf.append("0");
}
buf.append(Integer.toHexString(intData));
buf.append(' ');
}
mess = buf.toString();
}
if (mess.equals("Give me Rights")) {
try {
clientConn = (MessageConnection) Connector.open(senderAddress);
}catch (Exception e) {
alert = new Alert("Alert");
alert.setString("Unable to connect to Station because of network problem");
alert.setTimeout(2000);
display.setCurrent(alert);
}
try {
TextMessage textmessage = (TextMessage) clientConn.newMessage(MessageConnection.TEXT_MESSAGE);
textmessage.setAddress(senderAddress);
textmessage.setPayloadText("Approoved");
clientConn.send(textmessage);
} catch (Exception e) {
Alert alert = new Alert("", "", null, AlertType.INFO);
alert.setTimeout(Alert.FOREVER);
alert.setString(e.toString());
display.setCurrent(alert);
}
}
} else {
}
} catch (IOException e) {
content.setString(e.toString());
display.setCurrent(content);
}
}
public void pauseApp() {
done = true;
thread = null;
resumeScreen = display.getCurrent();
}
public void destroyApp(boolean unconditional) {
done = true;
thread = null;
if (smsconn != null) {
try {
smsconn.close();
} catch (IOException e) {
}
notifyDestroyed();
}
public void showMessage(String message, Displayable displayable) {
Alert alert = new Alert("");
alert.setTitle("Error");
alert.setString(message);
alert.setType(AlertType.ERROR);
alert.setTimeout(5000);
display.setCurrent(alert);
}
public void commandAction(Command cmd, Displayable disp) {
try {
if (cmd == exitCommand || cmd == Alert.DISMISS_COMMAND) {
destroyApp(false);
notifyDestroyed();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void notifyIncomingMessage(MessageConnection conn) {
if (thread == null) {
content.setString("Waiting for Authentication Request");
display.setCurrent(content);
done = false;
thread = new Thread(this);
thread.start();
}
}
}
No comments:
Post a Comment