⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 receivemidlet.java

📁 JavaME 手机短信息的收发
💻 JAVA
字号:
/*
 * ReceiveMidlet.java
 *
 * Created on 2008年6月2日, 下午7:54
 */

package sms;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.PushRegistry;
import javax.wireless.messaging.*;
/**
 *
 * @author  Administrator
 * @version
 */
public class ReceiveMidlet extends MyMidlet implements CommandListener,Runnable,MessageListener{
    Command exitCommand=new Command("exit",Command.EXIT,2);
    Display display;
    Alert content;
    Thread th;
    String[] connections;
    boolean done;
    String smsP="5000";
    MessageConnection conn;
    Message msg;
    String senderAddress;
    Alert sendingMessageAlert;
    Displayable resumeScreen;
    String address;
    
    public ReceiveMidlet(){
        //smsP=getAppProperty("SMS-Port");//得不到!!
        //System.out.println(smsP);
        display=Display.getDisplay(this);
        content=new Alert("receive message");
        content.setTimeout(Alert.FOREVER);
        content.addCommand(exitCommand);
        content.setCommandListener(this);
        content.setString("waiting for receive message...");
        resumeScreen=content;
    }
    public void startApp() {
        String c="sms://:5000";//+smsP;
        if(conn==null){
            try{
                conn=(MessageConnection)Connector.open(c);
                conn.setMessageListener(this);
            }catch(IOException e){
                e.printStackTrace();
            }
        }
        connections=PushRegistry.listConnections(true);//返回一系列注册过的连接
        if(connections==null||connections.length==0){
            content.setString("waiting for"+smsP+"port info...");
        }
        done=false;
        th=new Thread(this);
        th.start();
        display.setCurrent(resumeScreen);
    }
    
    public void notifyIncomingMessage(MessageConnection conn){
        if(th==null){
            done=false;
            th=new Thread(this);
            th.start();
        }
    }
    
    public void commandAction(Command c,Displayable s){
         
        try{
           if(c==exitCommand||c==Alert.DISMISS_COMMAND){
               destroyApp(false);
               notifyDestroyed();
           }
        }catch(Exception e){
            e.printStackTrace();
        }

    }
    
   
    public void run(){
        try{
            //System.out.println("thread start!");
            msg=conn.receive();
            //System.out.println("msg is received");
            if(msg!=null){
                senderAddress=msg.getAddress();
                address=senderAddress;
                content.setTitle("from:"+senderAddress);
                content.setString(((TextMessage)msg).getPayloadText());
                display.setCurrent(content);
            }    
        }catch(IOException e){
            //e.printStackTrace();
        }
    }
    
    public void pauseApp() {
        done=true;
        th=null;
        resumeScreen=display.getCurrent();
    }
    
    public void destroyApp(boolean unconditional) {
        done=true;
        th=null;
         if(conn!=null){
            try{
                conn.close();
            }catch(IOException e){
                e.printStackTrace();
            }
         }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -