SDK5.0的API提供了PushApplication接口,可以非常简单地实现推送客户端, 同时支持接收BIS/BES推送消息. 代码非常简短,不需要后台进程,不需要自己实现侦听.
范例如下:
public class MainApp extends UiApplication implements PushApplication {
public void onMessage(PushInputStream arg0, StreamConnection arg1) {
// 接收到推送数据
System.out.println("got it");
}
public void onStatusChange(PushApplicationStatus status) {
// 监控推送状态
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MainApp app= new MainApp();
app.enterEventDispatcher();
}
public MainApp()
{
MainScreen screen = new MainScreen();
screen.add(new LabelField("使用接口接收数据"));
MenuItem startMenuItem= new MenuItem("启动监听",20,20){
public void run()
{
startIt();
}
};
screen.addMenuItem(startMenuItem);
this.pushScreen(screen);
}
public void startIt()
{
// 在9888端口上监听数据并注册监听
PushApplicationDescriptor pad = new PushApplicationDescriptor( 9888 );
PushApplicationRegistry.registerApplication( pad );
}
}