Android常见管理器—短信管理器 SmsManager
来源:互联网 发布:windows车载系统 编辑:程序博客网 时间:2024/06/10 06:36
SmsManager(短信管理器)是Android提供的另一个非常常见的服务,SmsManager提供了系列sendXxxMessage()方法用于发送短信。
1.所有有关方法 均在android.telephony.SmsManager包内
2.官方给出的常用方法
Public methods
String
createAppSpecificSmsToken(PendingIntent intent)
Create a single use app specific incoming SMS request for the the calling package.
ArrayList<String>
divideMessage(String text)
Divide a message text into several fragments, none bigger than the maximum SMS message size.
void
downloadMultimediaMessage(Context context, String locationUrl, Uri contentUri, Bundle configOverrides,PendingIntent downloadedIntent)
Download an MMS message from carrier by a given location URL
Bundle
getCarrierConfigValues()
Get carrier-dependent configuration values.
static SmsManager
getDefault()
Get the SmsManager associated with the default subscription id.
static int
getDefaultSmsSubscriptionId()
Get default sms subscription id
static SmsManager
getSmsManagerForSubscriptionId(int subId)
Get the the instance of the SmsManager associated with a particular subscription id
int
getSubscriptionId()
Get the associated subscription id.
void
injectSmsPdu(byte[] pdu, String format, PendingIntent receivedIntent)
Inject an SMS PDU into the android application framework.
void
sendDataMessage(String destinationAddress, String scAddress, short destinationPort, byte[] data, PendingIntentsentIntent, PendingIntent deliveryIntent)
Send a data based SMS to a specific application port.
void
sendMultimediaMessage(Context context, Uri contentUri, String locationUrl, Bundle configOverrides,PendingIntent sentIntent)
Send an MMS message
void
sendMultipartTextMessage(String destinationAddress, String scAddress, ArrayList<String> parts,ArrayList<PendingIntent> sentIntents, ArrayList<PendingIntent> deliveryIntents)
Send a multi-part text based SMS.
void
sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent,PendingIntent deliveryIntent)
Send a text based SMS.
3.常用方法详解
3.1.divideMessage 分离短信 即 当短信超过SMS消息的最大长度时,将短信分割为几块
ArrayList<String> divideMessage (String text)
Divide a message text into several fragments, none bigger than the maximum SMS message size.
text
String
: the original message. Must not be null.ArrayList<String>
an ArrayList
of strings that, in order, comprise the original messageIllegalArgumentException
if text is null3.2.getDefault 获取SmsManager对象
SmsManager getDefault ()
Get the SmsManager associated with the default subscription id. The instance will always be associated with the default subscription id, even if the default subscription id is changed.
SmsManager
the SmsManager associated with the default subscription id3.3.sendDataMessage 发送短信
void sendDataMessage (String destinationAddress, String scAddress, short destinationPort, byte[] data, PendingIntent sentIntent, PendingIntent deliveryIntent)
Send a data based SMS to a specific application port.
Note: Using this method requires that your app has the SEND_SMS
permission.
destinationAddress
String
: the address to send the message toscAddress
String
: is the service center address or null to use the current default SMSCdestinationPort
short
: the port to deliver the message todata
byte
: the body of the message to sendsentIntent
PendingIntent
: if not NULL this PendingIntent
is broadcast when the message is successfully sent, or failed. The result code will be Activity.RESULT_OK
for success, or one of these errors:RESULT_ERROR_GENERIC_FAILURE
RESULT_ERROR_RADIO_OFF
RESULT_ERROR_NULL_PDU
For
RESULT_ERROR_GENERIC_FAILURE
the sentIntent may include the extra "errorCode" containing a radio technology specific value, generally only useful for troubleshooting.The per-application based SMS control checks sentIntent. If sentIntent is NULL the caller will be checked against all unknown applications, which cause smaller number of SMS to be sent in checking period.
deliveryIntent
PendingIntent
: if not NULL this PendingIntent
is broadcast when the message is delivered to the recipient. The raw pdu of the status report is in the extended data ("pdu").IllegalArgumentException
if destinationAddress or data are empty参数说明
1)、destinationAddress——消息的目标地址
2)、scAddress——服务中心的地址or为空使用当前默认的SMSC
3)destinationPort——消息的目标端口号
4)、data——消息的主体,即消息要发送的数据
5)、sentIntent——如果不为空,当消息成功发送或失败这个PendingIntent就广播。结果代码是Activity.RESULT_OK表示成功,或RESULT_ERROR_GENERIC_FAILURE、RESULT_ERROR_RADIO_OFF、RESULT_ERROR_NULL_PDU之一表示错误。对应RESULT_ERROR_GENERIC_FAILURE,sentIntent可能包括额外的“错误代码”包含一个无线电广播技术特定的值,通常只在修复故障时有用。
每一个基于SMS的应用程序控制检测sentIntent。如果sentIntent是空,调用者将检测所有未知的应用程序,这将导致在检测的时候发送较小数量的SMS
6)、deliveryIntent——如果不为空,当消息成功传送到接收者这个PendingIntent就广播
异常:如果destinationAddress或data是空时,抛出IllegalArgumentException异常
3.4.sendMultimediaMessage
void sendMultimediaMessage (Context context, Uri contentUri, String locationUrl, Bundle configOverrides, PendingIntent sentIntent)
Send an MMS message
context
Context
: application contextcontentUri
Uri
: the content Uri from which the message pdu will be readlocationUrl
String
: the optional location url where message should be sent toconfigOverrides
Bundle
: the carrier-specific messaging configuration values to override for sending the message.sentIntent
PendingIntent
: if not NULL this PendingIntent
is broadcast when the message is successfully sent, or failedIllegalArgumentException
if contentUri is empty3.5.sendMultipartTextMessage
void sendMultipartTextMessage (String destinationAddress, String scAddress, ArrayList<String> parts, ArrayList<PendingIntent> sentIntents, ArrayList<PendingIntent> deliveryIntents)
Send a multi-part text based SMS. The callee should have already divided the message into correctly sized parts by calling divideMessage
.
Note: Using this method requires that your app has the SEND_SMS
permission.
Note: Beginning with Android 4.4 (API level 19), if and only if an app is not selected as the default SMS app, the system automatically writes messages sent using this method to the SMS Provider (the default SMS app is always responsible for writing its sent messages to the SMS Provider). For information about how to behave as the default SMS app, see Telephony
.
destinationAddress
String
: the address to send the message toscAddress
String
: is the service center address or null to use the current default SMSCparts
ArrayList
: an ArrayList
of strings that, in order, comprise the original messagesentIntents
ArrayList
: if not null, an ArrayList
of PendingIntent
s (one for each message part) that is broadcast when the corresponding message part has been sent. The result code will be Activity.RESULT_OK
for success, or one of these errors:RESULT_ERROR_GENERIC_FAILURE
RESULT_ERROR_RADIO_OFF
RESULT_ERROR_NULL_PDU
For
RESULT_ERROR_GENERIC_FAILURE
each sentIntent may include the extra "errorCode" containing a radio technology specific value, generally only useful for troubleshooting.The per-application based SMS control checks sentIntent. If sentIntent is NULL the caller will be checked against all unknown applications, which cause smaller number of SMS to be sent in checking period.
deliveryIntents
ArrayList
: if not null, an ArrayList
of PendingIntent
s (one for each message part) that is broadcast when the corresponding message part has been delivered to the recipient. The raw pdu of the status report is in the extended data ("pdu").IllegalArgumentException
if destinationAddress or data are empty参数:
1)、destinationAddress——消息的目标地址
2)、scAddress——服务中心的地址or为空使用当前默认的SMSC
3)、parts——有序的ArrayList<String>,可以重新组合为初始的消息
4)、sentIntents——跟SendDataMessage方法中一样,只不过这里的是一组PendingIntent
5)、deliverIntents——跟SendDataMessage方法中一样,只不过这里的是一组PendingIntent
异常:如果destinationAddress或data是空时,抛出IllegalArgumentException异常。
Android 官方网址:https://developer.android.google.cn/reference/android/telephony/SmsManager.html
- Android常见管理器—短信管理器 SmsManager
- android短信管理器-SmsManager
- android短信管理器(SmsManager)实例
- 短信管理器(SmsManager)
- SmsManager(短信管理器)
- Android基础入门教程——10.2 SmsManager(短信管理器)
- Android基础入门教程——10.2 SmsManager(短信管理器)
- Android开发之SmsManager(短信管理器)详解
- Android提供的系统服务之--SmsManager(短信管理器)
- Android SmsManager(短信管理器),发送短信息
- Android SmsManager 短信群发
- android 短信发送器(SmsManager)
- Android短信之SmsManager类
- android smsManager发不出短信,why?
- Android:利用SmsManager发送短信
- 初学Android,短信管理器之发送短信(六十五)
- 初学Android,短信管理器之发送短信(六十五)
- 智能短信分类管理器
- 线性代数 -- 行列式及其性质
- js arr数组排序
- javascript实用技巧,js小知识
- 助考100%,祝你取得好成绩.
- 快速排序
- Android常见管理器—短信管理器 SmsManager
- 初学$ajax()简单使用及前后台交互
- 链式复制
- Bootstrap3 移动设备优先并使用了一些HTML5元素
- JSP着重点
- 无头双链表的创建
- Distributed hash table
- winfrom以管理员身份运行程序
- 【R笔记】R机器学习(三)——多元回归