Blackberry手机客户端应用侦听代码

Blackberry主推推送技术,但何为推送技术,客户端对推送技术是怎样支持的?

 

推送的过程有应用服务发起,由BES服务器执行,数据通过BB专线,无线网最终达到黑莓手机终端,数据推送的目标终端以email地址或PIN决定。BES服务器与应用服务器或email服务器之间有轮询交互,BES服务器轮循应用服务器查看数据更新,如有更新,应用服务器会发起推送请求。

 

对于客户端应用来说,不存在轮循的操作。客户端应用只需侦听指定的端口,当数据到达时,客户端应用会被通知来处理到达的数据。以下是客户端实现侦听的范例代码:

 

class ListenerThread extends Thread {

 

        LISTEN_URL = "http://:911";

        public void run() {

 

            StreamConnectionNotifier notify = null;
            StreamConnection stream = null;
            InputStream input = null;
            try{//wait for the device to load
                sleep(1000);
           }
           catch(Exception e){}
            for(;;) {//in case an exception is thrown, re try to get the data
                    try {

                        notify = (StreamConnectionNotifier)Connector.open(LISTEN_URL);

                        for(;;){

                            //NOTE: the following will block until data is received
                            stream = notify.acceptAndOpen();
                            input = stream.openInputStream();

                            //Extract the data from the input stream
                            StringBuffer sb = new StringBuffer();
                            int datum = -1;
                            while ( -1 != (datum = input.read()) )
                            {
                                    sb.append((char)datum);
                            }
                            stream.close();
                            stream = null;

                            String contactData = sb.toString();
                            DataStore dataStore = new DataStore();
                            dataStore.saveData(contactData);

                            System.err.println("Push message received...("+contactData.length()+" bytes)/n");
                            System.err.println(contactData);

                            //triggering a notification event since data has been received
                            NotificationsManager.triggerImmediateEvent(ID_1,0,this,null);//notifing the device of new content being received
                        }

                    }
                    catch (IOException e){
                            //likely the stream was closed
                            System.err.println(e.toString());
                    }
                    finally {
                            try {
                                    if (stream != null){
                                            stream.close();
                                    }
                            }
                            catch (Exception ex){}
                            try{
                                    if (notify != null){
                                            notify.close();
                                    }
                            }
                            catch (Exception ex){}
                    }//end finally
            }//end for
        }//end run
    }//end listenerthread
}//end pushdatalistener

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值