谷歌同意 SDK

Google consent SDK(谷歌同意 SDK)

本文介绍了谷歌同意 SDK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 Google ConsentSDK 在 Android 应用同意表单中显示.当我调用 form.show() 时,我收到此错误:同意书错误同意书尚未准备好显示."谁能帮帮我?

I try to use Google ConsentSDK to show in Android app consent form. When I call form.show() I get this error: "Consent form error Consent form is not ready to be displayed." Who can help me?

我的代码:

  ConsentForm form = new ConsentForm.Builder(context, privacyUrl)
            .withListener(new ConsentFormListener() {
                @Override
                public void onConsentFormLoaded() {
                    // Consent form loaded successfully.
                    Log.d("SplashScreen", "Consent form Loaded ");
                }

                @Override
                public void onConsentFormOpened() {
                    // Consent form was displayed.
                    Log.d("SplashScreen", "Consent form opened ");
                }

                @Override
                public void onConsentFormClosed(
                        ConsentStatus consentStatus, Boolean userPrefersAdFree) {
                    // Consent form was closed.
                    Log.d("SplashScreen", "Consent form Closed ");
                }

                @Override
                public void onConsentFormError(String errorDescription) {
                    // Consent form error.
                    Log.d("SplashScreen", "Consent form error " + errorDescription);
                }
            })
            .withPersonalizedAdsOption()
            .withNonPersonalizedAdsOption()
            .build();
    form.load();
    form.show();

推荐答案

这是我在应用中使用的 Google Consent SDK 的帮助程序类.为了初始化同意信息并在需要时显示同意书,我在主要活动的 onCreate() 方法中有以下代码:

Here is my helper class for the Google Consent SDK that I use in my app. To initialise the consent information and display the Consent Form if needed, I have following code in onCreate() method of my main activity:

GdprHelper gdprHelper = new GdprHelper(this);
gdprHelper.initialise();

同样,当用户点击偏好设置中的重置我的隐私许可"时,我会运行以下代码:

Similarly, I run following code when user clicks on "Reset my privacy consent" in preferences:

GdprHelper gdprHelper = new GdprHelper(this);
gdprHelper.resetConsent();

这两次都是引用当前正在运行的活动.

where both times, this is referencing current running activity.

帮助类的完整实现:​​

Full implementation of the helper class:

    package com.example.app;

    import android.content.Context;
    import android.content.Intent;
    import android.net.Uri;
    import android.widget.Toast;

    import com.google.ads.consent.ConsentForm;
    import com.google.ads.consent.ConsentFormListener;
    import com.google.ads.consent.ConsentInfoUpdateListener;
    import com.google.ads.consent.ConsentInformation;
    import com.google.ads.consent.ConsentStatus;

    import java.net.MalformedURLException;
    import java.net.URL;

    public class GdprHelper {

        private static final String PUBLISHER_ID = "YOUR-PUBLISHER-ID";
        private static final String PRIVACY_URL = "YOUR-PRIVACY-URL";
        private static final String MARKET_URL_PAID_VERSION = "market://details?id=com.example.app.pro";

        private final Context context;

        private ConsentForm consentForm;

        public GdprHelper(Context context) {
            this.context = context;
        }

        // Initialises the consent information and displays consent form if needed
        public void initialise() {
            ConsentInformation consentInformation = ConsentInformation.getInstance(context);
            consentInformation.requestConsentInfoUpdate(new String[]{PUBLISHER_ID}, new ConsentInfoUpdateListener() {
                @Override
                public void onConsentInfoUpdated(ConsentStatus consentStatus) {
                    // User's consent status successfully updated.
                    if (consentStatus == ConsentStatus.UNKNOWN) {
                        displayConsentForm();
                    }
                }

                @Override
                public void onFailedToUpdateConsentInfo(String errorDescription) {
                    // Consent form error. Would be nice to have proper error logging. Happens also when user has no internet connection
                    if (BuildConfig.BUILD_TYPE.equals("debug")) {
                        Toast.makeText(context, errorDescription, Toast.LENGTH_LONG).show();
                    }
                }
            });
        }

        // Resets the consent. User will be again displayed the consent form on next call of initialise method
        public void resetConsent() {
            ConsentInformation consentInformation = ConsentInformation.getInstance(context);
            consentInformation.reset();
        }

        private void displayConsentForm() {

            consentForm = new ConsentForm.Builder(context, getPrivacyUrl())
                    .withListener(new ConsentFormListener() {
                        @Override
                        public void onConsentFormLoaded() {
                            // Consent form has loaded successfully, now show it
                            consentForm.show();
                        }

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

                        @Override
                        public void onConsentFormClosed(
                                ConsentStatus consentStatus, Boolean userPrefersAdFree) {
                            // Consent form was closed. This callback method contains all the data about user's selection, that you can use.
                            if (userPrefersAdFree) {
                                redirectToPaidVersion();
                            }
                        }

                        @Override
                        public void onConsentFormError(String errorDescription) {
                            // Consent form error. Would be nice to have some proper logging
                            if (BuildConfig.BUILD_TYPE.equals("debug")) {
                                Toast.makeText(context, errorDescription, Toast.LENGTH_LONG).show();
                            }
                        }
                    })
                    .withPersonalizedAdsOption()
                    .withNonPersonalizedAdsOption()
                    .withAdFreeOption()
                    .build();
            consentForm.load();
        }

        private URL getPrivacyUrl() {
            URL privacyUrl = null;
            try {
                privacyUrl = new URL(PRIVACY_URL);
            } catch (MalformedURLException e) {
                // Since this is a constant URL, the exception should never(or always) occur
                e.printStackTrace();
            }
            return privacyUrl;
        }

        private void redirectToPaidVersion() {
            Intent i = new Intent(
                    Intent.ACTION_VIEW,
                    Uri.parse(MARKET_URL_PAID_VERSION));
            context.startActivity(i);
        }
    }

这篇关于谷歌同意 SDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:谷歌同意 SDK

基础教程推荐