新建类NetworkThread它继承在Thread并将原先commandAction()中发送接受消息的操作移到此类中完成代码如下
以下是引用片段
/*
*NetworkThreadjava
*
*Createdon年月日下午:
*
*/
packagenjunnection;
importjavaioIOException;
importjavaxmicroeditionlcduiTextBox;
/**
*
*@authorMagic
*/
publicclassNetworkThreadextendsThread{
privateNetworkConnectionconnection;
privateTextBoxtext;
publicNetworkThread(TextBoxtext){
super();
thistext=text;
}
publicvoidrun(){
Stringmessage=null;
connection=newNetworkConnection(HelloWorld);
try{
connectionsendMessage();
message=connectionreceiveMessage();
connectionclose();
}catch(IOExceptionex){
exprintStackTrace();
}
textsetString(message);
}
} 同时修改原先的MIDlet得到新的MIDlet
以下是引用片段
/*
*ConnectionMidletjava
*
*Createdon年月日下午:
*/
packagenjunnection;
importjavaioIOException;
importjavaxmicroeditionmidlet*;
importjavaxmicroeditionlcdui*;
/**
*
*@authorMagic
*@version
*/
publicclassConnectionMidletextendsMIDletimplementsCommandListener{
privateDisplaydisplay;
privateTextBoxtext;
privateCommandshowCommand;
publicConnectionMidlet(){
display=DisplaygetDisplay(this);
text=newTextBox(Message请使用问候命令发送消息TextFieldANY);
showCommand=newCommand(问候CommandSCREEN);
textaddCommand(showCommand);
textsetCommandListener(this);
}
publicvoidstartApp(){
displaysetCurrent(text);
}
publicvoidpauseApp(){
}
publicvoiddestroyApp(booleanunconditional){
}
publicvoidcommandAction(CommandcommandDisplayabledisplayable){
if(command==showCommand){
/**
*创建新的线程完成联网操作
*/
(newNetworkThread(text))start();
}
}
} 此时在commandAction()中我们创建了新的线程来完成消息的发送和接受运行程序可以成功接受到返回的消息如图所示
图
多线程与拍照
同样在移动多媒体API(JSR )的使用过程中也需要应用到多线程利用手机摄像头拍照的操作也会引起的堵塞因此当在commandAction()中调用拍照操作时若未开辟新线程来处理同样也会造成程序没有响应让我们通过一个例子来观察一下吧
这是一个很简单的摄像头程序首先程序将会调用摄像头取景然后当用户按下拍照命令后就捕捉当前的画面并显示给用户图是程序的UML类图
图
MalCameraMidlet和CameraMidlet分别是错误和正确的MIDlet它们都创建一个CameraView(用于显示摄像头画面)的对象唯一的不同在于前者创建MalCamera的对象后者创建Camera的对象(MalCamera和Camera都是CameraView的子类)SnapShot则是将摄像头捕捉到的图像显示给用户的类
首先我们还是来看一下未使用多线程而造成程序没有响应的情况如下是MalCameraMidlet类的代码其中保存着一个指向CameraView的引用并在startApp()中创建了MalCamera对象
以下是引用片段
/**
*MalCameraMidletjava
*
*/
packagenjuhysteriathreadcamera;
importjavaxmicroeditionlcduiDisplay;
importjavaxmicroeditionmidletMIDlet;
/**
*ThisMIDletcreatethemalcameraviewsotheprogram
*willblockaftercallingCapturecommand
*@authorMagic
*
*/
publicclassMalCameraMidletextendsMIDlet{
protectedDisplaydisplay;
privateCameraViewcamera;
publicMalCameraMidlet(){
super();
display=DisplaygetDisplay(this);
}
protectedvoidstartApp(){
camera=newMalCamera(this); displaysetCurrent(camera);
}
/**
*Showcurrentcamera
*/
publicvoidshowCamera(){
displaysetCurrent(camera);
}
protectedvoidpauseApp(){
}
protectedvoiddestroyApp(booleanarg){
}
} CameraView是一个抽象类是两个显示Camera的类的父类它负责显示Camera并将commandAction()方法留给子类实现代码如下
以下是引用片段
/**
*CameraViewjava
*/
packagenjuhysteriathreadcamera;
importjavaioIOException;
importjavaxmicroeditionlcduiCommand;
importjavaxmicroeditionlcduiCommandListener;
importjavaxmicroeditionlcduiDisplayable;
importjavaxmicroeditionlcduiForm;
importjavaxmicroeditionlcduiItem;
importjadiaManager;
importjadiaMediaException;
importjadiaPlayer;
importjantrolVideoControl;
importjavaxmicroeditionmidletMIDlet;
/**
*Thisisanabstractclasswhichdisplaycameratouser
*@authorMagic
*
*/
publicabstractclassCameraViewextendsFormimplementsCommandListener{
protectedMIDletmidlet;
protectedPlayerplayer;
protectedVideoControlvc;
protectedCommandexitCommand;
protectedCommandcaptureCommand;
protectedCameraView(MIDletmidlet){
super(照相);
thismidlet=midlet;
exitCommand=newCommand(退出CommandEXIT);
captureCommand=newCommand(拍照CommandSCREEN);
addCommand(exitCommand);
addCommand(captureCommand);
setCommandListener(this);
/**
*Createcameraplayerandcontrol
*/
try{
player=ManagercreatePlayer(capture://video);
playerrealize();
vc=(VideoControl)playergetControl(VideoControl);
append((Item)vcinitDisplayMode(VideoControlUSE_GUI_PRIMITIVEnull));
playerstart();
}catch(IOExceptione){
eprintStackTrace();
}catch(MediaExceptione){
eprintStackTrace();
}
}
publicabstractvoidcommandAction(CommandcmdDisplayabledisplayable); }