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


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





2.官方给出的常用方法


Public methods

String createAppSpecificSmsToken(PendingIntentintent)

Create a single use app specific incoming SMS request for the the calling package.

ArrayList<String> divideMessage(Stringtext)

Divide a message text into several fragments, none bigger than the maximum SMS message size.

void downloadMultimediaMessage(Contextcontext,StringlocationUrl,UricontentUri,BundleconfigOverrides,PendingIntentdownloadedIntent)

Download an MMS message from carrier by a given location URL

Bundle getCarrierConfigValues()

Get carrier-dependent configuration values.

staticSmsManager getDefault()

Get the SmsManager associated with the default subscription id.

static int getDefaultSmsSubscriptionId()

Get default sms subscription id

staticSmsManager 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,Stringformat,PendingIntentreceivedIntent)

Inject an SMS PDU into the android application framework.

void sendDataMessage(StringdestinationAddress,StringscAddress,
short destinationPort, byte[] data,PendingIntentsentIntent,PendingIntentdeliveryIntent)

Send a data based SMS to a specific application port.

void sendMultimediaMessage(Contextcontext,UricontentUri,StringlocationUrl,BundleconfigOverrides,PendingIntentsentIntent)

Send an MMS message

void sendMultipartTextMessage(StringdestinationAddress,StringscAddress,ArrayList<String>
parts,ArrayList<PendingIntent>
sentIntents,ArrayList<PendingIntent>
deliveryIntents)

Send a multi-part text based SMS.

void sendTextMessage(StringdestinationAddress,StringscAddress,Stringtext,PendingIntentsentIntent,PendingIntentdeliveryIntent)

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.

Parameters
text String: the original message. Must not be null.

Returns
ArrayList<String> anArrayListof strings that, in order, comprise the original message

Throws
IllegalArgumentException if 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.

Returns
SmsManager the 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 theSEND_SMSpermission.

Parameters
destinationAddress String: the address to send the message to

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

destinationPort short: the port to deliver the message to

data byte: the body of the message to send

sentIntent PendingIntent: if not NULL thisPendingIntentis broadcast when the message is successfully sent, or failed. The result code will beActivity.RESULT_OKfor success, or one of these errors:
RESULT_ERROR_GENERIC_FAILURE
RESULT_ERROR_RADIO_OFF
RESULT_ERROR_NULL_PDU
ForRESULT_ERROR_GENERIC_FAILUREthe 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 thisPendingIntentis broadcast when the message is delivered to the recipient. The raw pdu of the status report is in the extended data ("pdu").

Throws
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

Parameters
context Context: application context

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

locationUrl String: the optional location url where message should be sent to

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

sentIntent PendingIntent: if not NULL thisPendingIntentis broadcast when the message is successfully sent, or failed

Throws
IllegalArgumentException if 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 callingdivideMessage.

Note:Using this method requires that your app has theSEND_SMSpermission.

Note:Beginning with Android 4.4 (API level 19), ifand only ifan 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, seeTelephony.

Parameters
destinationAddress String: the address to send the message to

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

parts ArrayList: anArrayListof strings that, in order, comprise the original message

sentIntents ArrayList: if not null, anArrayListofPendingIntents (one for each message part) that is broadcast when the corresponding message part has been sent. The result code will beActivity.RESULT_OKfor success, or one of these errors:
RESULT_ERROR_GENERIC_FAILURE
RESULT_ERROR_RADIO_OFF
RESULT_ERROR_NULL_PDU
ForRESULT_ERROR_GENERIC_FAILUREeach 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, anArrayListofPendingIntents (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").

Throws
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


更多相关文章

  1. android在onCreate()方法中获取View的宽度与高度的方法实战
  2. 安卓消息处理机制
  3. W/System.err:at java.net.PlainDatagramSocketImpl.bind(PlainDa
  4. Android Studio获取数字签名(SHA1)的方法
  5. Android Volley:使用方法总结及实例解析
  6. eclipse下运行EasyAR官方sample的方法
  7. Android App性能信息获取方法
  8. Android Studio 检测内存泄漏与解决方法
  9. C#/Java 调用WSDL接口及方法

随机推荐

  1. Apple 再挑起戰火,控告 Samsung 15款 Andr
  2. Android撸一个转盘抽奖
  3. android 中隐藏EditText的下划线方法
  4. android:configChanges属性
  5. Android 线程超时的例子
  6. Tips: compilation and creating new pro
  7. Android应用在未启动的情况下无法收到指
  8. 一个现有Android工程作为组件加入到另一
  9. Android RadioGroup 设置默认值之后 Radi
  10. 微信ANDROID客户端-会话速度提升70%的背