【问题标题】:AdMob ad doesn't show until I reopen the app在我重新打开应用之前,AdMob 广告不会展示
【发布时间】:2021-10-10 10:46:58
【问题描述】:

我正在尝试将 AdMob 横幅添加到我的应用中,但有一半时间它没有显示任何内容。 AdListener 甚至接到了对 onAdLoaded 的电话,我从来没有收到任何错误。
我注意到,如果广告没有显示,并且我将应用程序置于后台,广告就会开始显示。例如,如果我:

  1. 关闭并重新打开应用程序
  2. 开始全屏插页式广告
  3. 启动应用内购买覆盖

所有这些操作都会触发以某种方式重绘/重新测量视图的更新。
我在onAdLoaded 中尝试了许多不同的方法来模拟这一点。

即使我没有收到任何错误,AdMob 是否也可能找不到匹配的广告?根据网站,匹配率为 98%。展示次数约为请求的三分之一。

我发现了更多具有类似问题的帖子(全部来自大约 7 年前),但他们的答案都不适用于我的情况:
Admob not showing up until phone is locked
AdMob won't show the banner until refresh or sign in to google plus
Android AdMob: addView doesn't show ad until return to activity
@ 987654324@

我试图让它工作超过两天。请看一下我的代码,我用解释对其进行了注释:

主活动:

private AdView adView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    binding = ActivityMainBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());
    if(privacyPolicyAcceptedAndHasNoPremium()){ //at first i check the shared preferences
        binding.adPlaceholder.setVisibility(View.VISIBLE); //the adPlaceholder starts as GONE when i set it to VISIBLE the rest of the content slides up
        List<String> testDevices = Arrays.asList(AdRequest.DEVICE_ID_EMULATOR, "...");
        RequestConfiguration requestConfiguration = new RequestConfiguration.Builder().setTestDeviceIds(testDevices).build();
        MobileAds.setRequestConfiguration(requestConfiguration);

        MobileAds.initialize(MainActivity.this, initializationStatus -> {});

        adView = new AdView(this); //i create the adView programmatically but using the xml view element causes the same problem
        adView.setAdSize(AdSize.LARGE_BANNER);
        adView.setAdUnitId("...");
        adView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() { // this gets called so i think the ad is successfully downloaded from the google server. i put all kinds of code in here to force the adView to show
                //setContentView(binding.getRoot());
                //binding.adPlaceholder.addView(new View(MainActivity.this));
                //binding.adPlaceholder.forceLayout();
                //binding.constraintLayout.requestLayout();
                //adView.invalidate();
                //adView.bringToFront();
                //adView.setVisibility(AdView.GONE);
                //adView.setVisibility(AdView.VISIBLE);
                //adView.setBackgroundColor(Color.TRANSPARENT);
            }
        }
        AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
        binding.adPlaceholder.addView(adView); //i add the adView into the empty LinearLayout. i tried to add it directly to binding.constraintLayout too.
        adView.loadAd(adRequestBuilder.build());
}

activity_main.xml:

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <!-- this is the empty container into which i load the ads. it sits at the bottom of the screen and is not visible before an ad is showing -->
    <LinearLayout
        android:id="@+id/adPlaceholder"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:orientation="vertical"
        android:visibility="gone" />

    <!-- this MotionLayout contains the rest of the app (except the toolbar). it slides up when an ad is showing -->
    <androidx.constraintlayout.motion.widget.MotionLayout
        android:id="@+id/motionLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layoutDescription="@xml/activity_main_scene"
        app:layout_constraintBottom_toTopOf="@+id/adPlaceholder"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:context=".MainActivity">
    ...
    </androidx.constraintlayout.motion.widget.MotionLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

清单:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application ...>
    <meta-data
        android:name="com.google.android.gms.ads.APPLICATION_ID"
        android:value="..." />
    ...
</application>

编辑:

【问题讨论】:

  • 您能否尝试在手机上的开发者选项中打开布局边界并检查 adPlaceholder 是否可见?
  • @Amod Gokhale 是的,它是可见的(但只显示一个透明/白色框)。我还尝试将其直接添加到 baseLayout 而没有任何可见性更改。我刚刚添加了 Layout Inspector 的屏幕截图。
  • 只有真实身份证或测试身份证有问题吗?
  • @Amod Gokhale 两个
  • aha.. MotionLayout contraintlayout 看起来不正确

标签: java android admob ads


【解决方案1】:

我终于找到了适合我的方法。

我只是将窗口背景颜色设置为它已有的颜色。这真的很快,不会重新创建活动。

但是我不知道为什么这有效,为什么只有这对我有效。我猜它以与其他问题的许多其他解决方案类似的方式更新视图。

adView.setAdListener(new AdListener() {
    @Override
    public void onAdLoaded() {
        super.onAdLoaded();
        getWindow().setBackgroundDrawableResource(R.color.background);
    }
}

【讨论】:

    猜你喜欢
    • 2014-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    相关资源
    最近更新 更多