gpt4 book ai didi

android - 如何在不使用 GPS 的情况下获取我的 Android 设备国家代码?

转载 作者:IT王子 更新时间:2023-10-28 23:49:17 27 4
gpt4 key购买 nike

Android 手机实际上确实很清楚它在哪里 - 但有没有办法通过国家代码或国家名称之类的东西来检索国家?

不需要知道确切的 GPS 位置 - 国家代码或名称就足够了,我正在使用这个代码:

 String locale = context.getResources().getConfiguration().locale.getCountry(Locale.getDefault());
System.out.println("country = "+locale);

它给了我代码“美国”,但我的设备保存在印度。有没有办法在不使用 GPS 或网络提供商的情况下找到设备当前的国家/地区代码?

因为我使用的是平板电脑。

最佳答案

您可以简单地使用此代码,

TelephonyManager tm = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
String countryCodeValue = tm.getNetworkCountryIso();

如果您当前连接的网络在美国,这将返回“US”。即使没有 SIM 卡也可以使用。

关于android - 如何在不使用 GPS 的情况下获取我的 Android 设备国家代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11293642/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com