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

📄 mainmidlet.java

📁 一个电子白版程序,J2me程序,注意,使用EC开发,要安装WTK和其插件才能使用
💻 JAVA
字号:
import java.io.OutputStreamWriter;

import javax.microedition.io.Connector;
import javax.microedition.io.ServerSocketConnection;
import javax.microedition.io.SocketConnection;
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import java.io.DataOutputStream;
/*
 * Created on 2006-2-18
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author blrc
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class MainMIDlet extends MIDlet {

	ShareCanvas shareCanvas;
	SocketConnection currentConnection;
	Notify_Alery notifyAlert;
	MySocketServer ss;
	
	protected boolean started;
	public Display display;
	
	public MainMIDlet() {
		super();
		started = false;
		shareCanvas = new ShareCanvas(this);	
		notifyAlert = new Notify_Alery(this);
		
	}

	/* (non-Javadoc)
	 * @see javax.microedition.midlet.MIDlet#startApp()
	 */
	protected void startApp() throws MIDletStateChangeException {
		if (!started)
		{
			display = Display.getDisplay(this);			
			ss = new MySocketServer(this);
			ss.start();
			display.setCurrent(shareCanvas);
			System.out.println("setCurrent: shareCanvas");
		}
	}

	
	protected void pauseApp() {	

	}

	
	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {		

	}
	
	public void someone_invite()
	{
		display.setCurrent(notifyAlert);		
	}
	
	public void accept_invite()
	{
		display.setCurrent(shareCanvas);
		shareCanvas.set_currentConnection(currentConnection);
		currentConnection = null;
		shareCanvas.accept_invite();
	}
	
	public void deney_invite()
	{
		display.setCurrent(shareCanvas);
		try 
		{
			DataOutputStream os = new DataOutputStream(currentConnection.openOutputStream());
			os.writeUTF("NO");
			System.out.println("writeUTF NO to deney invite");
			os.close();
			currentConnection.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public void set_currentConnection(SocketConnection currentConnection)
	{
		this.currentConnection = currentConnection; 
	}
	
	public void revert()
	{
		ss.revert();
	}

}

class MySocketServer extends Thread
{
	MainMIDlet adaptee;
	int currentConnectionNum;
	SocketConnection currentConnection;
	
	public MySocketServer(MainMIDlet adaptee)
	{
		super();
		this.adaptee = adaptee;
		currentConnectionNum = 0;
	}
	
	public void revert()
	{
		currentConnectionNum = 0;
	}
	
	public void run()
	{
		try
		{			
	  		ServerSocketConnection scn = (ServerSocketConnection)Connector.open("socket://:8000");
	  		System.out.println("MySocketServer start"); 
	  		while (true)
	  		{
	  			 SocketConnection sc = (SocketConnection) scn.acceptAndOpen();
	  			 System.out.println("MySocketServer is listening here");
	  			 if (currentConnectionNum > 0)
	  			 {
	  			 	DataOutputStream os = new DataOutputStream(sc.openOutputStream());
	  			 	System.out.println("reject a connection");
	  			 	os.writeUTF("NO");
	  			 	os.close();
	  			 	sc.close();
	  			 }
	  			 else
	  			 {
	  			 	currentConnectionNum++;
	  			 	adaptee.set_currentConnection(sc);
	  			 	adaptee.someone_invite();
	  			 	sc = null;
	  			 }
	  			 
	  		}
		}
	  	catch (Exception e)
		{
	  		e.printStackTrace();
		}
	}
}

⌨️ 快捷键说明

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