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

 

 

發佈了41 篇原創文章 · 獲贊 1 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章