隨筆 - 3  文章 - 1  trackbacks - 0
          <2011年5月>
          24252627282930
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          1.在Android中,SMS消息傳遞是由SmsManager進行處理的。可以通過靜態方法SmsManager.getDefault()來獲得對SmsManager的引用,如下:

              SmsManager  smsManager = SmsManager..getDefault();

          2.Android中,要接收和發送SMS需要以下兩個權限

              <uses-permission android:name="android.permission.READ_SMS" />  
                   <uses-permission android:name="android.permission.RECEIVE_SMS" />

          3.SMS的發送

              1) 發送文本信息,可以使用SMS Manager 中的sendTextManager 的方法

                 sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent);

                 參數如下:

                  destinationAddress:接收方的手機號碼

                  scAddress:發送方的手機號碼

                  text:信息內容

                  sentIntent:發送是否成功的回執,會在消息發送成功或者失敗后觸發。

                  DeliveryIntent:接收是否成功的回執,當目標接收人收到你的信息后觸發。

              2)跟蹤和確認SMS消息的發送

                  sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent);

                  參數sentlntent的返回碼如下:

                  Activity.RESULT_OK:表示發送成功

                  RESULT_ERROR_GENERIC_FAILURE :表示發生了為指定的錯誤

                  RESULT_ERROR_RADIO_OFF :表示連接的無線信號被 關閉

                  RESULT_ERROR_NULL_PDU:表示PDU錯誤

              3)發送SMS以及監控它的發送過程是否成功的經典示例:

                  String SEND_SMS_ACTION = "SENT_SMS_ACTION";
                     String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";
                     //創建senTIntent參數
                     Intent sentIntent = new Intent(SEND_SMS_ACTION);
                     PendingIntent sentPI = PendingIntent.getBroadcast(getApplicationContext(), 0, sentIntent, 0);
                     //創建deliveredIntent參數
                     Intent deliveredIntent = new Intent(DELIVERED_SMS_ACTION);
                     PendingIntent delivePI = PendingIntent.getBroadcast(getApplicationContext(), 0, deliveredIntent, 0);

                   //注冊廣播器
                     registerReceiver(new BroadcastReceiver(){

                   @Override
                   public void onReceive(Context context, Intent intent) {
                     // TODO Auto-generated method stub
                    switch (getResultCode()) {
                       case Activity.RESULT_OK:
                          ....
                       case RESULT_ERROR_GENERIC_FAILURE:
                          ....
                        case RESULT_ERROR_RADIO_OFF:
                          ....
                        case RESULT_ERROR_NULL_PDU:
                          ....
                        }
                      }
                      },
                      new IntentFilter(SEND_SMS_ACTION));
                      

                      //注冊廣播器
                      registerReceiver(new BroadcastReceiver(){

                        @Override
                       public void onReceive(Context context, Intent intent) {
                            // TODO Auto-generated method stub
                              ....
                             }
                        }
                      },
                      new IntentFilter(DELIVERED_SMS_ACTION));

              4)保證不超過最大的SMS信息大小

                  SMS的大小一般被限制為160個字符,比它大的信息會被分割為多個小的部分。SMS Manager的divideMeaasge方法可    以接收一個字符串作為輸入,并把他分割到一個消息的ArrayList中,每一個消息都比允許的最大長度小。使用sendMultipartTextM    essage可以發送消息數組。如下:

                  ArrayList<String> messageArray - smsManager.divideMeaasge(myMessage);

                  ArrayList<PendingIntent> sentIntents = new ArrayList<Pendinglntent>();

                  for(int i = 0; i < messageArrsy.size(); i ++)

                  {

                      sentIntents.add(sentPI);

                      smsManager.sendMultipartTextMessage(sendTo,null,messageArray,sentintent,null);

                  }

              5)發送數據消息

              使用SMS Manager的sendDataMessage方法,可以經由SMS來發送二進制數據。

              sendDataMessage與sendTextMessage方法相似,前者需要額外的參數:信息到達的目的端口和由你想發送的數據所組成的一個    字節數組    

              short destinationPort = 80;

              byte[] data = [...you data ...];

              smsManager.sendDataMessage(sendTo,null,destinationPort ,data,sentPI,null);

          4.SMS的監聽

              SMS廣播Intent包含了收到的SMS的詳細信息。要提取封裝在SMS廣播的SmsManager對象,需要使用PDU密鑰來提取一個SMS

              pdus數組,其中每一個pdu都表示一條SMS信息。

              如下:

              Bundle bundle = intent.getExtras();

              Object[] pdus = (Object[]) bundle.get("pdus");

                   SmsMessage[] msgs = new SmsMessage[pdus.length];

                   for (int i = 0; i < pdus.length; i++) {

                   msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);

                  }

              每一個SMS Manager對象都包含了SMSManager信息的詳細內容(電話號碼,時間戳,信息體)。

              要監聽到來的信息,需要使用一個監聽android.provider.Telephony.SMS_RECEIVED動作串的Intent Filter 來注冊一個廣播接收     器。如下:

              final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED動作串的Intent Filter";    

              IntentFilter filter = new IntentFilter(SMS_RECEIVED );

              BroadcastReceiver receiver = new IncomingSMSReceiver();

              registerReceiver(receiver ,filter);
          posted on 2011-05-20 00:38 3ant 閱讀(1941) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          沒有熱愛,何來成就

          主站蜘蛛池模板: 克什克腾旗| 始兴县| 舞阳县| 澳门| 十堰市| 泰宁县| 建瓯市| 镇远县| 嘉义市| 吕梁市| 铜山县| 辽源市| 双辽市| 井陉县| 崇阳县| 富顺县| 登封市| 永定县| 莱西市| 全南县| 大同市| 无极县| 宁明县| 义马市| 彭阳县| 那坡县| 呼玛县| 英吉沙县| 鄂州市| 江城| 天门市| 临泽县| 苏尼特右旗| 墨竹工卡县| 额尔古纳市| 安仁县| 娄底市| 绥阳县| 城口县| 盈江县| 平塘县|