您是否需要为AdView明确调用暂停,恢复或销毁? [英] Do you need to explicitly call pause, resume, destroy for an AdView?

查看:89
本文介绍了您是否需要为AdView明确调用暂停,恢复或销毁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是AdView是否自动绑定到活动生命周期,还是您必须明确调用暂停恢复毁灭事件?是否取决于AdView的大小?我正在使用横​​幅广告。

Is the AdView automatically tied to the activity life cycle or do you have to explicitly call the pause, resume, destroy events? Does it depend on the size of the AdView? I'm using banner ads.

我找不到很多其他这样做的代码示例,而Android的主要帮助文章也没有提及(只需将广告加载到onCreate中即可,而无需执行其他任何操作)。

I couldn't find a lot of code samples of other people doing this and the main Android help article doesn't mention it (they just load the ad in onCreate and don't do anything else with it).

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


推荐答案

它是您的选择,但您不愿意真的不需要一次在Android生命周期中一次一次地暂停,恢复或销毁它。简单地在onCreate中加载横幅广告和非页内广告,并在某些点击事件中调用非页内广告,一旦用户关闭广告,只需重新加载非页内广告即可。

Its your choice but you don't really need to pause, resume or destroy it again and again with the android life cycle. Simple load the banner and interstitial ads in onCreate and call the interstitial ads in some click event and once the ad is closed by user simply reload the interstitial ads.

这里是给定非页内广告示例

Here is the given example for interstitial ads

调用此函数可在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
            }

这篇关于您是否需要为AdView明确调用暂停,恢复或销毁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆