Archyvas

Įrašo tag'ai: ‘programamming’

Howto. Send SMS with JavaME

Gruodis 28th, 2009 Ernestas Kardzys 4 komentarai

Here is our second program with JavaME. As I promised this program sends SMS messages by using your mobile phone && JavaME.

The program looks like that:

The application looks like that

To send the SMS you need to select Menu->Send (ok, it was probably very difficult to guess that :D ).

The code looks like that:

/*
 * File SMSSender.java – the GUI for our application
 */

package info.ernestas.smssender;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.*;

/**
 * @author Ernestas
 */

public class SMSSender extends MIDlet implements CommandListener {
    //
    private Display display;
    private Form mainForm;

    private Command cmdExit;
    private Command cmdAbout;
    private Command cmdBack;
    private Command cmdSendSMS;

    private TextField tfSMSMessage;
    private TextField tfPhoneNumber;

    //

    public void startApp() {
        display = Display.getDisplay(this);

        cmdExit = new Command("Exit", Command.EXIT, 1);
        cmdSendSMS = new Command("Send", Command.SCREEN, 2);
        cmdBack = new Command("Back", Command.BACK, 1);
        cmdAbout = new Command("About", Command.SCREEN, 3);

        tfPhoneNumber = new TextField("Phone number:", "", 20, TextField.PHONENUMBER);
        tfSMSMessage = new TextField("SMS message:", "", 320, TextField.ANY);

        mainForm = new Form("SMS Sender v.0.1");
        mainForm.addCommand(cmdExit);
        mainForm.addCommand(cmdSendSMS);
        mainForm.addCommand(cmdAbout);
        mainForm.append(tfPhoneNumber);
        mainForm.append(tfSMSMessage);
        mainForm.setCommandListener(this);

        display.setCurrent(mainForm);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable d) {
        if (c == cmdExit) {
            destroyApp(true);
            notifyDestroyed();
        }
        else if (c == cmdBack) {
            display.setCurrent(mainForm);
        }
        else if (c == cmdSendSMS) {
            String phoneNumber = tfPhoneNumber.getString().trim();
            String smsMessage = tfSMSMessage.getString().trim();
            if (phoneNumber.length() > 0 && smsMessage.length() > 0) {
                SMSClient client = new SMSClient();
                client.sendSMS(phoneNumber, smsMessage);

                tfPhoneNumber.setString("");
                tfSMSMessage.setString("");
            }
        }
        else if (c == cmdAbout) {
            Alert about = new Alert("About…", "Created by Ernestas Kardzys, ", null, AlertType.INFO);
            about.setTimeout(Alert.FOREVER);
            about.setCommandListener(this);
            about.addCommand(cmdBack);

            display.setCurrent(about);
        }
    }
}
// ————————————————-
/*
 * File SMSClient.java – the class for Sending SMSes
 */

package info.ernestas.smssender;

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

/**
 *
 * @author Ernestas Kardzys
 */

public class SMSClient implements Runnable, MessageListener {
    private String phoneNumber;
    private String message;

    public void sendSMS(String phoneNumber, String message) {
        this.phoneNumber = phoneNumber;
        this.message = message;
        new Thread(this).start();
    }

    public void run() {
        try {
            String address = "sms://" + this.phoneNumber;
            MessageConnection smsConnection = (MessageConnection) Connector.open(address);

            TextMessage smsMessage = (TextMessage)smsConnection.newMessage(MessageConnection.TEXT_MESSAGE);
            smsMessage.setPayloadText(this.message);
            smsConnection.send(smsMessage);

            smsConnection.close();
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void notifyIncomingMessage(MessageConnection mc) {
        synchronized (this) {
            notify();
        }
    }

}

The program can be found here: http://www.ernestas.info/projects/java/javame/smssender/smssender.zip
Don’t forget: you’re using it at our own risk ;)

Kategorijos:JavaME, Programavimas Raktažodžiai:, , ,