?? pushsmslistner.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 + -