android不调用系统发送短信,android之两种方式调用短信发送接口
釋放雙眼,帶上耳機,聽聽看~!
相信很多程序員在開發程序的時候都會遇到短信調用端口的情況,今天是技術狗小編為大家帶來的關于android之兩種方式調用短信發送接口,希望對你學習這方面知識有幫助!
android之兩種方式調用短信發送接口
第一:調用系統短信接口直接發送短信;主要代碼如下:
//直接調用短信接口發短信
SmsManager smsManager = SmsManager.getDefault();
List divideContents = smsManager.divideMessage(content);
for (String text : divideContents) {
smsManager.sendTextMessage("150xxxxxxxx", null, text, sentPI, deliverPI);
}
第二:調起系統發短信功能;主要代碼如下:
Uri uri = Uri.parse("smsto:10010");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body", "102");
activity.startActivity(it);
這里主要講解第一種方法,其中大部分信息來源于互聯網:
獲取短信管理器
SmsManager smsManager = SmsManager.getDefault();
拆分短信內容(手機短信長度限制)
List divideContents = smsManager.divideMessage(content);
發送拆分后的內容
List divideContents = smsManager.divideMessage(content);
for (String text : divideContents) {
smsManager.sendTextMessage("150xxxxxxxx", null, text, sentPI, deliverPI);
}
處理返回的發送狀態
String SENT_SMS_ACTION = "SENT_SMS_ACTION";
Intent sentIntent = new Intent(SENT_SMS_ACTION);
PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, sentIntent,
0);
// register the Broadcast Receivers
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:
break;
}
}
}, new IntentFilter(SENT_SMS_ACTION));
處理返回的接收狀態
String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";
// create the deilverIntent parameter
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));
發送短信的參數說明
smsManager.sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent)
— destinationAddress:目標電話號碼
— scAddress:短信中心號碼,測試可以不填
— text: 短信內容
— sentIntent:發送 –>中國移動 –> 中國移動發送失敗 –> 返回發送成功或失敗信號 –> 后續處理 即,這個意圖包裝了短信發送狀態的信息
— deliveryIntent: 發送 –>中國移動 –> 中國移動發送成功 –> 返回對方是否收到這個信息 –> 后續處理 即:這個意圖包裝了短信是否被對方收到的狀態信息(供應商已經發送成功,但是對方沒有收到)。
通過技術狗小編介紹的android之兩種方式調用短信發送接口,相信大家都有了一定的了解,如需了解更多的相關資訊,請繼續關注技術狗吧!
總結
以上是生活随笔為你收集整理的android不调用系统发送短信,android之两种方式调用短信发送接口的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《秋池二首》第二十二句是什么
- 下一篇: 不孕不育怎么要孩子