如何获得用户的国家/地区位置?

Raj*_*h K 14 android location privacy geolocation android-gps

由于GDPR,我需要检查用户位置是否用户来自欧盟.直到现在我找到了这些解决方案 -

1)使用电话的语言配置获取国家(如果用户使用英语美国版本可能会误导,即使他可能来自其他国家/地区).

String locale = context.getResources().getConfiguration().locale.getCountry();
Run Code Online (Sandbox Code Playgroud)

2)使用GPS获取位置(需要位置Corse权限,我不想添加,特别是因为Android 6.0+运行时权限).

    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);
    }
}
Run Code Online (Sandbox Code Playgroud)

3)使用Sim/WIFI获取位置(不能在没有SIM/WI-FI的桌子上使用,因为我的应用程序可以在没有互联网连接/ Wi-Fi的设备上使用).

TelephonyManager tm = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
String countryCodeValue = tm.getNetworkCountryIso();
Run Code Online (Sandbox Code Playgroud)

我的问题是,如果用户来自欧盟地区,是否有可能找到用户的位置.即使国家能够工作,我也不需要确切的位置.

请注意,这个问题不是任何其他问题的重复,因为我的案例没有被任何其他问题解决.

编辑:任何其他可能还需要位置权限的方法(非Corse位置权限也会有所帮助)

任何指针都会非常有用.

Ber*_*ero 8

您可以通过请求以下网址http://adservice.google.com/getconfig/pubvendors来检查用户是否在欧盟境内.它给你这样的答案:

{ "is_request_in_eea_or_unknown":真正}

在某个地方,我已经读到这是Android同意SDK的工作方式.