Search code examples
androidtelephony

Android making phones calls from inside & outside country


It is simple, I want to make a feature when clicked it calls a number.However, if you are inside Oman I want to call a number and if you are outside i want to call a different number.

I tried this, and would like to know if its correct way to do it...or i have to rely on something else

PackageManager pm = getPackageManager();
                boolean hasTelephony = pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
                if(hasTelephony)
                {
                    TelephonyManager tManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
                    if(tManager != null)
                    {
                        String locale = tManager.getSimCountryIso();
                        if(locale != null && locale.toLowerCase().equals("om"))
                        {
                            i.setData(Uri.parse("tel:800"));
                        }
                        else
                            i.setData(Uri.parse("tel:+9687"));
                    }
                    else
                        i.setData(Uri.parse("tel:+9687"));
                }
                else
                    i.setData(Uri.parse("tel:+9687"));

Solution

  • You're on the right path, but you probably want to call:

    tManager.getNetworkCountryIso()
    

    That way you get the country of the mobile network your phone is connected to, instead of the country where your SIM is from (which is always the same).

    On a side note: you don't need to check whether the device has the telephony feature - if it doesn't then tManager will be null anyway.