当前位置: 技术问答>java相关
为什么这个监听器一注册,程序就退出 (j2me 关于短信接收的问题),分不够再加,在线等待
来源: 互联网 发布时间:2015-11-18
本文导语: 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 StartMI...
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);
}
}
}
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);
}
}
}
|
这主要是手机厂商提供支持的问题。我将你的代码拿过去测试以后,几种注册的方法都测试过,在模拟器上都会退出,而在实际的siemens3188上没有退出,不过没有监听到