gpt4 book ai didi

java - 谷歌同意 SDK

转载 作者:搜寻专家 更新时间:2023-10-30 21:22:41 24 4
gpt4 key购买 nike

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

我的代码:

  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 的帮助程序类。要初始化同意信息并在需要时显示同意书,我在主要 Activity 的 onCreate() 方法中有以下代码:

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

同样,当用户点击首选项中的“重置我的隐私同意”时,我运行以下代码:

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

这两次都是指当前正在运行的 Activity 。

辅助类的完整实现:​​

    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);
}
}

关于java - 谷歌同意 SDK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50540976/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com