SmsMessage


public class SmsMessage
extends Object

java.lang.Object
   ↳ android.telephony.SmsMessage


A Short Message Service message.

Summary

Nested classes

class SmsMessage.SubmitPdu

 

Constants

int ENCODING_16BIT

int ENCODING_7BIT

int ENCODING_8BIT

int ENCODING_KSC5601

This value is not defined in global standard.

int ENCODING_UNKNOWN

User data text encoding code unit size

String FORMAT_3GPP

Indicates a 3GPP format SMS message.

String FORMAT_3GPP2

Indicates a 3GPP2 format SMS message.

int MAX_USER_DATA_BYTES

The maximum number of payload bytes per message

int MAX_USER_DATA_BYTES_WITH_HEADER

The maximum number of payload bytes per message if a user data header is present.

int MAX_USER_DATA_SEPTETS

The maximum number of payload septets per message

int MAX_USER_DATA_SEPTETS_WITH_HEADER

The maximum number of payload septets per message if a user data header is present.

Public methods

static int[] calculateLength(CharSequence msgBody, boolean use7bitOnly)

Calculates the number of SMS's required to encode the message body and the number of characters remaining until the next message.

static int[] calculateLength(String messageBody, boolean use7bitOnly)

Calculates the number of SMS's required to encode the message body and the number of characters remaining until the next message, given the current encoding.

static SmsMessage createFromPdu(byte[] pdu)

This method was deprecated in API level 23. Use createFromPdu(byte[], java.lang.String) instead.

static SmsMessage createFromPdu(byte[] pdu, String format)

Create an SmsMessage from a raw PDU with the specified message format.

String getDisplayMessageBody()

Returns the message body, or email message body if this message was from an email gateway.

String getDisplayOriginatingAddress()

Returns the originating address, or email from address if this message was from an email gateway.

String getEmailBody()
String getEmailFrom()
int getIndexOnIcc()

Returns the record index of the message on the ICC (1-based index).

int getIndexOnSim()

This method was deprecated in API level 15. Use getIndexOnIcc instead.

String getMessageBody()

Returns the message body as a String, if it exists and is text based.

SmsMessage.MessageClass getMessageClass()

Returns the class of this message.

String getOriginatingAddress()

Returns the originating address (sender) of this SMS message in String form or null if unavailable.

byte[] getPdu()

Returns the raw PDU for the message.

int getProtocolIdentifier()

Get protocol identifier.

String getPseudoSubject()

Unofficial convention of a subject line enclosed in parens empty string if not present

String getServiceCenterAddress()

Returns the address of the SMS service center that relayed this message or null if there is none.

int getStatus()

GSM: For an SMS-STATUS-REPORT message, this returns the status field from the status report.

int getStatusOnIcc()

Returns the status of the message on the ICC (read, unread, sent, unsent).

int getStatusOnSim()

This method was deprecated in API level 15. Use getStatusOnIcc instead.

static SmsMessage.SubmitPdu getSubmitPdu(String scAddress, String destinationAddress, String message, boolean statusReportRequested)

Gets an SMS-SUBMIT PDU for a destination address and a message.

static SmsMessage.SubmitPdu getSubmitPdu(String scAddress, String destinationAddress, short destinationPort, byte[] data, boolean statusReportRequested)

Gets an SMS-SUBMIT PDU for a data message to a destination address & port.

static int getTPLayerLengthForPDU(String pdu)

Get the TP-Layer-Length for the given SMS-SUBMIT PDU Basically, the length in bytes (not hex chars) less the SMSC header FIXME: This method is only used by a CTS test case that isn't run on CDMA devices.

long getTimestampMillis()

Returns the service centre timestamp in currentTimeMillis() format

byte[] getUserData()

returns the user data section minus the user data header if one was present.

boolean isCphsMwiMessage()

Returns true for CPHS MWI toggle message.

boolean isEmail()

Returns true if message is an email.

boolean isMWIClearMessage()

returns true if this message is a CPHS voicemail / message waiting indicator (MWI) clear message

boolean isMWISetMessage()

returns true if this message is a CPHS voicemail / message waiting indicator (MWI) set message

boolean isMwiDontStore()

returns true if this message is a "Message Waiting Indication Group: Discard Message" notification and should not be stored.

boolean isReplace()

See TS 23.040 9.2.3.9 returns true if this is a "replace short message" SMS

boolean isReplyPathPresent()

Returns true iff the TP-Reply-Path bit is set in this message.

boolean isStatusReportMessage()

Return true iff the message is a SMS-STATUS-REPORT message.

Inherited methods

Constants

ENCODING_16BIT

Added in API level 4
public static final int ENCODING_16BIT

Constant Value: 3 (0x00000003)

ENCODING_7BIT

Added in API level 4
public static final int ENCODING_7BIT

Constant Value: 1 (0x00000001)

ENCODING_8BIT

Added in API level 4
public static final int ENCODING_8BIT

Constant Value: 2 (0x00000002)

ENCODING_KSC5601

Added in API level 33
public static final int ENCODING_KSC5601

This value is not defined in global standard. Only in Korea, this is used.

Constant Value: 4 (0x00000004)

ENCODING_UNKNOWN

Added in API level 4
public static final int ENCODING_UNKNOWN

User data text encoding code unit size

Constant Value: 0 (0x00000000)

FORMAT_3GPP

Added in API level 28
public static final String FORMAT_3GPP

Indicates a 3GPP format SMS message.

Constant Value: "3gpp"

FORMAT_3GPP2

Added in API level 28
public static final String FORMAT_3GPP2

Indicates a 3GPP2 format SMS message.

Constant Value: "3gpp2"

MAX_USER_DATA_BYTES

Added in API level 4
public static final int MAX_USER_DATA_BYTES

The maximum number of payload bytes per message

Constant Value: 140 (0x0000008c)

MAX_USER_DATA_BYTES_WITH_HEADER

Added in API level 4
public static final int MAX_USER_DATA_BYTES_WITH_HEADER

The maximum number of payload bytes per message if a user data header is present. This assumes the header only contains the CONCATENATED_8_BIT_REFERENCE element.

Constant Value: 134 (0x00000086)

MAX_USER_DATA_SEPTETS

Added in API level 4
public static final int MAX_USER_DATA_SEPTETS

The maximum number of payload septets per message

Constant Value: 160 (0x000000a0)

MAX_USER_DATA_SEPTETS_WITH_HEADER

Added in API level 4
public static final int MAX_USER_DATA_SEPTETS_WITH_HEADER

The maximum number of payload septets per message if a user data header is present. This assumes the header only contains the CONCATENATED_8_BIT_REFERENCE element.

Constant Value: 153 (0x00000099)

Public methods

calculateLength

Added in API level 4
public static int[] calculateLength (CharSequence msgBody, 
                boolean use7bitOnly)

Calculates the number of SMS's required to encode the message body and the number of characters remaining until the next message.

Parameters
msgBody CharSequence: the message to encode

use7bitOnly boolean: if true, characters that are not part of the radio-specific 7-bit encoding are counted as single space chars. If false, and if the messageBody contains non-7-bit encodable characters, length is calculated using a 16-bit encoding.

Returns
int[] an int[6] with int[0] being the number of SMS's required, int[1] the number of code units used, and int[2] is the number of code units remaining until the next message. int[3] is an indicator of the encoding code unit size (see the ENCODING_* definitions in SmsConstants). int[4] is the GSM national language table to use, or 0 for the default 7-bit alphabet. int[5] The GSM national language shift table to use, or 0 for the default 7-bit extension table.

calculateLength

Added in API level 4
public static int[] calculateLength (String messageBody, 
                boolean use7bitOnly)

Calculates the number of SMS's required to encode the message body and the number of characters remaining until the next message, given the current encoding.

Parameters
messageBody String: the message to encode

use7bitOnly boolean: if true, characters that are not part of the radio specific (GSM / CDMA) alphabet encoding are converted to as a single space characters. If false, a messageBody containing non-GSM or non-CDMA alphabet characters are encoded using 16-bit encoding.

Returns
int[] an int[4] with int[0] being the number of SMS's required, int[1] the number of code units used, and int[2] is the number of code units remaining until the next message. int[3] is the encoding type that should be used for the message.

createFromPdu

Added in API level 4
Deprecated in API level 23
public static SmsMessage createFromPdu (byte[] pdu)

This method was deprecated in API level 23.
Use createFromPdu(byte[], java.lang.String) instead.

Create an SmsMessage from a raw PDU. Guess format based on Voice technology first, if it fails use other format. All applications which handle incoming SMS messages by processing the SMS_RECEIVED_ACTION broadcast intent must now pass the new format String extra from the intent into the new method createFromPdu(byte[], String) which takes an extra format parameter. This is required in order to correctly decode the PDU on devices that require support for both 3GPP and 3GPP2 formats at the same time, such as dual-mode GSM/CDMA and CDMA/LTE phones.

Parameters
pdu byte

Returns
SmsMessage

createFromPdu

Added in API level 23
public static SmsMessage createFromPdu (byte[] pdu, 
                String format)

Create an SmsMessage from a raw PDU with the specified message format. The message format is passed in the Telephony.Sms.Intents.SMS_RECEIVED_ACTION as the format String extra, and will be either "3gpp" for GSM/UMTS/LTE messages in 3GPP format or "3gpp2" for CDMA/LTE messages in 3GPP2 format.

Parameters
pdu byte: the message PDU from the Telephony.Sms.Intents.SMS_RECEIVED_ACTION intent

format String: the format extra from the Telephony.Sms.Intents.SMS_RECEIVED_ACTION intent

Returns
SmsMessage

getDisplayMessageBody

Added in API level 4
public String getDisplayMessageBody ()

Returns the message body, or email message body if this message was from an email gateway. Returns null if message body unavailable.

Returns
String

getDisplayOriginatingAddress

Added in API level 4
public String getDisplayOriginatingAddress ()

Returns the originating address, or email from address if this message was from an email gateway. Returns null if originating address unavailable.

Returns
String

getEmailBody

Added in API level 4
public String getEmailBody ()

Returns
String if isEmail() is true, body of the email sent through the gateway. null otherwise

getEmailFrom

Added in API level 4
public String getEmailFrom ()

Returns
String if isEmail() is true, email from address of email sent through the gateway. null otherwise

getIndexOnIcc

Added in API level 4
public int getIndexOnIcc ()

Returns the record index of the message on the ICC (1-based index).

Returns
int the record index of the message on the ICC, or -1 if this SmsMessage was not created from a ICC SMS EF record.

getIndexOnSim

Added in API level 4
Deprecated in API level 15
public int getIndexOnSim ()

This method was deprecated in API level 15.
Use getIndexOnIcc instead.

Returns the record index of the message on the SIM (1-based index).

Returns
int the record index of the message on the SIM, or -1 if this SmsMessage was not created from a SIM SMS EF record.

getMessageBody

Added in API level 4
public String getMessageBody ()

Returns the message body as a String, if it exists and is text based.

Returns
String message body if there is one, otherwise null

getMessageClass

Added in API level 4
public SmsMessage.MessageClass getMessageClass ()

Returns the class of this message.

Returns
SmsMessage.MessageClass

getOriginatingAddress

Added in API level 4
public String getOriginatingAddress ()

Returns the originating address (sender) of this SMS message in String form or null if unavailable.

If the address is a GSM-formatted address, it will be in a format specified by 3GPP 23.040 Sec 9.1.2.5. If it is a CDMA address, it will be a format specified by 3GPP2 C.S005-D Table 2.7.1.3.2.4-2. The choice of format is carrier-specific, so callers of the should be careful to avoid assumptions about the returned content.

Returns
String a String representation of the address; null if unavailable.

getPdu

Added in API level 4
public byte[] getPdu ()

Returns the raw PDU for the message.

Returns
byte[] the raw PDU for the message.

getProtocolIdentifier

Added in API level 4
public int getProtocolIdentifier ()

Get protocol identifier.

Returns
int

getPseudoSubject

Added in API level 4
public String getPseudoSubject ()

Unofficial convention of a subject line enclosed in parens empty string if not present

Returns
String

getServiceCenterAddress

Added in API level 4
public String getServiceCenterAddress ()

Returns the address of the SMS service center that relayed this message or null if there is none.

Returns
String

getStatus

Added in API level 4
public int getStatus ()

GSM: For an SMS-STATUS-REPORT message, this returns the status field from the status report. This field indicates the status of a previously submitted SMS, if requested. See TS 23.040, 9.2.3.15 TP-Status for a description of values. CDMA: For not interfering with status codes from GSM, the value is shifted to the bits 31-16. The value is composed of an error class (bits 25-24) and a status code (bits 23-16). Possible codes are described in C.S0015-B, v2.0, 4.5.21.

Returns
int 0 for GSM or 2 shifted left by 16 for CDMA indicates the previously sent message was received. See TS 23.040, 9.2.3.15 and C.S0015-B, v2.0, 4.5.21 for a description of other possible values.

getStatusOnIcc

Added in API level 4
public int getStatusOnIcc ()

Returns the status of the message on the ICC (read, unread, sent, unsent).

Returns
int the status of the message on the ICC. These are: SmsManager.STATUS_ON_ICC_FREE SmsManager.STATUS_ON_ICC_READ SmsManager.STATUS_ON_ICC_UNREAD SmsManager.STATUS_ON_ICC_SEND SmsManager.STATUS_ON_ICC_UNSENT

getStatusOnSim

Added in API level 4
Deprecated in API level 15
public int getStatusOnSim ()

This method was deprecated in API level 15.
Use getStatusOnIcc instead.

Returns the status of the message on the SIM (read, unread, sent, unsent).

Returns
int the status of the message on the SIM. These are: SmsManager.STATUS_ON_SIM_FREE SmsManager.STATUS_ON_SIM_READ SmsManager.STATUS_ON_SIM_UNREAD SmsManager.STATUS_ON_SIM_SEND SmsManager.STATUS_ON_SIM_UNSENT

getSubmitPdu

Added in API level 4
public static SmsMessage.SubmitPdu getSubmitPdu (String scAddress, 
                String destinationAddress, 
                String message, 
                boolean statusReportRequested)

Gets an SMS-SUBMIT PDU for a destination address and a message. This method will not attempt to use any GSM national language 7 bit encodings.

Parameters
scAddress String: Service Centre address. Null means use default.

destinationAddress String: the address of the destination for the message.

message String: string representation of the message payload.

statusReportRequested boolean: indicates whether a report is requested for this message.

Returns
SmsMessage.SubmitPdu a SubmitPdu containing the encoded SC address if applicable and the encoded message. Returns null on encode error.

getSubmitPdu

Added in API level 4
public static SmsMessage.SubmitPdu getSubmitPdu (String scAddress, 
                String destinationAddress, 
                short destinationPort, 
                byte[] data, 
                boolean statusReportRequested)

Gets an SMS-SUBMIT PDU for a data message to a destination address & port. This method will not attempt to use any GSM national language 7 bit encodings.

Parameters
scAddress String: Service Centre address. Null means use default.

destinationAddress String: the address of the destination for the message.

destinationPort short: the port to deliver the message to at the destination.

data byte: the data for the message.

statusReportRequested boolean: indicates whether a report is requested for this message.

Returns
SmsMessage.SubmitPdu a SubmitPdu containing the encoded SC address if applicable and the encoded message. Returns null on encode error.

getTPLayerLengthForPDU

Added in API level 4
public static int getTPLayerLengthForPDU (String pdu)

Get the TP-Layer-Length for the given SMS-SUBMIT PDU Basically, the length in bytes (not hex chars) less the SMSC header FIXME: This method is only used by a CTS test case that isn't run on CDMA devices. We should probably deprecate it and remove the obsolete test case.

Parameters
pdu String

Returns
int

getTimestampMillis

Added in API level 4
public long getTimestampMillis ()

Returns the service centre timestamp in currentTimeMillis() format

Returns
long

getUserData

Added in API level 4
public byte[] getUserData ()

returns the user data section minus the user data header if one was present.

Returns
byte[]

isCphsMwiMessage

Added in API level 4
public boolean isCphsMwiMessage ()

Returns true for CPHS MWI toggle message.

Returns
boolean true if this is a CPHS MWI toggle message See CPHS 4.2 section B.4.2

isEmail

Added in API level 4
public boolean isEmail ()

Returns true if message is an email.

Returns
boolean true if this message came through an email gateway and email sender / subject / parsed body are available

isMWIClearMessage

Added in API level 4
public boolean isMWIClearMessage ()

returns true if this message is a CPHS voicemail / message waiting indicator (MWI) clear message

Returns
boolean

isMWISetMessage

Added in API level 4
public boolean isMWISetMessage ()

returns true if this message is a CPHS voicemail / message waiting indicator (MWI) set message

Returns
boolean

isMwiDontStore

Added in API level 4
public boolean isMwiDontStore ()

returns true if this message is a "Message Waiting Indication Group: Discard Message" notification and should not be stored.

Returns
boolean

isReplace

Added in API level 4
public boolean isReplace ()

See TS 23.040 9.2.3.9 returns true if this is a "replace short message" SMS

Returns
boolean

isReplyPathPresent

Added in API level 4
public boolean isReplyPathPresent ()

Returns true iff the TP-Reply-Path bit is set in this message.

Returns
boolean

isStatusReportMessage

Added in API level 4
public boolean isStatusReportMessage ()

Return true iff the message is a SMS-STATUS-REPORT message.

Returns
boolean