仅首次安装应用程序时如何显示GDPR同意对话框

问题描述 投票:0回答:1

我在Utils类中有两种方法来处理用户是否在EEA国家/地区以及是否显示GDPR同意对话框

第一种方法is_EEA_country将检测该用户是否在EEA国家/地区,如果在该国家,则称为loadGDPRconsent

public final boolean is_EEA_country(Context context) {

        String EEA_COUNRIES[] = {"Austria", "Belgium",
                "Bulgaria", "Croatia", "Republic of Cyprus",
                "Czech Republic", "Denmark", "Estonia", "Finland",
                "France", "Germany", "Greece", "Hungary", "Ireland",
                "Italy", "Latvia", "Lithuania", "Luxembourg", "Malta",
                "Netherlands", "Poland", "Portugal", "Romania", "Slovakia",
                "Slovenia", "Spain", "Sweden", "United Kingdom"};

        GeoIpService ipApiService = ServicesManager.getGeoIpService();
        ipApiService.getGeoIp().enqueue(new Callback<GeoIpResponseModel>() {
            @Override
            public void onResponse(@NonNull Call<GeoIpResponseModel> call,
                                   @NonNull retrofit2.Response<GeoIpResponseModel> response) {
                if (response.isSuccessful()) {

                    String countryName = response.body().getCountryName();

                    if (Arrays.asList(EEA_COUNRIES).contains(countryName)) {
                        Toast.makeText(context, countryName, Toast.LENGTH_LONG).show();
                        isUserApproved = loadGDPRconsent(context);

                    } else {
                        Toast.makeText(context, "NOT GDRP", Toast.LENGTH_LONG).show();
                        if (context instanceof MainActivity) {
                            MainActivity.mainActivityBanner();
                        } else {
                            DetailsActivity.detailsActivityBanner();
                        }
                    }


                } else {

                    Toast.makeText(context, "NOT SUCESS", Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(@NonNull Call<GeoIpResponseModel> call, @NonNull Throwable t) {
                Toast.makeText(context, t.toString(), Toast.LENGTH_SHORT).show();
            }
        });

        return isUserApproved;

    }

[loadGDPRconsent方法

public final boolean loadGDPRconsent(Context context) {

        ConsentInformation consentInformation = ConsentInformation.getInstance(context);
        String[] publisherIds = {"pub-0123456789012345"};
        consentInformation.requestConsentInfoUpdate(publisherIds, new ConsentInfoUpdateListener() {
            @Override
            public void onConsentInfoUpdated(ConsentStatus consentStatus) {
                // User's consent status successfully updated.
                if (consentStatus == ConsentStatus.PERSONALIZED
                        || consentStatus == ConsentStatus.NON_PERSONALIZED) {

                    isUserApproved = true;
                    if (context instanceof MainActivity) {
                        MainActivity.mainActivityBanner();
                    } else {
                        DetailsActivity.detailsActivityBanner();
                    }

                }
            }

            @Override
            public void onFailedToUpdateConsentInfo(String errorDescription) {
                // User's consent status failed to update.
                Log.e(TAG, errorDescription);
            }
        });


        URL privacyUrl = null;
        try {
            privacyUrl = new URL("https://www.your.com/privacyurl");
        } catch (MalformedURLException e) {
            e.printStackTrace();
            // Handle error.
        }

        form = new ConsentForm.Builder(context, privacyUrl)
                .withListener(new ConsentFormListener() {
                    @Override
                    public void onConsentFormLoaded() {
                        // Consent form loaded successfully.
                        form.show();
                        Log.e(TAG, form.isShowing() + " ");
                    }

                    @Override
                    public void onConsentFormOpened() {
                        // Consent form was displayed.
                    }

                    @Override
                    public void onConsentFormClosed(
                            ConsentStatus consentStatus, Boolean userPrefersAdFree) {
                        // Consent form was closed.
                        if (consentStatus == ConsentStatus.PERSONALIZED
                                || consentStatus == ConsentStatus.NON_PERSONALIZED) {

                      isUserApproved = true;

                            if (context instanceof MainActivity) {
                                MainActivity.mainActivityBanner();
                            } else {
                                DetailsActivity.detailsActivityBanner();
                            }
                        }
                    }

                    @Override
                    public void onConsentFormError(String errorDescription) {
                        // Consent form error.
                        Log.e(TAG, errorDescription);
                    }
                })
                .withPersonalizedAdsOption()
                .withNonPersonalizedAdsOption()
                .build();
        form.load();

        return isUserApproved;
    }

现在在主要活动中,我试图在SharedPreferences中保存用户同意,以便在用户已经批准但无法使用的情况下,调用mainActivityBanner方法并直接显示广告

if (sharedPreferences.getBoolean("isUserApproved", false)) {
            mainActivityBanner();
        } else {
            boolean isUserApproved = new Utils().is_EEA_country(this);
            if (isUserApproved) {
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putBoolean("isUserApproved", isUserApproved);
                editor.apply();
            }
        }
android admob sharedpreferences
1个回答
0
投票

同一个问题兄弟。我想知道我们是否可以使用复选框技巧:通过自动选中复选框来保存同意状态,然后在每次启动时都调用che复选框状态以加载同意状态...

© www.soinside.com 2019 - 2024. All rights reserved.