监听短息

package test01;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import com.siemens.mp.gsm.SMS;
import com.siemens.mp.NotAllowedException;
import java.io.IOException;
import com.siemens.mp.io.*;
public class StartMIDlet extends MIDlet
{
static StartMIDlet instance;
UIMainList MainList = new UIMainList();
public StartMIDlet()
{
instance = this;
}
public void startApp() throws MIDletStateChangeException
{
try
{
Display.getDisplay(this).setCurrent(MainList);
}
catch(Exception e)
{
System.out.println("e = "+e);
}
}
public void pauseApp()
{
}
public void destroyApp(boolean UIcondition)
{
}
public static void quitApp()
{
instance.destroyApp(true);
instance.notifyDestroyed();
instance = null;
}
}

class UIMainList extends List
{
private static String[] Str_List = {"A",
"B",
"C"};
static Displayable instance = null;
private Command cmExit;
private Command cmInfo;
public UIMainList() //throws Exception
{
super("测试",
List.IMPLICIT,
Str_List,
null);
setCommandListener(new CommandListener()
{
public void commandAction(Command c, Displayable d)
{
DisplayCommand(c,d);
}
}
);
cmInfo = new Command("Help", 1, 1);
cmExit = new Command("Exit", 1, 2);
jbInit();
instance = this;
temp_run();

}
public void temp_run() //throws Exception
{
Receiver rec = new Receiver();
Connection con = new Connection ("SMS: 223322332");
Alert str = new Alert("fail");
try
{
con.setListener(rec);
//把上面这句注释后程序能跑,一打开模拟器就死。为什么?
//没有这句,我就监听不到短信的到来啊

}
catch(Exception e)
{
Display.getDisplay(StartMIDlet.instance).setCurrent(str);
}

}

public void DisplayCommand(Command c,Displayable d)
{
if(c == cmExit)
{
instance = null;
StartMIDlet.quitApp();
}
else if( c == cmInfo)
{
Alert Al = new Alert("Help");
Al.setString("测试的帮助文件");
Display.getDisplay(StartMIDlet.instance).setCurrent(Al);
}
}
private void jbInit()
{
addCommand(cmInfo);
addCommand(cmExit);
}

}

class Receiver implements ConnectionListener
{
public void receiveData(byte[] data)
{
try
{
Alert str = new Alert("sms succ");
Display.getDisplay(StartMIDlet.instance).setCurrent(str);

}
catch(Exception e)
{
Alert str = new Alert("sms fail");
Display.getDisplay(StartMIDlet.instance).setCurrent(str);
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值