Android提供的系統服務之--SmsManager(簡訊管理器)

來源:互聯網
上載者:User

標籤:android   系統服務   smsmanager   service   簡訊   

Android提供的系統服務之--SmsManager(簡訊管理器)

                                                                     --轉載請註明出處:coder-pig



SmsManager相關介紹以及使用圖解:






當然為了方便各位,把代碼粘一粘吧,就不用麻煩大家寫代碼了:

有需要的時候就複製粘貼下吧!

1)調用系統傳送簡訊的功能:

 public void SendSMSTo(String phoneNumber,String message){      //判斷輸入的phoneNumber是否為合法電話號碼    if(PhoneNumberUtils.isGlobalPhoneNumber(phoneNumber)){//Uri.parse("smsto") 這裡是轉換為指定Uri,固定寫法        Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:"+phoneNumber));                    intent.putExtra("sms_body", message);                    startActivity(intent);      }  }  



2)調用系統提供的簡訊介面傳送簡訊:

public void sendSMS(String phoneNumber,String message){      //擷取簡訊管理器       android.telephony.SmsManager smsManager = android.telephony.SmsManager.getDefault();      //拆分簡訊內容(手機簡訊長度限制),貌似長度限制為140個字元,就是    //只能發送70個漢字,多了要拆分成多條簡訊發送    //第四五個參數,如果沒有需要監聽發送狀態與接收狀態的話可以寫null        List<String> divideContents = smsManager.divideMessage(message);       for (String text : divideContents) {            smsManager.sendTextMessage(phoneNumber, null, text, sentPI, deliverPI);        }  } 


處理髮送狀態的PendingIntent:

//處理返回的發送狀態   String SENT_SMS_ACTION = "SENT_SMS_ACTION";  Intent sentIntent = new Intent(SENT_SMS_ACTION);  PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, sentIntent,  0);  //註冊發送資訊的廣播接收者context.registerReceiver(new BroadcastReceiver() {      @Override      public void onReceive(Context _context, Intent _intent) {          switch (getResultCode()) {          case Activity.RESULT_OK:Toast.makeText(context, "簡訊發送成功", Toast.LENGTH_SHORT).show();  break;          case SmsManager.RESULT_ERROR_GENERIC_FAILURE:    //普通錯誤break;        case SmsManager.RESULT_ERROR_RADIO_OFF:  //無線廣播被明確地關閉break;         case SmsManager.RESULT_ERROR_NULL_PDU:          //沒有提供pdubreak;case SmsManager.RESULT_ERROR_NO_SERVICE:         //服務當前不可用break;        }      }  }, new IntentFilter(SENT_SMS_ACTION));  


處理接收狀態的PendingIntent:

//處理返回的接收狀態   String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";  //建立接收返回的接收狀態的Intent  Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION);  PendingIntent deliverPI = PendingIntent.getBroadcast(context, 0,deliverIntent, 0);  context.registerReceiver(new BroadcastReceiver() {     @Override     public void onReceive(Context _context, Intent _intent) {         Toast.makeText(context,"收信人已經成功接收", Toast.LENGTH_SHORT).show();     }  }, new IntentFilter(DELIVERED_SMS_ACTION)); 






















Android提供的系統服務之--SmsManager(簡訊管理器)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.