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

📄 pushsmslistner.java

📁 这是非常简单的例子
💻 JAVA
字号:
/*///////////////////////////////////////////////////////////////////////////////
//文档生成日期:2005.11.3
//
//(1)概述:
//类名称:PushSMSLisnter
//类说明:
//	提供主界面
  
//所在子系统:MIMESMSnotifyPushRegistry
//
//系统总描述:
	    本工程发送一个MIME头的短信给目标手机。MIME头中指明了对方应该如何处理。
	    对方手机收到后,触发注册了PushRegistry的MIDlet应用,并解析短信,
	    按照指明的命令操作。


	 子系统描述:
		注册Push Registry,监听sms://:8341,当然端口可以修改的。
		这样,收到带有MIME头的短信后,解析并按照命令行事.

//(2)历史记录:
//创建人: 郑昀(2005.11.3)
//联系我: Google Talk >> zhengyun@gmail.com
//Blogs:    http://blog.csdn.net/zhengyun_ustc/以及http://www.cnblogs.com/zhengyun_ustc

//(3)版权声明:
//我这个版本的MIMESMSnotifyPushRegistry,
//j2me客户端代码仅仅允许您借鉴,但不得用于商业用途,除非得到郑昀本人的授权。本人保留所有权利。

////////////////////////////////////////////////////////////////////*/
package com.ultrapower.midlet;

import java.io.IOException;
import java.util.Vector;

import javax.microedition.io.PushRegistry;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import com.ultrapower.handler.DatagramHandler;

/**********************************************************
//PushSMSLisnter
//
//Class Description:
//	实际上应该算作MVC中的View部分,是MIDlet的主界面。
//Author: 
//   zhengyun@ultrapower 2005.11.3
//
**********************************************************/
public class PushSMSListner extends MIDlet implements CommandListener {

	private boolean init = false;
	private Vector connectionHandlers = new Vector();
	public  Form mainForm;
	private Display display;    // The display for this MIDlet
	private Command exitCommand; // The exit command
	private Command registerCommand;
	private Command unregisterCommand;
	
	public PushSMSListner() {
		super();

		display = Display.getDisplay(this);
		exitCommand = new Command("退出应用", Command.SCREEN, 4); 
		registerCommand = new Command("注册短信监听", Command.SCREEN, 2);
		unregisterCommand = new Command("取消短信监听", Command.SCREEN, 3);
		mainForm = new Form ("PushRegistry和短信联动演示"); 
	}

	protected void startApp() throws MIDletStateChangeException {
		
		mainForm.addCommand(exitCommand); 
		mainForm.addCommand(registerCommand);
		mainForm.addCommand(unregisterCommand);
		mainForm.setCommandListener(this); 
        display.setCurrent(mainForm); 
		
		if (!init) {
		    init = true;
		    String[] conns = 
		      PushRegistry.listConnections(false);
		    System.out.println("Found " + conns.length + 
		      " connections.");
		    for(int ccnt=0; ccnt < conns.length; ccnt++){
		      DatagramHandler handler = 
		        new DatagramHandler(conns [ccnt], this);
		      connectionHandlers.addElement(handler);
			   handler.start();
		    }
		  }

	}

	protected void pauseApp() {
		System.out.println( "pauseApp()" );

		// release any resoruces not required while Paused

		System.out.println( "pauseApp(): return" );

	}

	protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
		System.out.println( "Enter destroyApp");
		
		if(connectionHandlers != null)
		{
			for(int index = 0; index < connectionHandlers.size();index++)
			{
				DatagramHandler handler = (DatagramHandler)connectionHandlers.elementAt(index);
				if(handler != null)
				{
					System.out.println( "关闭DatagramHandler!" );
					handler.stopHandler();
				}
			}
		}
		connectionHandlers = null;

		if ( unconditional == false ) {		// can opt not to be destroyed
			// decide if termination is acceptable, re-assign unconditional
		}

		System.out.println( "destroyApp: return" );
	}
	
	
   public void commandAction(Command c, Displayable s) {
        if (c == exitCommand) {
			try
			{
				destroyApp(false);
			}
			catch(Exception exc)
			{
				exc.printStackTrace();
			}
            notifyDestroyed();
        }
		
        else if (c == registerCommand) 
        { 
			// 匿名内部Thread,由他来完成注册Push Registry的任务,当然也可以通过静态注册。 
            Thread registerThread = new Thread()
            { 
				private String m_sRegClassName = "";
				public void setClassName(String className){
					m_sRegClassName = className;
				}
				public void run(){  
					System.out.println("Try push registry for:" +
							m_sRegClassName);
					
					try {
						String connURL = "sms://:8341";
						String MIDletStr = "com.ultrapower.midlet.PushSMSListner";
						String	FilterStr = "*";
						PushRegistry.registerConnection(connURL,
								MIDletStr, FilterStr);
					}
					catch(IllegalArgumentException iae)
					{
						iae.printStackTrace();
						setCurrent(
								new Alert(
										"注册Push Registry错误", 
										"socket://:8341不被是认为合法的连接语法!"
										+ iae.getMessage() +
										"/" + iae.getClass(),
										null, AlertType.ERROR),
								(Displayable)mainForm);
					}
					catch ( ClassNotFoundException cnf ) {
						cnf.printStackTrace();
						setCurrent(
							new Alert(
									"注册Push Registry错误", 
									"可能是你的手机不支持!"
									+ cnf.getMessage() +
									"/" + cnf.getClass(),
									null, AlertType.ERROR),
							(Displayable)mainForm);
					} catch ( IOException ioe ) {
						ioe.printStackTrace();
						setCurrent(
							new Alert(
									"注册Push Registry错误", 
									"可能本连接已经被其他应用注册了!"
									+ ioe.getMessage() +
									"/" + ioe.getClass(), null, AlertType.ERROR),
							(Displayable)mainForm);
					}
					catch(SecurityException  se)
					{
						se.printStackTrace();
						setCurrent(
								new Alert(
										"注册Push Registry错误", 
										"可能没有权限注册!"
										+ se.getMessage() +
										"/" + se.getClass(), null, AlertType.ERROR),
								(Displayable)mainForm);
					}
					catch(Exception exc)
					{
						exc.printStackTrace();
					}
					
					System.out.println("Succ push registry!");
				}
			};
			registerThread.start(); 
			
            //mainForm.append(item); 
            display.setCurrent(mainForm); 
         }
		else if (c == unregisterCommand) 
        { 
			// 匿名内部Thread,由他来完成注册Push Registry的任务,当然也可以通过静态注册。 
            Thread registerThread = new Thread()
            { 
				public void run(){  
					System.out.println("Try unregister push registry for:");
					
					try {
						String connURL = "sms://:8341";
						PushRegistry.unregisterConnection(connURL);
					}
					catch(IllegalArgumentException iae)
					{
						iae.printStackTrace();
						setCurrent(
								new Alert(
										"反注册Push Registry错误", 
										"socket://:8341不被是认为合法的连接语法!"
										+ iae.getMessage() +
										"/" + iae.getClass(),
										null, AlertType.ERROR),
								(Displayable)mainForm);
					}
					catch(SecurityException  se)
					{
						se.printStackTrace();
						setCurrent(
								new Alert(
										"反注册Push Registry错误", 
										"可能没有权限注册!"
										+ se.getMessage() +
										"/" + se.getClass(), null, AlertType.ERROR),
								(Displayable)mainForm);
					}
					catch(Exception exc)
					{
						exc.printStackTrace();
					}
					
					System.out.println("Succ unregister push registry!");
				}
			};
			registerThread.start(); 
			 
            display.setCurrent(mainForm); 
         }
   }
   
	/**********************************************************
	//	 setCurrent()
	//
	//	 Description:
	//	  设置当前显示的界面
	//
	//	 Parameters:
	//	 Return Values:
	//	 Author:
	//	      zhengyun@ultrapower 2005.10.28
	//
	**********************************************************/
	public void setCurrent(Displayable disp){
		this.setCurrent(disp);
   }
	public void setCurrent(Alert alert, Displayable disp){
		this.setCurrent(alert, disp);
   }
}

⌨️ 快捷键说明

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