How can I get a user's country location?

Rajesh K picture Rajesh K · May 9, 2018 · Viewed 8.4k times · Source

Due to GDPR, I am requiring to check the user's location - whether the user is from the European Union. Till now I have found these solutions -

  1. Get Country using the Phone's language configuration (can misguide if the user uses English US version even though he might be from some other country).

     String locale = context.getResources().getConfiguration().locale.getCountry();
    
  2. Get Location using the GPS (requires the location coarse permission which I do not want to add especially because of Android 6.0+ runtime permissions).

     private void getCountryCode(final Location location) {
         Helper.log(TAG, "getCountryCode");
    
         AsyncTask<Void, Void, String> countryCodeTask = new AsyncTask<Void, Void, String>() {
    
             final float latitude = (float) location.getLatitude();
             final float longitude = (float) location.getLongitude();
             // Helper.log(TAG, "latitude: " + latitude);
             List<Address> addresses = null;
             Geocoder gcd = new Geocoder(mContext);
             String code = null;
    
             @Override
             protected String doInBackground(Void... params) {
                 try {
                     addresses = gcd.getFromLocation(latitude, longitude, 1);
                     code = addresses.get(0).getCountryCode();
                 } 
                 catch (IOException e) {
                     e.printStackTrace();
                 }
                 return code;
             }
    
             @Override
             protected void onPostExecute(String code) {
                 Helper.log(TAG, "onPostExecute");
                 mCountryCodeListener.returnCountryCode(code);
             }
         };
    
         if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
             countryCodeTask.execute();
         }
         else {
             countryCodeTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
         }
     }
    
  3. Get Location using the SIM card or Wi-Fi (can not be used on tables without SIM card or Wi-Fi, whereas my app can be used on the device without an Internet connection or Wi-Fi).

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

Is it possible to find out the location of the user if the user is from European Union region or not?

I do not need the exact location and even the country will work.

Is there another method which might also require location permission (not the coarse location permission will also be helpful)?

Please note that this question is not a duplicate of any other question as my case is not being solved by any other question.

Answer

Bero picture Bero · May 30, 2018

You can check if user is in the EU by requesting the URL http://adservice.google.com/getconfig/pubvendors. It gives you an answer like this:

{"is_request_in_eea_or_unknown":true}

Somewhere I've read that it is the way the Android consent SDK works.