2

我的 Android 应用已准备好发布,但它目前在通过同意表单加载个性化广告之前不征求用户同意。它确实有隐私和 cookie 政策。我下载了 Consent SDK 并将其导入到我的应用程序的 Main Activity 中。显示启动活动后,consentform.html 不显示。然后应用程序正常运行测试广告。主要活动代码如下所示:


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.content.Context;

import androidx.appcompat.app.AppCompatActivity;

import com.google.ads.consent.*;
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 com.google.ads.mediation.admob.AdMobAdapter;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;

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


import static com.facebook.FacebookSdk.setAdvertiserIDCollectionEnabled;
import static com.facebook.FacebookSdk.setAutoLogAppEventsEnabled;


public class MainActivity extends AppCompatActivity {


    private AdView mAdView;
    private ConsentForm form;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        displayConsentForm();

        ConsentInformation consentInformation = ConsentInformation.getInstance(this);
        String[] publisherIDs = {"pub-8757875504565304", "173696027825328"};

        consentInformation.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
        // Geography appears as in EEA for test devices.
        ConsentInformation.getInstance(this).
                setDebugGeography(DebugGeography.DEBUG_GEOGRAPHY_EEA);
// Geography appears as not in EEA for debug devices.
        ConsentInformation.getInstance(this).
                setDebugGeography(DebugGeography.DEBUG_GEOGRAPHY_NOT_EEA);

        consentInformation.requestConsentInfoUpdate(publisherIDs, new ConsentInfoUpdateListener() {
            @Override
            public void onConsentInfoUpdated(ConsentStatus consentStatus) {
                // User's consent status successfully updated.
            }

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


        MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {


                mAdView = findViewById(R.id.adviewBanner);
                AdRequest adRequest = new AdRequest.Builder().build();
                mAdView.loadAd(adRequest);
            }
        });

    }

    private void displayConsentForm() {


        URL privacyUrl = null;
        try {

            privacyUrl = new URL("https://www.iubenda.com/privacy-policy/57789951");
        } catch (MalformedURLException e) {
            e.printStackTrace();
            // Handle error.
        }
        // Consent form loaded successfully.
        // Consent form was displayed.
        // Consent form was closed.
        // Consent form error.

        form = new ConsentForm.Builder(MainActivity.this, privacyUrl)
                .withListener(new ConsentFormListener() {
                    @Override
                    public void onConsentFormLoaded() {
                        // Consent form loaded successfully.


                    }

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

                    @Override
                    public void onConsentFormClosed(
                            ConsentStatus consentStatus, Boolean userPrefersAdFree) {
                        // Consent form was closed.
                    }

                    @Override
                    public void onConsentFormError(String errorDescription) {
                        // Consent form error.
                    }
                })
                .withPersonalizedAdsOption()
                .withNonPersonalizedAdsOption()
                .withAdFreeOption()
                .build();

        form.load();
        form.show();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.app_options, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {

        //Handle item selection
        int itemId = item.getItemId();
        if (itemId == R.id.home) {
            return true;
        } else if (itemId == R.id.Paperboard) {
            Intent intentPaperboard = new Intent(this, PaperboardActivity.class);
            startActivity(intentPaperboard);
            return true;
        } else if (itemId == R.id.Cardboard) {
            Intent intentCardboard = new Intent(this, CardboardActivity.class);
            startActivity(intentCardboard);
            return true;
        } else if (itemId == R.id.stencils) {
            Intent stencilIntent = new Intent(this, EmailStencilsExterior.class);
            startActivity(stencilIntent);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}```

I've imported the consent sdk 'com.google.android.ads.consent:consent-library:1.0.8'

I have a mediation group set up with just Facebook Audience Network as the only other ad source. Here is my AndroidManifest.xml file:

```<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:grantUriPermissions="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        >


        <activity
            android:name=".SplashActivity"
            android:theme="@style/SplashTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data android:name="com.facebook.sdk.AutoLogAppEventsEnabled"
            android:value="false"/>

        <meta-data android:name="com.facebook.sdk.AdvertiserIDCollectionEnabled"
            android:value="false"/>

        <!-- Delay app measurement until MobileAds.initialize() is called. -->
        <meta-data
            android:name="com.google.android.gms.ads.DELAY_APP_MEASUREMENT_INIT"
            android:value="true"/>

        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-8757875504565304~1889665505" />

        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/facebook_app_id"/>

        <activity android:name=".MediaPlayerActivity" />
        <activity android:name=".CardboardActivity" />
        <activity android:name=".PaperboardActivity" />
        <activity android:name=".EmailStencilsExterior">
            <intent-filter>
                <action android:name="android.intent.action.SEND_MULTIPLE" />

                <data android:mimeType="application/*" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>```

The logcat shows the following message after running the app:

2021-06-27 14:35:21.509 23977-23977/com.gcps.classicsplitlevel I/ConsentInformation: This request is sent from a test device.

I don't know why the consent form doesn't show after app launch. I hope I've provided enough info to assess the problem. Thanks.

4

1 回答 1

0

像这样在public void onConsentFormLoaded ()下移动form.load()

@Override
public void onConsentFormLoaded() {
   // Consent form loaded successfully.
   form.load();
}
于 2021-12-31T04:46:02.850 回答