如何添加插页广告来的WebView?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何添加插页广告来的WebView?相关的知识,希望对你有一定的参考价值。

我只是想插页广告在我的主要活动好歹

    private void initAdmob() {
        if (isAdmobEnable) {
            adView = new AdView(this);
            adView.setAdSize(AdSize.BANNER);
            adView.setAdUnitId(MY_AD_UNIT_ID);

            RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
        //  p.addRule(RelativeLayout.BELOW, webviewProgress.getId());
             p.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            parentView.addView(adView, p);

            AdRequest adRequest = new AdRequest.Builder().build();

            // Start loading the ad in the background.
            adView.loadAd(adRequest);
        }
    }
答案

好吧,因为你的代码非常大,我会给你一个场景如何做,你可以用我的代码来指导。

1.首先确保你的Google as services中有build.gradle的代码

compile 'com.google.android.gms:play-services:7.0.0'

2.在manifest.xml上添加这些行

<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.version" android:value="@integer/google_play_services_version"/>
<activity android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
        android:theme="@android:style/Theme.Translucent" />

3.在你要添加Activityad view上应包含以下代码:

private InterstitialAd mInterstitialAd;
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getString(R.string.ad_unit_id));
if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
        mInterstitialAd.show();
} 
else {
       if (!mInterstitialAd.isLoaded() && !mInterstitialAd.isLoaded()) {
        AdRequest adRequest = new AdRequest.Builder().build();
        mInterstitialAd.loadAd(adRequest);
       }  
}

如果你想以编程方式添加它,你应该在WebView上包含你的LinearLayout(更简单),然后添加它:

private AdView adView;
adView = new AdView(this, AdSize.INTERSTITIAL, getString(R.string.ad_unit_id));
LinearLayout root = (LinearLayout)findViewById(R.id.your_main_LinearLayout);
root.addView(adView);
adView.loadAd(new AdRequest());

如果您不想以编程方式执行此操作,只需在AdView下面的xml文件中添加WebView

<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="false"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    ads:adSize="INTERSTITIAL"
    ads:adUnitId="@string/banner_ad_unit_id">   
 </com.google.android.gms.ads.AdView>
另一答案

import com.google.android.gms.ads.AdListener;

private InterstitialAd interstitial;





     interstitial = new InterstitialAd(WebViewActivity.this);
                // Insert the Ad Unit ID
                interstitial.setAdUnitId("ca-app-pub-6466988621230720/20102112499");


    // Request for Ads
    AdRequest adRequest = new AdRequest.Builder()

    // Add a test device to show Test Ads
    // .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
    // .addTestDevice("0A8B6312AD2047B3E0951BFB32026553")
            .build();

    // Load ads into Banner Ads
    adView.loadAd(adRequest);

    // Load ads into Interstitial Ads
    interstitial.loadAd(adRequest);

    // Prepare an Interstitial Ad Listener
    interstitial.setAdListener(new AdListener() {
        public void onAdLoaded() {
            // Call displayInterstitial() function
            displayInterstitial();
        }
    });
}
public void displayInterstitial() {
    // If Ads are loaded, show Interstitial else show nothing.
    if (interstitial.isLoaded()) {
        interstitial.show();
    }
}

以上是关于如何添加插页广告来的WebView?的主要内容,如果未能解决你的问题,请参考以下文章

如何从静态片段中显示Admob插页式广告?

在android上加载webview后没有显示插页式广告?

如何使用“expo-ads-admob”在插页式广告中添加间隔

如何停用Admob插页式广告?

不显示插页式广告

如何在 facebook 受众网络插页式广告中设置点击计数器?