Android get Device Country Code - android

I want to get device country code with out using sim card.
e.g If Country : Sri lanka
Country code : LK
my final goal is get calling code (+94 for sri lanka)
i tried with this
TelephonyManager tm = (TelephonyManager)this.getSystemService(getApplicationContext().TELEPHONY_SERVICE);
String countryCodeValue = tm.getNetworkCountryIso();
but this return empty string

It's returning an empty string because the device doesn't have a sim card.
You might be able to use the device's current locale:
Locale current = getResources().getConfiguration().locale;
but that's not necessarily going to be the same as what you'd get by looking at the carrier iso and the user can update that in the settings whenever they want to whatever they want. As pointed out, you can also ask the location manager for the user's coordinates. But remember, users move around and their current location may not always correspond to their carrier's iso.

Here is a complete example. Try to get country code from TelephonyManager (from SIM or CDMA devices), and if not available try to get it from local configuration.
private static String getDeviceCountryCode(Context context) {
String countryCode;
// try to get country code from TelephonyManager service
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if(tm != null) {
// query first getSimCountryIso()
countryCode = tm.getSimCountryIso();
if (countryCode != null && countryCode.length() == 2)
return countryCode.toLowerCase();
if (tm.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA) {
// special case for CDMA Devices
countryCode = getCDMACountryIso();
} else {
// for 3G devices (with SIM) query getNetworkCountryIso()
countryCode = tm.getNetworkCountryIso();
}
if (countryCode != null && countryCode.length() == 2)
return countryCode.toLowerCase();
}
// if network country not available (tablets maybe), get country code from Locale class
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
countryCode = context.getResources().getConfiguration().getLocales().get(0).getCountry();
} else {
countryCode = context.getResources().getConfiguration().locale.getCountry();
}
if (countryCode != null && countryCode.length() == 2)
return countryCode.toLowerCase();
// general fallback to "us"
return "us";
}
#SuppressLint("PrivateApi")
private static String getCDMACountryIso() {
try {
// try to get country code from SystemProperties private class
Class<?> systemProperties = Class.forName("android.os.SystemProperties");
Method get = systemProperties.getMethod("get", String.class);
// get homeOperator that contain MCC + MNC
String homeOperator = ((String) get.invoke(systemProperties,
"ro.cdma.home.operator.numeric"));
// first 3 chars (MCC) from homeOperator represents the country code
int mcc = Integer.parseInt(homeOperator.substring(0, 3));
// mapping just countries that actually use CDMA networks
switch (mcc) {
case 330: return "PR";
case 310: return "US";
case 311: return "US";
case 312: return "US";
case 316: return "US";
case 283: return "AM";
case 460: return "CN";
case 455: return "MO";
case 414: return "MM";
case 619: return "SL";
case 450: return "KR";
case 634: return "SD";
case 434: return "UZ";
case 232: return "AT";
case 204: return "NL";
case 262: return "DE";
case 247: return "LV";
case 255: return "UA";
}
} catch (ClassNotFoundException ignored) {
} catch (NoSuchMethodException ignored) {
} catch (IllegalAccessException ignored) {
} catch (InvocationTargetException ignored) {
} catch (NullPointerException ignored) {
}
return null;
}
Also another idea is to try an API request like in this answer, or to use fine location.
And in order to get country prefix you can use LibPhoneNumber library from Google.
References here and here

You can use this http://maps.googleapis.com/maps/api/geocode/json?latlng=7.0000,81.0000&sensor=false for getting country code ..The short name will give you country code ..
public void getAddress(double lat,double lng) {
Address1 = "";
Address2 = "";
City = "";
State = "";
Country = "";
County = "";
PIN = "";
try {
JSONObject jsonObj = JsonParser.getJSONfromURL("http://maps.googleapis.com/maps/api/geocode/json?latlng=" + lat + ","
+lng + "&sensor=false");
String Status = jsonObj.getString("status");
if (Status.equalsIgnoreCase("OK")) {
JSONArray Results = jsonObj.getJSONArray("results");
JSONObject zero = Results.getJSONObject(0);
JSONArray address_components = zero.getJSONArray("address_components");
for (int i = 0; i < address_components.length(); i++) {
JSONObject zero2 = address_components.getJSONObject(i);
String long_name = zero2.getString("long_name");
String short_name = zero2.getString("short_name");
JSONArray mtypes = zero2.getJSONArray("types");
String Type = mtypes.getString(0);
if (TextUtils.isEmpty(long_name) == false || !long_name.equals(null) || long_name.length() > 0 || long_name != "") {
if (Type.equalsIgnoreCase("street_number")) {
Address1 = long_name + " ";
} else if (Type.equalsIgnoreCase("route")) {
Address1 = Address1 + long_name;
} else if (Type.equalsIgnoreCase("sublocality")) {
Address2 = long_name;
} else if (Type.equalsIgnoreCase("locality")) {
// Address2 = Address2 + long_name + ", ";
City = long_name;
} else if (Type.equalsIgnoreCase("administrative_area_level_2")) {
County = long_name;
} else if (Type.equalsIgnoreCase("administrative_area_level_1")) {
State = long_name;
} else if (Type.equalsIgnoreCase("country")) {
Country = short_name;
} else if (Type.equalsIgnoreCase("postal_code")) {
PIN = long_name;
}
}
// JSONArray mtypes = zero2.getJSONArray("types");
// String Type = mtypes.getString(0);
// Log.e(Type,long_name);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
But you need to get the current lat and long ..
Use this link for getting the current lat and lng
http://gabesechansoftware.com/location-tracking/

In my case telephonyManager.getNetworkCountryIso() is returning empty as i have two sim slots, you can pass slot as parameter if it returns "". Simply change your code to:
String iso=telephonyManager.getNetworkCountryIso();
if(TextUtils.isEmpty(iso))
iso=telephonyManager.getNetworkCountryIso(1);
if(TextUtils.isEmpty(iso))
iso=telephonyManager.getNetworkCountryIso(2);
// this code will give preference to Sim Slot 1

get iso country code => US
lookup this table => 1
https://countrycode.org/

Related

Android GeoCoder location search produce wrong location

I am trying to add twitter style add location to feed.
When I try to search cities for instance words starts with "new york" results comes with many different results with new york as image
My geocode is as below
public static List<String> getAddressesFromLocation(Context context, double lat, double lon) {
Locale current = context.getResources().getConfiguration().locale;
Geocoder coder = new Geocoder(context, current);
List<Address> addresses;
int maxResult = 5;
List<String> addressList = new ArrayList<String>();
try {
// May throw an IOException
addresses = coder.getFromLocation(lat, lon, maxResult);
if (addresses == null) {
return null;
}
for (int j = 0; j < addresses.size(); j++) {
Address returnedAddress = addresses.get(j);
String city = (returnedAddress.getLocality() == null || returnedAddress.getLocality().isEmpty()) ? "" : (returnedAddress.getLocality() + ", ");
String state = (returnedAddress.getAdminArea() == null || returnedAddress.getAdminArea().isEmpty()) ? "" : (returnedAddress.getAdminArea() + ", ");
String country = (returnedAddress.getCountryName() == null || returnedAddress.getCountryName().isEmpty()) ? "" : returnedAddress.getCountryName();
if((city == null || city.isEmpty()) && (state == null || state.isEmpty())){
continue;
}
if(city.equals(state)){
state = "";
}
addressList.add(city + state + country);
}
} catch (IOException ex) {
ex.printStackTrace();
}
return addressList;
}
Everything works fine but with wrong results. What is the problem?

addresses from Geocoder size is always zero

i have the problem as this question and i also did the same hardcoding for the lat and lon.
i get the exception to confirm the size as
Exception for the locationjava.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
When I try the same code on the Redmi a4, it works fine and give me the size as 1, and also the geocoding works fine, returning me all the addresses. The same code when I run on Lenovo it gives the exception. I checked the lat and lon and they have values which when i trace is pointing to my location. So what should I do to make the app run on both of the phones.
Lenovo phone running Android 4.4.4
Redmi running 6.0.0
if(lat != null && lon !=null) {
try{
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(dlat, dlon, 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
System.out.println("this is the size of address size"+addresses.size());
String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
String city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
String country = addresses.get(0).getCountryName();
String postalCode = addresses.get(0).getPostalCode();
String knownName = addresses.get(0).getFeatureName();
complete_address=address + "_" + city + "_" + address + "_" + postalCode + "_" + knownName + "_" + country;
txtOutputLat.setText(complete_address);
txtOutputLon.setText("Map Address");
}
catch(Exception e)
{
System.out.println("Exception for the location"+e.toString());
}
}
Create this class and copy the code this works perfectly for me also this works in background and stops the main thread from ANR dialog
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.os.AsyncTask;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
public class GetLocationAsync extends AsyncTask<String, Void, String> {
private String fulladdress = "";
private String smallAddress = "";
private String city = "";
private String state = "";
private String country = "";
private String zipcode = "";
private String placeName = "";
private AsyncResponse asyncResponseDelegate = null;
private double x, y;
private Context context;
public interface AsyncResponse {
void onProcessFinished(String fulladdress, String smallAddress, String state, String city, String country, String zipCode, String placeName);
}
public GetLocationAsync(double latitude, double longitude, final Context context, AsyncResponse asyncResponse) {
x = latitude;
y = longitude;
this.context = context;
this.asyncResponseDelegate = asyncResponse;
}
#Override
protected void onPreExecute() {
}
#Override
protected String doInBackground(String... params) {
Geocoder geocoder = new Geocoder(context, Locale.ENGLISH);
try {
List<Address> addressList = geocoder.getFromLocation(x, y, 1);
StringBuilder sb = new StringBuilder();
if (addressList != null && addressList.size() > 0) {
Address address = addressList.get(0);
fulladdress = "";
smallAddress = "";
placeName = "";
if (sb.append(address.getAddressLine(0)) != null) {
smallAddress = address.getAddressLine(0);
fulladdress = smallAddress;
}
if (address.getLocality() != null) {
city = address.getLocality();
if (!fulladdress.contains(city))
fulladdress = fulladdress + " " + city;
}
if (address.getFeatureName() != null) {
placeName = address.getFeatureName();
}
if (address.getAdminArea() != null) {
state = address.getAdminArea();
if (!fulladdress.contains(state))
fulladdress = fulladdress + " " + state;
}
if (address.getPostalCode() != null) {
zipcode = address.getPostalCode();
if (!fulladdress.contains(zipcode))
fulladdress = fulladdress + " " + zipcode;
}
if (address.getCountryName() != null) {
country = address.getCountryName();
if (!fulladdress.contains(country))
fulladdress = fulladdress + " " + country;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(String result) {
try {
if (fulladdress != null && !fulladdress.isEmpty()) {
asyncResponseDelegate.onProcessFinished(fulladdress, smallAddress, state, city, country, zipcode, placeName);
} else {
asyncResponseDelegate.onProcessFinished("", "", "", "", "", "", "");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
to call this class
GetLocationAsync locationAsync = new GetLocationAsync(latitude,
longitude, getActivity(), new GetLocationAsync.AsyncResponse() {
#Override
public void onProcessFinished(String fulladdress, String smallAddress, String state, String city, String country, String zipCode, String placeName) {
etAddress.setText(fulladdress);
}
});
locationAsync.execute();
What happens is when you try to get the postal code,city,country etc etc only from the address is that you might not recieve it for the very first time since its trying to get all zipcode, country, state and city from a single provider, which may not be available at times. Instead if you loop through the providers you may find it in one of them. Try this hope it helps!
List<Address> addresses = null;
try {
addresses =geocoder.getFromLocation(latitude,longitude, 5);
if (addresses.size() > 0) {
zipCode = addresses.get(0).getPostalCode();
city = addresses.get(0).getLocality();
country = addresses.get(0).getCountryName();
state = addresses.get(0).getAdminArea();
address = addresses.get(0).getAddressLine(0);
knownName = addresses.get(0).getFeatureName();
//if 1st provider does not have data, loop through other providers to find it.
int count = 0;
while ( count < addresses.size()) {
zipCode = addresses.get(count).getPostalCode();
city= addresses.get(count).getLocality();
country = addresses.get(count).getCountryName();
state = addresses.get(count).getAdminArea();
address = addresses.get(count).getAddressLine(0);
knownName = addresses.get(count).getFeatureName();
count++;
}
}
} catch (IOException e) {
e.printStackTrace();
}
Check for internet connection before calling geocoder.
if(isConnectingToInternet(getContext()){
//write your code of calling geocoder
}
public boolean isConnectingToInternet(Context context) {
try {
if (context != null) {
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
} catch (Exception e) {
e.printStacktrace
}
return false;
}

How to extract location attributes using Geocoder?

When I try to get default locale it returns "zz_ZZ":
countryCode: "ZZ"
languageCode: "zz"
Code is:
private String getAddressFromLocation(Location location) {
Geocoder geoCoder = new Geocoder(this.getApplicationContext(), Locale.getDefault());
List<Address> matches = null;
try {
matches = geoCoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
Address bestMatch = (matches.isEmpty() ? null : matches.get(0) );
if (bestMatch != null){
String fullAddress = bestMatch.getAddressLine(0);
for (int i = 1; i <= bestMatch.getMaxAddressLineIndex(); i++){
fullAddress += ", " + bestMatch.getAddressLine(i);
}
return fullAddress;
}else{
return Constants.EMPTY_STRING;
}
} catch (IOException e) {
ACRA.getErrorReporter().handleSilentException(new MyGeoLocationAddressException(TAG + " ERROR CREATING ADDRESS - " + e.getMessage()));
return Constants.EMPTY_STRING;
}
}
Do I need to setDefaultLocale? What does ZZ_zz mean?
Second problem is:
If I use the "ZZ_zz" default locale, I don't get any address but if I use:
Locale.setDefault(Locale.ENGLISH);
I get the exact address.
ZZ_zz is the identifier for the Accented English locale and is mostly used for finding missing translations in your app.
I don't think there's a best practice on how to handle accented english, but treating it as english would probably be the best option.
You must handle locations in this way, check the scenario when no address is returned
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(
location.getLatitude(),
location.getLongitude(),
// In this sample, get just a single address.
1);
} catch (IOException ioException) {
// Catch network or other I/O problems.
errorMessage = getString(R.string.service_not_available);
Log.e(TAG, errorMessage, ioException);
} catch (IllegalArgumentException illegalArgumentException) {
// Catch invalid latitude or longitude values.
errorMessage = getString(R.string.invalid_lat_long_used);
Log.e(TAG, errorMessage + ". " +
"Latitude = " + location.getLatitude() +
", Longitude = " +
location.getLongitude(), illegalArgumentException);
}
// Handle case where no address was found.
if (addresses == null || addresses.size() == 0) {
if (errorMessage.isEmpty()) {
errorMessage = getString(R.string.no_address_found);
Log.e(TAG, errorMessage);
}
deliverResultToReceiver(Constants.FAILURE_RESULT, errorMessage);
} else {
Address address = addresses.get(0);
ArrayList<String> addressFragments = new ArrayList<String>();
// Fetch the address lines using getAddressLine,
// join them, and send them to the thread.
for(int i = 0; i < address.getMaxAddressLineIndex(); i++) {
addressFragments.add(address.getAddressLine(i));
}
Log.i(TAG, getString(R.string.address_found));
deliverResultToReceiver(Constants.SUCCESS_RESULT,
TextUtils.join(System.getProperty("line.separator"),
addressFragments));
}
}

Extracting the City and State from Geocoder in Android

I'm attempting to extract the City and State only from the Geocoder in Android. I've gotten pretty far to where it extracts the city fine, but the state I'm having issues with its printing the zip along side it.
I'm looking for "City, State" whereas the final output now is "City, State Zip"
Any ideas?
Geocoder geoCoder = new Geocoder(getActivity(), Locale.getDefault());
List<Address> addresses;
try {
addresses = geoCoder.getFromLocation(mLatitude, mLongitude, 10);
int i=1;
for(Address addObj:addresses)
{
// Looping once
if(i==1)
{
String add_line1_extract;
add_line1_extract=addObj.getAddressLine(1);
String string = add_line1_extract;
String[] parts = string.split(",");
//Setting city
mCity = parts[0];
//setting state
mState = parts[1];
// Final Output
String cityAndState = mCity + ", " + mState;
i++;
}
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
You could get the state name by calling the getAdminArea() method from Address class. However, it might not work for all the countries. Also, you could call the getLocality() to retrieve the city name.
So for your code, you can do the following:
String cityName = addObject.getLocality();
String stateName = addObject.getAdminArea();
String cityAndState = cityName + ", " + stateName;
If you want the abbreviation of a state name, you can do further split in your String[] parts. You can do the following:
String[] moreSplit = parts[1].split(" ");
String mStateAbbr = moreSplit[0];
String mCityAndStateAbbr = mCity + mStateAbbr;

Geocoding and reverse geocoding [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am trying a simple app where i need to display a map.And when i click on any location it should return the address of that location.Can u please guide me as how to proceed with this.
I Found Google API is very handy for reverse Geo coding. For this I've created this class. You can directly use this class in your app :
public class getReverseGeoCoding
{
private String Address1 = "", Address2 = "", City = "", State = "", Country = "", County = "", PIN = "";
public void getAddress()
{
Address1 = ""; Address2 = ""; City = ""; State = ""; Country = ""; County = ""; PIN = "";
try {
JSONObject jsonObj = parser_Json.getJSONfromURL("http://maps.googleapis.com/maps/api/geocode/json?latlng="+ Global.curLatitude + "," + Global.curLongitude +"&sensor=true");
String Status = jsonObj.getString("status");
if(Status.equalsIgnoreCase("OK"))
{
JSONArray Results = jsonObj.getJSONArray("results");
JSONObject zero = Results.getJSONObject(0);
JSONArray address_components = zero.getJSONArray("address_components");
for(int i = 0; i<address_components.length(); i++)
{
JSONObject zero2 = address_components.getJSONObject(i);
String long_name = zero2.getString("long_name");
JSONArray mtypes = zero2.getJSONArray("types");
String Type = mtypes.getString(0);
if(TextUtils.isEmpty(long_name) == false || !long_name.equals(null) || long_name.length() > 0 || long_name != "" )
{
if(Type.equalsIgnoreCase("street_number"))
{
Address1 = long_name + " ";
}
else if(Type.equalsIgnoreCase("route"))
{
Address1 = Address1 + long_name;
}
else if(Type.equalsIgnoreCase("sublocality"))
{
Address2 = long_name;
}
else if(Type.equalsIgnoreCase("locality"))
{
// Address2 = Address2 + long_name + ", ";
City = long_name;
}
else if(Type.equalsIgnoreCase("administrative_area_level_2"))
{
County = long_name;
}
else if(Type.equalsIgnoreCase("administrative_area_level_1"))
{
State = long_name;
}
else if(Type.equalsIgnoreCase("country"))
{
Country = long_name;
}
else if(Type.equalsIgnoreCase("postal_code"))
{
PIN = long_name;
}
}
// JSONArray mtypes = zero2.getJSONArray("types");
// String Type = mtypes.getString(0);
// Log.e(Type,long_name);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public String getAddress1()
{
return Address1;
}
public String getAddress2()
{
return Address2;
}
public String getCity()
{
return City;
}
public String getState()
{
return State;
}
public String getCountry()
{
return Country;
}
public String getCounty()
{
return County;
}
public String getPIN()
{
return PIN;
}
Do you know how to display a map in your android app? If not you can take a look at this nice tutorial.
Then displaying address can be done like this:
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
for getting the coordinate of a click, check this thread:
Getting coordinates when clicking anywhere on a MapView
and you can turn a coordinate into an adress with this:
Address a = ((Address)geocoder.getFromLocation(latiAsDouble, longiAsDouble, 1).get(0));
String Position = a.getAddressLine(1) + " - " + a.getAddressLine(0);
this does a http call to a google server, so you might want to put this into some kind of thread.

Categories

Resources