我正在寻找一种通过Wi-Fi获取用户所在国家/地区的方法。
到目前为止,我已经设法使用TelephonyManager
和SIM卡,就像这样
TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
String s = telephonyManager.getSimCountryIso();
这样可行,但问题是,用户可能没有SIM卡(平板电脑),所以我还需要能够通过使用他们连接的Wi-Fi来确定国家/地区。
我尝试了这段代码,但它不能按照我的意愿工作,我只想要一个国家代码,比如US,UK,DE,而不是这种方法返回GPS坐标...
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
Log.v("LOCATION", location.getProvider());
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
};
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
如何使用Wi-Fi获取国家/地区?
答案 0 :(得分:4)
您需要执行反向地理编码,即转换您要到达的地址。为此,您可以使用Geocoder
Geocoder gcd = new Geocoder(this, Locale.getDefault());
List<Address> addresses;
try {
addresses = gcd.getFromLocation(location.getLatitude(), location.getLongitude(), 1); //1 - is number of result you want you write it any integer value. But as you require country name 1 will suffice.
if (addresses.size() > 0)
System.out.println(addresses.get(0).getCountryName());
} catch (IOException e1) {
e1.printStackTrace();
} catch (Exception e1) {
e1.printStackTrace();
}
答案 1 :(得分:3)
这可以通过GeoCoder完成,这是一段用于检查WiFi和获取位置信息的代码:
public Location getLocation() {
try {
// any location
// Getting network status
Log.e("GPS Service", "Get Location Called");
isNetworkEnabled = mLocationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Log.e("GPS Service", String.valueOf(isNetworkEnabled));
if (isNetworkEnabled) {
Log.e("GPS Service", "Yay Wifi Enabled");
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, this);
if (mLocationManager != null) {
mLocation = mLocationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (mLocation != null) {
Log.e("GPS Service", "Yay Location");
mLatitude = mLocation.getLatitude();
mLongitude = mLocation.getLongitude();
isLocationAvailable = true; // setting a flag that
// location is available
return mLocation;
}
}
}
// If reaching here means, we were not able to get location neither
// from GPS not Network,
if (!isGPSEnabled) {
// so asking user to open GPS
//askUserToOpenGPS();
}
} catch (Exception e) {
e.printStackTrace();
}
// if reaching here means, location was not available, so setting the
// flag as false
isLocationAvailable = false;
return null;
}
public String getCountryCode() {
getLocation();
if (isLocationAvailable) {
Geocoder geocoder = new Geocoder(mContext, Locale.getDefault());
// Get the current location from the input parameter list
// Create a list to contain the result address
List<Address> addresses = null;
try {
/*
* Return 1 address.
*/
mLatitude = getLatitude();
mLongitude = getLongitude();
addresses = geocoder.getFromLocation(mLatitude, mLongitude, 1);
} catch (IOException e1) {
e1.printStackTrace();
Log.e("returning", "tm");
//TelephonyManager tm = (TelephonyManager)mContext.getSystemService(mContext.TELEPHONY_SERVICE);
//return tm.getNetworkCountryIso();
} catch (IllegalArgumentException e2) {
// Error message to post in the log
String errorString = "Illegal arguments "
+ Double.toString(mLatitude) + " , "
+ Double.toString(mLongitude)
+ " passed to address service";
e2.printStackTrace();
return errorString;
}
// If the reverse geocode returned an address
if (addresses != null && addresses.size() > 0) {
// Get the first address
Address address = addresses.get(0);
/*
* Format the first line of address (if available), city, and
* country name.
*/
// Return the text
Log.e("returning", address.getCountryCode());
String countrycode;
if (address.getCountryCode() == null) {
countrycode = "null";
} else {
countrycode = address.getCountryCode();
}
return countrycode;
} else {
return "null";
}
} else {
//String locale = getResources().getConfiguration().locale.getCountry();
Log.e("returning", "wifi");
getJSON();
Log.e("ELSE", wifiCode);
return wifiCode;
}
}