Android retrieve user country from google account
Asked Answered
C

1

6

I'm working on app that have content filtering based on user country. As far as I know, it's possible to get country on iOS via App Store. Are there any possibility to do this on Android through Google Play?

List of solutions, that are not acceptable:

  1. Locale: user might live in France, but have English locale, so this will not work.
  2. Telephony manager: what about tablets? Of if user went abroad and changed sim card?
  3. IP or geolocation: user might go abroad, so no filtering there.

My app has in-app-purchases, and I can get currency code in ISO 4217. But this this partial solution, what about Eurozone?

Caenogenesis answered 23/5, 2016 at 9:4 Comment(4)
developers.google.com/maps/documentation/geocoding/…Certainly
@IntelliJAmiya I can't use IP or geolocation. See #3Caenogenesis
If you want to fetch information from the Google account, make a REST call to Google to read their profile. One of the fields in the profile is the location (I forget whether it's city or country). Authenticate via OAuth. This solution is independent of Android — works in an iOS app or a web app, too.Gahnite
I'm looking for the same feature, could you find any solution?Grammalogue
F
-1

You can use a web service which will return the location based on the IP address of the user. Here's an example of one of the web service:

private class AsyncTaskClass extends AsyncTask<Void, JSONObject, JSONObject> {

        @Override
        protected JSONObject doInBackground(Void... params) {

            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url("http://ip-api.com/json")
                    .build();

            try {
                Response response = client.newCall(request).execute();
                return new JSONObject(response.body().string());
            } catch (IOException | JSONException e) {
                e.printStackTrace();
                return null;
            }
        }

        @Override
        protected void onPostExecute(JSONObject data) {
            try {
                Log.i("Country", data.getString("country"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
Flare answered 23/5, 2016 at 9:25 Comment(2)
I can't use this, because what if user went abroad? IP will be differentCaenogenesis
If that's the case, you can ask the user his country upon app installation and store it in SharedPreferences to filter your content.Flare

© 2022 - 2024 — McMap. All rights reserved.