Checking if call being made is international

2019-01-29 13:43发布

问题:

I'd like to check if a call being made is international or not. I have the SIM number which I obtain by using TelephonyManager.getLine1Number() and also the Country code ISO of the SIM Card which I obtain using TelephonyManager.getSimCountryIso().

Is there anyway I can find the country code of the number to which the call is being made?

回答1:

I don't think telephonyManager.getSimCountryIso() would help you determine 'to which country the call is being made' as it will return your country's ISO.

Moreover, length of ISD codes vary across countries. For some countries it is 1, for some it's 2, for some it's 3 and for others it's 4. So you will need to extract/make 4 different keys of these lengths from the outgoing number as I have shown below:

Say the out going number is +91-XXX-XXX-XXXX. then you'll create 4 keys as:

  1. 9 (1 digit key)
  2. 91 (2 digit key)
  3. 91X (3 digit key)
  4. 91XX (4 digit key)

Now check if any of these 4 keys is present in this list: ISO List.

[EDIT: Alternative Solution]

Again, if you only need to determine if the call being made is international or not then you can simply check for below condition:

if(outgoing-number `startswith` "00" || outgoing-number does not `startswith` your "country's-ISD-code") {
    //it's an international call;
} else {
    //it's a domestic call;
}