Android常见管理器—短信管理器 SmsManager

来源:互联网 发布:windows车载系统 编辑:程序博客网 时间:2024/06/10 06:36

SmsManager(短信管理器)是Android提供的另一个非常常见的服务,SmsManager提供了系列sendXxxMessage()方法用于发送短信。


1.所有有关方法 均在android.telephony.SmsManager包内





2.官方给出的常用方法


Public methods

StringcreateAppSpecificSmsToken(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.

voiddownloadMultimediaMessage(Context context, String locationUrl, Uri contentUri, Bundle configOverrides,PendingIntent downloadedIntent)

Download an MMS message from carrier by a given location URL

BundlegetCarrierConfigValues()

Get carrier-dependent configuration values.

static SmsManagergetDefault()

Get the SmsManager associated with the default subscription id.

static intgetDefaultSmsSubscriptionId()

Get default sms subscription id

static SmsManagergetSmsManagerForSubscriptionId(int subId)

Get the the instance of the SmsManager associated with a particular subscription id

intgetSubscriptionId()

Get the associated subscription id.

voidinjectSmsPdu(byte[] pdu, String format, PendingIntent receivedIntent)

Inject an SMS PDU into the android application framework.

voidsendDataMessage(String destinationAddress, String scAddress, short destinationPort, byte[] data, PendingIntentsentIntent, PendingIntent deliveryIntent)

Send a data based SMS to a specific application port.

voidsendMultimediaMessage(Context context, Uri contentUri, String locationUrl, Bundle configOverrides,PendingIntent sentIntent)

Send an MMS message

voidsendMultipartTextMessage(String destinationAddress, String scAddress, ArrayList<String> parts,ArrayList<PendingIntent> sentIntents, ArrayList<PendingIntent> deliveryIntents)

Send a multi-part text based SMS.

voidsendTextMessage(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.

ParameterstextString: the original message. Must not be null.

ReturnsArrayList<String>an ArrayList of strings that, in order, comprise the original message

ThrowsIllegalArgumentExceptionif text is null

3.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.

ReturnsSmsManagerthe SmsManager associated with the default subscription id


3.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.

ParametersdestinationAddressString: the address to send the message to

scAddressString: is the service center address or null to use the current default SMSC

destinationPortshort: the port to deliver the message to

databyte: the body of the message to send

sentIntentPendingIntent: 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.

deliveryIntentPendingIntent: 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").

ThrowsIllegalArgumentExceptionif 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

ParameterscontextContext: application context

contentUriUri: the content Uri from which the message pdu will be read

locationUrlString: the optional location url where message should be sent to

configOverridesBundle: the carrier-specific messaging configuration values to override for sending the message.

sentIntentPendingIntent: if not NULL this PendingIntent is broadcast when the message is successfully sent, or failed

ThrowsIllegalArgumentExceptionif contentUri is empty

3.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.

ParametersdestinationAddressString: the address to send the message to

scAddressString: is the service center address or null to use the current default SMSC

partsArrayList: an ArrayList of strings that, in order, comprise the original message

sentIntentsArrayList: if not null, an ArrayList of PendingIntents (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.

deliveryIntentsArrayList: if not null, an ArrayList of PendingIntents (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").

ThrowsIllegalArgumentExceptionif 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