我怎样才能得到一个国家的全名从code? [英] How can I get the fullname of a country from its code?

查看:148
本文介绍了我怎样才能得到一个国家的全名从code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在就可以拿到国内code,但我需要国家的全名。

我该怎么办呢?

 公共静态字符串getCountry(上下文C){
        TelephonyManager经理=(TelephonyManager)C
                .getSystemService(Context.TELEPHONY_SERVICE);        返回manager.getNetworkCountryIso()与toUpperCase()。
    }


解决方案
而不是有国家的一个巨大的HashMap和其codeS(这也将工作),你可以尝试以下之一

区域设置方法

  string地区= context.getResources()getConfiguration()locale.getDisplayCountry()。

然而,这个人将不会在为哪些Android不具有区域设置的国家工作。例如,在瑞士,语言可能被设置为德语或法语。这种方法会给你德国或法国,瑞士没有。最好使用的LocationManager或TelephonyManager方法。

电话经理法

  TelephonyManager TM =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
字符串国code = tm.getSimCountryIso();

这将由从SIM卡获取国家工作。这将需要在 READ_PHONE_STATE 许可。在没有SIM卡将无法正常工作,或者当手机漫游国际是不准确的。

位置管理方法(最可靠)

首先,获得的LocationManager 。然后,调用 LocationManager.getLastKnownPosition()。然后,创建一个地理codeR 和呼叫地理coder.getFromLocation()。做到这一点是一个单独的线程!这会给你地址对象的列表。呼叫 Address.getCountryName(),你得到了它。

记住,最后已知的位置可以是一个有点陈旧,因此如果用户刚刚越过边界,你可能不知道它一段时间。然而,它比任何其他的更可靠的

I can get the country code now but I need the full name of the country.

How can I do it?

public static String getCountry(Context c) {
        TelephonyManager manager = (TelephonyManager) c
                .getSystemService(Context.TELEPHONY_SERVICE);

        return manager.getNetworkCountryIso().toUpperCase();
    }

解决方案

Instead of having a huge hashmap of countries and their codes (which would also work), you could try one of the following:

Locale Approach

 String locale = context.getResources().getConfiguration().locale.getDisplayCountry();

However, this one won't work in countries for which Android does not have a Locale. For example, in Switzerland, the language is likely to be set to German or French. This method will give you Germany or France, not Switzerland. Better to use the LocationManager or TelephonyManager approach.

Telephone Manager Approach

TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String countryCode = tm.getSimCountryIso();

This will work by getting the country from the SIM card. It will require the READ_PHONE_STATE permission. It will not work when there is no SIM card, or be inaccurate when the phone is roaming internationally.

Location Manager Approach (most reliable)

First, get the LocationManager. Then, call LocationManager.getLastKnownPosition(). Then, create a GeoCoder and call GeoCoder.getFromLocation(). Do this is a separate thread!! This will give you a list of Address objects. Call Address.getCountryName() and you got it.

Keep in mind that the last known position can be a bit stale, so if the user just crossed the border, you may not know about it for a while. However, it is more reliable than any of the others.

这篇关于我怎样才能得到一个国家的全名从code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆