Added in API level 1

PhoneNumberUtils


open class PhoneNumberUtils
kotlin.Any
   ↳ android.telephony.PhoneNumberUtils

Various utilities for dealing with phone number strings.

Summary

Constants
static Int

static Int

static Int

Japanese formatting

static Int

NANP formatting

static Int

The current locale is unknown, look for a country code or don't format

static Char

static Int

static Int

static Char

static Char

Public constructors

Public methods
open static Unit
addTtsSpan(s: Spannable!, start: Int, endExclusive: Int)

Attach a TtsSpan to the supplied Spannable at the indicated location, annotating that location as containing a phone number.

open static Boolean
areSamePhoneNumber(number1: String, number2: String, defaultCountryIso: String)

Determines if two phone numbers are the same.

open static String!
calledPartyBCDFragmentToString(bytes: ByteArray!, offset: Int, length: Int)

Like calledPartyBCDToString, but field does not start with a TOA byte.

open static String!
calledPartyBCDFragmentToString(bytes: ByteArray!, offset: Int, length: Int, bcdExtType: Int)

Like calledPartyBCDToString, but field does not start with a TOA byte.

open static String!
calledPartyBCDToString(bytes: ByteArray!, offset: Int, length: Int)

3GPP TS 24.

open static String!
calledPartyBCDToString(bytes: ByteArray!, offset: Int, length: Int, bcdExtType: Int)

3GPP TS 24.

open static Boolean
compare(a: String!, b: String!)

Compare phone numbers a and b, return true if they're identical enough for caller ID purposes.

open static Boolean
compare(context: Context!, a: String!, b: String!)

Compare phone numbers a and b, and return true if they're identical enough for caller ID purposes.

open static String!

Translates any alphabetic letters (i.e. [A-Za-z]) in the specified phone number into the equivalent numeric digits, according to the phone keypad letter mapping described in ITU E.

open static TtsSpan!
createTtsSpan(phoneNumberString: String!)

Create a TtsSpan for the supplied String.

open static CharSequence!

Wrap the supplied CharSequence with a TtsSpan, annotating it as containing a phone number in its entirety.

open static String!

Extracts the network address portion and canonicalizes (filters out separators.) Network address portion is everything up to DTMF control digit separators (pause or wait), but without non-dialable characters.

open static String!

Extracts the post-dial sequence of DTMF control digits, pauses, and waits.

open static Unit

Formats a phone number in-place using the Japanese formatting rules.

open static Unit

Formats a phone number in-place using the NANP formatting rules.

open static String!
formatNumber(source: String!)

Breaks the given number down and formats it according to the rules for the country the number is from.

open static Unit
formatNumber(text: Editable!, defaultFormattingType: Int)

Formats a phone number in-place.

open static String!
formatNumber(phoneNumber: String!, defaultCountryIso: String!)

Format a phone number.

open static String!
formatNumber(phoneNumber: String!, phoneNumberE164: String!, defaultCountryIso: String!)

Format the phone number only if the given number hasn't been formatted.

open static String!
formatNumberToE164(phoneNumber: String!, defaultCountryIso: String!)

Formats the specified phoneNumber to the E.

open static String!
formatNumberToRFC3966(phoneNumber: String!, defaultCountryIso: String!)

Formats the specified phoneNumber to the RFC3966 representation.

open static Int

Returns the phone number formatting type for the given locale.

open static String!
getNumberFromIntent(intent: Intent!, context: Context!)

Extracts the phone number from an Intent.

open static String!
getStrippedReversed(phoneNumber: String!)

Returns the network portion reversed.

static Boolean

True if c is ISO-LATIN characters 0-9, *, #

static Boolean

True if c is ISO-LATIN characters 0-9, *, # , +, WILD

open static Boolean

Checks a given number against the list of emergency numbers provided by the RIL and SIM card.

open static Boolean
isGlobalPhoneNumber(phoneNumber: String!)

open static Boolean

True if c is ISO-LATIN characters 0-9

open static Boolean
isLocalEmergencyNumber(context: Context!, number: String!)

Checks if a given number is an emergency number for the country that the user is in.

static Boolean

True if c is ISO-LATIN characters 0-9, *, # , +, WILD, WAIT, PAUSE

static Boolean

True if c is ISO-LATIN characters 0-9, *, # , + (no WILD)

static Boolean

This any anything to the right of this char is part of the post-dial string (eg this is PAUSE or WAIT)

open static Boolean

isVoiceMailNumber: checks a given number against the voicemail number provided by the RIL and SIM card.

open static Boolean

Return true iff the network portion of address is, as far as we can tell on the device, suitable for use as an SMS destination address.

open static Boolean

Check if the number is for Wireless Priority Service call.

open static ByteArray!

Note: calls extractNetworkPortion(), so do not use for SIM EF[ADN] style records Returns null if network portion is empty.

open static ByteArray!

Same as networkPortionToCalledPartyBCD, but includes a one-byte length prefix.

open static String!
normalizeNumber(phoneNumber: String!)

Normalize a phone number by removing the characters other than digits.

open static ByteArray!

Convert a dialing number to BCD byte array

open static ByteArray!
numberToCalledPartyBCD(number: String!, bcdExtType: Int)

Convert a dialing number to BCD byte array

open static String!

Replaces all unicode(e.g. Arabic, Persian) digits with their decimal digit equivalents.

open static String!

Basically: makes sure there's a + in front of a TOA_International number Returns null if s == null

open static String!
stripSeparators(phoneNumber: String!)

Strips separators from a phone number string.

open static String!
toCallerIDMinMatch(phoneNumber: String!)

Returns the rightmost minimum matched characters in the network portion in *reversed* order This can be used to do a database lookup against the column that stores getStrippedReversed() Returns null if phoneNumber == null

open static Int

Returns the TOA for the given dial string Basically, returns TOA_International if there's a + prefix

Constants

BCD_EXTENDED_TYPE_CALLED_PARTY

Added in API level 28
static val BCD_EXTENDED_TYPE_CALLED_PARTY: Int
Value: 2

BCD_EXTENDED_TYPE_EF_ADN

Added in API level 28
static val BCD_EXTENDED_TYPE_EF_ADN: Int
Value: 1

FORMAT_JAPAN

Added in API level 3
static val FORMAT_JAPAN: Int

Japanese formatting

Value: 2

FORMAT_NANP

Added in API level 1
static val FORMAT_NANP: Int

NANP formatting

Value: 1

FORMAT_UNKNOWN

Added in API level 1
static val FORMAT_UNKNOWN: Int

The current locale is unknown, look for a country code or don't format

Value: 0

PAUSE

Added in API level 1
static val PAUSE: Char
Value: 44

TOA_International

Added in API level 1
static val TOA_International: Int
Value: 145

TOA_Unknown

Added in API level 1
static val TOA_Unknown: Int
Value: 129

WAIT

Added in API level 1
static val WAIT: Char
Value: 59

WILD

Added in API level 1
static val WILD: Char
Value: 78

Public constructors

PhoneNumberUtils

PhoneNumberUtils()

Public methods

addTtsSpan

Added in API level 23
open static fun addTtsSpan(
    s: Spannable!,
    start: Int,
    endExclusive: Int
): Unit

Attach a TtsSpan to the supplied Spannable at the indicated location, annotating that location as containing a phone number.

Parameters
s Spannable!: A Spannable to annotate.
start Int: The starting character position of the phone number in s.
endExclusive Int: The position after the ending character in the phone number s.

areSamePhoneNumber

Added in API level 31
open static fun areSamePhoneNumber(
    number1: String,
    number2: String,
    defaultCountryIso: String
): Boolean

Determines if two phone numbers are the same.

Matching is based on compare(java.lang.String,java.lang.String), matching takes into account national dialing plans rather than simply matching the last 7 digits of the two phone numbers. As a result, it is expected that some numbers which would match using the previous method will no longer match using this new approach.

Parameters
number1 String: This value cannot be null.
number2 String: This value cannot be null.
defaultCountryIso String: The lowercase two letter ISO 3166-1 country code. Used when parsing the phone numbers where it is not possible to determine the country associated with a phone number based on the number alone. It is recommended to pass in TelephonyManager#getNetworkCountryIso(). This value cannot be null.
Return
Boolean True if the two given phone number are same.

calledPartyBCDFragmentToString

Added in API level 1
Deprecated in API level 28
open static fun calledPartyBCDFragmentToString(
    bytes: ByteArray!,
    offset: Int,
    length: Int
): String!

Deprecated: use calledPartyBCDFragmentToString(byte[],int,int,int) instead. Calling this method is equivalent to calling calledPartyBCDFragmentToString(byte[],int,int,int) with BCD_EXTENDED_TYPE_EF_ADN as the extended type.

Like calledPartyBCDToString, but field does not start with a TOA byte. For example: SIM ADN extension fields

calledPartyBCDFragmentToString

Added in API level 28
open static fun calledPartyBCDFragmentToString(
    bytes: ByteArray!,
    offset: Int,
    length: Int,
    bcdExtType: Int
): String!

Like calledPartyBCDToString, but field does not start with a TOA byte. For example: SIM ADN extension fields

Parameters
bcdExtType Int: Value is android.telephony.PhoneNumberUtils#BCD_EXTENDED_TYPE_EF_ADN, or android.telephony.PhoneNumberUtils#BCD_EXTENDED_TYPE_CALLED_PARTY

calledPartyBCDToString

Added in API level 1
Deprecated in API level 28
open static fun calledPartyBCDToString(
    bytes: ByteArray!,
    offset: Int,
    length: Int
): String!

Deprecated: use calledPartyBCDToString(byte[],int,int,int) instead. Calling this method is equivalent to calling calledPartyBCDToString(byte[],int,int) with BCD_EXTENDED_TYPE_EF_ADN as the extended type.

3GPP TS 24.008 10.5.4.7 Called Party BCD Number See Also TS 51.011 10.5.1 "dialing number/ssc string" and TS 11.11 "10.3.1 EF adn (Abbreviated dialing numbers)"

Parameters
bytes ByteArray!: the data buffer
offset Int: should point to the TOA (aka. TON/NPI) octet after the length byte
length Int: is the number of bytes including TOA byte and must be at least 2
Return
String! partial string on invalid decode

calledPartyBCDToString

Added in API level 28
open static fun calledPartyBCDToString(
    bytes: ByteArray!,
    offset: Int,
    length: Int,
    bcdExtType: Int
): String!

3GPP TS 24.008 10.5.4.7 Called Party BCD Number See Also TS 51.011 10.5.1 "dialing number/ssc string" and TS 11.11 "10.3.1 EF adn (Abbreviated dialing numbers)"

Parameters
bytes ByteArray!: the data buffer
offset Int: should point to the TOA (aka. TON/NPI) octet after the length byte
length Int: is the number of bytes including TOA byte and must be at least 2
bcdExtType Int: used to determine the extended bcd coding Value is android.telephony.PhoneNumberUtils#BCD_EXTENDED_TYPE_EF_ADN, or android.telephony.PhoneNumberUtils#BCD_EXTENDED_TYPE_CALLED_PARTY

compare

Added in API level 1
Deprecated in API level 31
open static fun compare(
    a: String!,
    b: String!
): Boolean

Deprecated: use areSamePhoneNumber(java.lang.String,java.lang.String,java.lang.String) instead

Compare phone numbers a and b, return true if they're identical enough for caller ID purposes.

compare

Added in API level 5
Deprecated in API level 31
open static fun compare(
    context: Context!,
    a: String!,
    b: String!
): Boolean

Deprecated: use areSamePhoneNumber(java.lang.String,java.lang.String,java.lang.String) instead

Compare phone numbers a and b, and return true if they're identical enough for caller ID purposes. Checks a resource to determine whether to use a strict or loose comparison algorithm.

convertKeypadLettersToDigits

Added in API level 1
open static fun convertKeypadLettersToDigits(input: String!): String!

Translates any alphabetic letters (i.e. [A-Za-z]) in the specified phone number into the equivalent numeric digits, according to the phone keypad letter mapping described in ITU E.161 and ISO/IEC 9995-8.

Return
String! the input string, with alpha letters converted to numeric digits using the phone keypad letter mapping. For example, an input of "1-800-GOOG-411" will return "1-800-4664-411".

createTtsSpan

Added in API level 23
open static fun createTtsSpan(phoneNumberString: String!): TtsSpan!

Create a TtsSpan for the supplied String.

Parameters
phoneNumberString String!: A String the entirety of which represents a phone number.
Return
TtsSpan! A TtsSpan for {@param phoneNumberString}.

createTtsSpannable

Added in API level 23
open static fun createTtsSpannable(phoneNumber: CharSequence!): CharSequence!

Wrap the supplied CharSequence with a TtsSpan, annotating it as containing a phone number in its entirety.

Parameters
phoneNumber CharSequence!: A CharSequence the entirety of which represents a phone number.
Return
CharSequence! A CharSequence with appropriate annotations.

extractNetworkPortion

Added in API level 1
open static fun extractNetworkPortion(phoneNumber: String!): String!

Extracts the network address portion and canonicalizes (filters out separators.) Network address portion is everything up to DTMF control digit separators (pause or wait), but without non-dialable characters. Please note that the GSM wild character is allowed in the result. This must be resolved before dialing. Returns null if phoneNumber == null

extractPostDialPortion

Added in API level 1
open static fun extractPostDialPortion(phoneNumber: String!): String!

Extracts the post-dial sequence of DTMF control digits, pauses, and waits. Strips separators. This string may be empty, but will not be null unless phoneNumber == null. Returns null if phoneNumber == null

formatJapaneseNumber

Added in API level 3
Deprecated in API level 21
open static fun formatJapaneseNumber(text: Editable!): Unit

Deprecated: Use link #formatNumber(String phoneNumber, String defaultCountryIso) instead

Formats a phone number in-place using the Japanese formatting rules. Numbers will be formatted as:

03-xxxx-xxxx 090-xxxx-xxxx 0120-xxx-xxx +81-3-xxxx-xxxx +81-90-xxxx-xxxx

Parameters
text Editable!: the number to be formatted, will be modified with the formatting

formatNanpNumber

Added in API level 1
Deprecated in API level 21
open static fun formatNanpNumber(text: Editable!): Unit

Deprecated: Use link #formatNumber(String phoneNumber, String defaultCountryIso) instead

Formats a phone number in-place using the NANP formatting rules. Numbers will be formatted as:

xxxxx xxx-xxxx xxx-xxx-xxxx 1-xxx-xxx-xxxx +1-xxx-xxx-xxxx

Parameters
text Editable!: the number to be formatted, will be modified with the formatting

formatNumber

Added in API level 1
Deprecated in API level 21
open static fun formatNumber(source: String!): String!

Deprecated: Use link #formatNumber(String phoneNumber, String defaultCountryIso) instead

Breaks the given number down and formats it according to the rules for the country the number is from.

Parameters
source String!: The phone number to format
Return
String! A locally acceptable formatting of the input, or the raw input if formatting rules aren't known for the number

formatNumber

Added in API level 1
Deprecated in API level 21
open static fun formatNumber(
    text: Editable!,
    defaultFormattingType: Int
): Unit

Deprecated: Use link #formatNumber(String phoneNumber, String defaultCountryIso) instead

Formats a phone number in-place. Currently FORMAT_JAPAN and FORMAT_NANP is supported as a second argument.

Parameters
text Editable!: The number to be formatted, will be modified with the formatting
defaultFormattingType Int: The default formatting rules to apply if the number does not begin with +[country_code]

formatNumber

Added in API level 21
open static fun formatNumber(
    phoneNumber: String!,
    defaultCountryIso: String!
): String!

Format a phone number.

If the given number doesn't have the country code, the phone will be formatted to the default country's convention.

Parameters
phoneNumber String!: the number to be formatted.
defaultCountryIso String!: the ISO 3166-1 two letters country code whose convention will be used if the given number doesn't have the country code.
Return
String! the formatted number, or null if the given number is not valid.

formatNumber

Added in API level 21
open static fun formatNumber(
    phoneNumber: String!,
    phoneNumberE164: String!,
    defaultCountryIso: String!
): String!

Format the phone number only if the given number hasn't been formatted.

The number which has only dailable character is treated as not being formatted.

Parameters
phoneNumber String!: the number to be formatted.
phoneNumberE164 String!: the E164 format number whose country code is used if the given phoneNumber doesn't have the country code.
defaultCountryIso String!: the ISO 3166-1 two letters country code whose convention will be used if the phoneNumberE164 is null or invalid, or if phoneNumber contains IDD.
Return
String! the formatted number if the given number has been formatted, otherwise, return the given number.

formatNumberToE164

Added in API level 21
open static fun formatNumberToE164(
    phoneNumber: String!,
    defaultCountryIso: String!
): String!

Formats the specified phoneNumber to the E.164 representation.

Parameters
phoneNumber String!: the phone number to format.
defaultCountryIso String!: the ISO 3166-1 two letters country code in UPPER CASE.
Return
String! the E.164 representation, or null if the given phone number is not valid.

formatNumberToRFC3966

Added in API level 23
open static fun formatNumberToRFC3966(
    phoneNumber: String!,
    defaultCountryIso: String!
): String!

Formats the specified phoneNumber to the RFC3966 representation.

Parameters
phoneNumber String!: the phone number to format.
defaultCountryIso String!: the ISO 3166-1 two letters country code in UPPER CASE.
Return
String! the RFC3966 representation, or null if the given phone number is not valid.

getFormatTypeForLocale

Added in API level 1
Deprecated in API level 21
open static fun getFormatTypeForLocale(locale: Locale!): Int

Deprecated: Use link #formatNumber(String phoneNumber, String defaultCountryIso) instead

Returns the phone number formatting type for the given locale.

Parameters
locale Locale!: The locale of interest, usually Locale#getDefault()
Return
Int The formatting type for the given locale, or FORMAT_UNKNOWN if the formatting rules are not known for the given locale

getNumberFromIntent

Added in API level 1
open static fun getNumberFromIntent(
    intent: Intent!,
    context: Context!
): String!

Extracts the phone number from an Intent.

Parameters
intent Intent!: the intent to get the number of
context Context!: a context to use for database access
Return
String! the phone number that would be called by the intent, or null if the number cannot be found.

getStrippedReversed

Added in API level 1
open static fun getStrippedReversed(phoneNumber: String!): String!

Returns the network portion reversed. This string is intended to go into an index column for a database lookup. Returns null if phoneNumber == null

is12Key

Added in API level 1
static fun is12Key(c: Char): Boolean

True if c is ISO-LATIN characters 0-9, *, #

isDialable

Added in API level 1
static fun isDialable(c: Char): Boolean

True if c is ISO-LATIN characters 0-9, *, # , +, WILD

isEmergencyNumber

Added in API level 1
Deprecated in API level 29
open static fun isEmergencyNumber(number: String!): Boolean

Deprecated: Please use TelephonyManager#isEmergencyNumber(String) instead.

Checks a given number against the list of emergency numbers provided by the RIL and SIM card.

Parameters
number String!: the number to look up.
Return
Boolean true if the number is in the list of emergency numbers listed in the RIL / SIM, otherwise return false.

isGlobalPhoneNumber

Added in API level 1
open static fun isGlobalPhoneNumber(phoneNumber: String!): Boolean

isISODigit

Added in API level 1
open static fun isISODigit(c: Char): Boolean

True if c is ISO-LATIN characters 0-9

isLocalEmergencyNumber

Added in API level 21
Deprecated in API level 29
open static fun isLocalEmergencyNumber(
    context: Context!,
    number: String!
): Boolean

Deprecated: Please use TelephonyManager#isEmergencyNumber(String) instead.

Checks if a given number is an emergency number for the country that the user is in.

Parameters
number String!: the number to look up.
context Context!: the specific context which the number should be checked against
Return
Boolean true if the specified number is an emergency number for the country the user is currently in.

isNonSeparator

Added in API level 1
static fun isNonSeparator(c: Char): Boolean

True if c is ISO-LATIN characters 0-9, *, # , +, WILD, WAIT, PAUSE

isReallyDialable

Added in API level 1
static fun isReallyDialable(c: Char): Boolean

True if c is ISO-LATIN characters 0-9, *, # , + (no WILD)

isStartsPostDial

Added in API level 1
static fun isStartsPostDial(c: Char): Boolean

This any anything to the right of this char is part of the post-dial string (eg this is PAUSE or WAIT)

isVoiceMailNumber

Added in API level 21
open static fun isVoiceMailNumber(number: String!): Boolean

isVoiceMailNumber: checks a given number against the voicemail number provided by the RIL and SIM card. The caller must have the READ_PHONE_STATE credential.

Parameters
number String!: the number to look up.
Return
Boolean true if the number is in the list of voicemail. False otherwise, including if the caller does not have the permission to read the VM number.

isWellFormedSmsAddress

Added in API level 1
open static fun isWellFormedSmsAddress(address: String!): Boolean

Return true iff the network portion of address is, as far as we can tell on the device, suitable for use as an SMS destination address.

isWpsCallNumber

Added in API level 35
open static fun isWpsCallNumber(number: String): Boolean

Check if the number is for Wireless Priority Service call.

Parameters
number String: The phone number used for WPS call. This value cannot be null.
Return
Boolean true if number matches WPS pattern and false otherwise.

networkPortionToCalledPartyBCD

Added in API level 1
open static fun networkPortionToCalledPartyBCD(s: String!): ByteArray!

Note: calls extractNetworkPortion(), so do not use for SIM EF[ADN] style records Returns null if network portion is empty.

networkPortionToCalledPartyBCDWithLength

Added in API level 1
open static fun networkPortionToCalledPartyBCDWithLength(s: String!): ByteArray!

Same as networkPortionToCalledPartyBCD, but includes a one-byte length prefix.

normalizeNumber

Added in API level 21
open static fun normalizeNumber(phoneNumber: String!): String!

Normalize a phone number by removing the characters other than digits. If the given number has keypad letters, the letters will be converted to digits first.

Parameters
phoneNumber String!: the number to be normalized.
Return
String! the normalized number.

numberToCalledPartyBCD

Added in API level 1
Deprecated in API level 28
open static fun numberToCalledPartyBCD(number: String!): ByteArray!

Deprecated: use numberToCalledPartyBCD(java.lang.String,int) instead. Calling this method is equivalent to calling numberToCalledPartyBCD(java.lang.String,int) with BCD_EXTENDED_TYPE_EF_ADN as the extended type.

Convert a dialing number to BCD byte array

Parameters
number String!: dialing number string. If the dialing number starts with '+', set to international TOA
Return
ByteArray! BCD byte array

numberToCalledPartyBCD

Added in API level 28
open static fun numberToCalledPartyBCD(
    number: String!,
    bcdExtType: Int
): ByteArray!

Convert a dialing number to BCD byte array

Parameters
number String!: dialing number string. If the dialing number starts with '+', set to international TOA
bcdExtType Int: used to determine the extended bcd coding Value is android.telephony.PhoneNumberUtils#BCD_EXTENDED_TYPE_EF_ADN, or android.telephony.PhoneNumberUtils#BCD_EXTENDED_TYPE_CALLED_PARTY
Return
ByteArray! BCD byte array

replaceUnicodeDigits

Added in API level 21
open static fun replaceUnicodeDigits(number: String!): String!

Replaces all unicode(e.g. Arabic, Persian) digits with their decimal digit equivalents.

Parameters
number String!: the number to perform the replacement on.
Return
String! the replaced number.

stringFromStringAndTOA

Added in API level 1
open static fun stringFromStringAndTOA(
    s: String!,
    TOA: Int
): String!

Basically: makes sure there's a + in front of a TOA_International number Returns null if s == null

stripSeparators

Added in API level 1
open static fun stripSeparators(phoneNumber: String!): String!

Strips separators from a phone number string.

Parameters
phoneNumber String!: phone number to strip.
Return
String! phone string stripped of separators.

toCallerIDMinMatch

Added in API level 1
open static fun toCallerIDMinMatch(phoneNumber: String!): String!

Returns the rightmost minimum matched characters in the network portion in *reversed* order This can be used to do a database lookup against the column that stores getStrippedReversed() Returns null if phoneNumber == null

toaFromString

Added in API level 1
open static fun toaFromString(s: String!): Int

Returns the TOA for the given dial string Basically, returns TOA_International if there's a + prefix