如何将插页式广告添加到网页视图?

时间:2016-06-02 16:00:14

标签: android eclipse webview admob interstitial

无论如何,我只想在我的主要活动中使用插页式广告

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

2 个答案:

答案 0 :(得分:0)

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

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。在您要添加Activity的{​​{1}}上应包含以下代码:

ad 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

答案 1 :(得分:0)

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