• Downloading from our site will require you to have a paid membership. Upgrade to a Premium Membership from 10$ a month today!

    Dont forget read our Rules! Also anyone caught Sharing this content will be banned. By using this site you are agreeing to our rules so read them. Saying I did not know is simply not an excuse! You have been warned.

Admin

Well-Known Member
Staff member
Administrator
Code:
package sendsms;

import java.io.IOException;
import java.io.InterruptedIOException;
import javax.microedition.io.Connector;
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.*;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;

/**
 * @author pc
 */
public class Midlet2 extends MIDlet implements CommandListener {
//phần khởi tạo giao diện
    private Display display;
    private Form form;
    private TextField sdt;
    private TextField nd;
    private Command send, exit;
    private Alert alert;
    private MessageConnection conn;
//tạo hàm contructor
    public Midlet2() {
        display = Display.getDisplay(this);
        form = new Form("mesaging");
        sdt = new TextField("sđt", "", 11, TextField.PHONENUMBER);
        nd = new TextField("nội dung tin nhắn", "", 1000, TextField.ANY);
        send = new Command("Gửi", Command.OK, 1);
        exit = new Command("Thoát", Command.EXIT, 1);
        form.append(sdt);
        form.append(nd);
        form.addCommand(send);
        form.addCommand(exit);
        form.setCommandListener(this);
    }

    public void startApp() {
        display.setCurrent(form);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
        notifyDestroyed();
    }

    public void commandAction(Command c, Displayable d) {
        //bắt sự kiện khi bấm exit
        if (c == exit) {
            destroyApp(true);
        } else if (c == send) {
            //tạo biến string xuất chuỗi của 2 textfield
            String mno = sdt.getString();
            String msg = nd.getString();
            if (mno.equals("")) {
                //nếu người dùng không nhập gì mà bấm gửi sẽ xuất ra thông báo
                alert = new Alert("alert", "hãy nhập nội dung và số điện thoại", null, AlertType.INFO);
                alert.setTimeout(alert.FOREVER);
                display.setCurrent(alert);
            } else {
                try {
                    //khởi tạo messageconection nếu không gửi được sẽ thông báo lỗi
                    conn = (MessageConnection) Connector.open("sms://" + mno);
                } catch (Exception ex) {
                    alert = new Alert("lỗi", "không gửi được tin nhắn", null, AlertType.ERROR);
                    alert.setTimeout(3000);
                    display.setCurrent(alert);
                }

                try {
                    //tạo textmessage khi người dùng nhập vào sẽ gửi đi và xuất thông báo gửi thành công
                    TextMessage tms = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE);
                    tms.setAddress("sms://" + mno);
                    tms.setPayloadText(msg);
                    conn.send(tms);
                } catch (Exception ex) {
                    alert = new Alert("alert", "gửi thành công", null, AlertType.INFO);
                    alert.setTimeout(alert.FOREVER);
                    display.setCurrent(alert);
            }
        }
    }
}}
Chúc các bạn thành công!
 

Facebook Comments

New posts New threads New resources

Back
Top