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

时间:2019-03-31 15:11:04

标签: android admob sharedpreferences

我在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();
            }
        }

0 个答案:

没有答案