【问题标题】:Do you need to explicitly call pause, resume, destroy for an AdView?您是否需要为 AdView 显式调用 pause、resume、destroy?
【发布时间】:2019-12-31 21:28:31
【问题描述】:

AdView 是自动绑定到活动生命周期,还是必须显式调用 pauseresumedestroy 事件?是否取决于 AdView 的大小?我正在使用横​​幅广告。

我找不到很多其他人这样做的代码示例,而且主要的 Android 帮助文章也没有提到它(他们只是在 onCreate 中加载广告,而不用它做任何其他事情)。

https://developers.google.com/android/reference/com/google/android/gms/ads/AdView (代码示例包括暂停/恢复/销毁,它在方法说明中提到我们“应该调用这些方法”,但没有详细说明)。

https://developers.google.com/admob/android/banner (没有提到需要暂停/恢复/销毁)。

http://thetechnocafe.com/a-complete-guide-to-integrating-admob-in-your-android-app/ (暂停并销毁代码中的视频广告,但没有说明原因或给出任何解释)

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    <!-- app content -->

    <com.google.android.gms.ads.AdView
            xmlns:ads="http://schemas.android.com/apk/res-auto"
            android:id="@+id/myAdView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            ads:adSize="BANNER"
            ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
    </com.google.android.gms.ads.AdView>
</LinearLayout>
public class MainActivity extends AppCompatActivity {
    private AdView mAdView;

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

        // ...

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

    // do I need this code as well???
    @Override
    public void onResume() {
        mAdView.resume();
        super.onResume();
    }
    
    @Override
    public void onPause() {
        mAdView.pause();
        super.onPause();
    }
    
    @Override
    public void onDestroy() {
        mAdView.destroy();
        super.onDestroy();
    }

【问题讨论】:

  • 您自己找到答案了吗?我遇到了同样的问题,不知道是否有必要。
  • 为了安全起见,我正在调用 pause、destroy 和 resume,但我还没有收到明确的答复。
  • 嗯,好的,非常感谢。我想我也必须这样做:/

标签: android admob ads android-lifecycle


【解决方案1】:

这是您的选择,但您并不需要在 android 生命周期中一次又一次地暂停、恢复或销毁它。只需在 onCreate 中加载横幅和插页式广告,并在某些点击事件中调用插页式广告,一旦广告被用户关闭,只需重新加载插页式广告。

这是插页式广告的给定示例

调用该函数在onCreate中加载插页式广告

private void requestNewInterstitial() {
    AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .build();

    mInterstitialAd.loadAd(adRequest);
}

将此代码添加到您要加载广告的位置

if (mInterstitialAd.isLoaded()) {

                mInterstitialAd.show();

                mInterstitialAd.setAdListener(new AdListener() {
                    @Override
                    public void onAdClosed() {
                        requestNewInterstitial();
                        //Perform Your functionality here
                    }
                });
            }
            else {
                requestNewInterstitial();
                // Your functionality here as well in case ad was not loaded 
                //  before
            }

【讨论】:

  • 我不是在谈论插页式广告。我说的是横幅广告。另外,我想解释一下为什么你不需要暂停/恢复/销毁广告,背后有一些可靠的来源。 “你真的不需要”不是一个足够好的答案。
【解决方案2】:

是的,我们需要添加这些代码行以节省可用内存量。

    @Override
    public void onResume() {
        super.onResume();
        mAdView.resume();
    }

你将 adview 的 resume() 移到超级构造函数下面。

【讨论】:

  • 您在哪里找到此信息?你能指出任何文档吗?
  • 来自谷歌开发者文档,这里是链接developers.google.com/android/reference/com/google/android/gms/…
  • 您链接的文档没有提到这样做是为了节省内存或您这样做的任何原因。这只是一个代码示例,就像我在原始问题中链接的那样。
  • 我以为您问为什么应该使用此代码。现在它变得复杂了,因为您要求提供活动生命周期。了解每个阶段的 Activity 生命周期 为什么我们先 onPause 然后 onResume。
  • 您需要暂停和恢复广告,因为如果您不暂停它,横幅仍然会在应用处于后台时获得展示,这将导致 admob 限制(无效流量)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-24
  • 1970-01-01
  • 2014-07-16
  • 2020-06-06
相关资源
最近更新 更多