public class ShortMessageManager extends Object
This class is a singleton class. Applications can use the getInstance method to obtain an instance
and then use the instance to invoke the methods to manage SMS operations, for example, sending SMS messages
or checking whether a device supports SMS.
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
int |
getDefaultSmsSlotId() |
Obtains the default SIM card for sending SMS messages.
|
static ShortMessageManager |
getInstance(Context context) |
Obtains an instance of
ShortMessageManager. |
String |
getSmscAddr(int slotId) |
Obtains the SMSC address based on a specified slot ID.
|
boolean |
hasSmsCapability() |
Returns whether a device is capable of sending and receiving SMS messages.
|
void |
sendMessage(int slotId,
String destinationHost,
String serviceCenter,
byte[] data,
short destinationPort,
ISendShortMessageCallback sendCallback,
IDeliveryShortMessageCallback deliveryCallback) |
Sends a data SMS message.
|
void |
sendMessage(int slotId,
String destinationHost,
String serviceCenter,
String content,
ISendShortMessageCallback sendCallback,
IDeliveryShortMessageCallback deliveryCallback) |
Sends a text SMS message.
|
boolean |
setSmscAddr(int slotId,
String smscAddr) |
Sets the address for the Short Message Service Center (SMSC) based on a specified slot ID.
|
ArrayList<String> |
splitMessage(String content) |
Splits a long SMS message into multiple fragments.
|
public static ShortMessageManager getInstance(Context context)
ShortMessageManager.
If the ID of the default SIM card used for sending SMS messages changes, the ShortMessageManager
instance changes accordingly. You can obtain the default SIM card by using getDefaultSmsSlotId.
context - Indicates the context of the application to invoke.ShortMessageManager.public int getDefaultSmsSlotId()
0 if the default SIM card for sending SMS messages is in card slot 1;
returns 1 if the default SIM card for sending SMS messages is in card slot 2.public boolean hasSmsCapability()
true if the device is capable of sending and receiving SMS messages;
returns false otherwise.public ArrayList<String> splitMessage(String content)
If the length of an SMS message exceeds the maximum length allowed (140 bytes), the SMS message is split into multiple segments for processing.
Applications must have the ohos.permission.SEND_MESSAGES permission to call this method.
content - Indicates the short message content, which cannot be null.null.sendMessage(int, java.lang.String, java.lang.String, java.lang.String, ohos.telephony.ISendShortMessageCallback, ohos.telephony.IDeliveryShortMessageCallback)public void sendMessage(int slotId,
String destinationHost,
String serviceCenter,
String content,
ISendShortMessageCallback sendCallback,
IDeliveryShortMessageCallback deliveryCallback)
This method checks whether the length of an SMS message exceeds the maximum length. If the maximum length is exceeded, the SMS message is split into multiple parts and sent separately.
You need to obtain the following permission before calling this method:
SystemPermission.SEND_MESSAGES
slotId - Indicates the ID of the SIM card slot used for sending the SMS message.destinationHost - Indicates the address to which the SMS message is sent.serviceCenter - Indicates the short message service center (SMSC) address. If the value is
null, the default SMSC address of the SIM card is used.content - Indicates the content of the SMS message. If the length of the content exceeds the
maximum length, this SMS message is split into multiple parts.sendCallback - Indicates the callback invoked after the SMS message is sent.deliveryCallback - Indicates the callback invoked after the SMS message is delivered.public void sendMessage(int slotId,
String destinationHost,
String serviceCenter,
byte[] data,
short destinationPort,
ISendShortMessageCallback sendCallback,
IDeliveryShortMessageCallback deliveryCallback)
You need to obtain the following permission before calling this method:
SystemPermission.SEND_MESSAGES
slotId - Indicates the ID of the SIM card slot used for sending the SMS message.destinationHost - Indicates the address to which the SMS message is sent.serviceCenter - Indicates the SMSC address. If the value is null, the default
SMSC address of the SIM card
is used.data - Indicates the content of the SMS message.destinationPort - Indicates the destination port for sending the SMS message.sendCallback - Indicates the callback invoked after the SMS message is sent.deliveryCallback - Indicates the callback invoked after the SMS message is delivered.IllegalArgumentException - if the argument data is null or slotId is invalid.public boolean setSmscAddr(int slotId,
String smscAddr)
Permissions: SystemPermission.SET_TELEPHONY_STATE
slotId - Indicates the ID of the slot holding the SIM card for sending SMS messages.smscAddr - Indicates the SMSC address.true if the operation is successful; returns false otherwise.public String getSmscAddr(int slotId)
Permissions: SystemPermission.GET_TELEPHONY_STATE
slotId - Indicates the ID of the slot holding the SIM card for sending SMS messages.