Firebase Auth获取用户国家



我正在使用firebase auth with google登录和facebook in

还有其他方法可以知道用户来自哪个国家吗?

应用程序知道用户来自哪个国家之后,该应用程序将决定将向用户显示哪个服务/未来。

方法1:尝试此代码。该代码将根据连接的国家网络返回国家代码。看这里。没有SIM卡就无法使用。

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

方法2:如果您的设备连接到Internet,则可以使用此链接http://ip-api.com/json。它将根据IP地址自动检测设备返回位置详细信息的IP地址。

方法3:您可以从GPS获得位置。

 LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String provider = locationManager.getBestProvider(criteria,true);
Location curLocation = locationManager.getLastKnownLocation(provider);
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
List<Address> addresses = null;
    addresses = geocoder.getFromLocation((double)curLocal.getLatitudeE6(),(double)curLocal.getLongitudeE6(),1);
String countryCode= addresses.get(0).getCountryCode();

最新更新