Android平台使用-谷歌广告联盟AdMob-赚取收益的编程方法

谷歌广告联盟AdMob是什么

谷歌广告联盟AdMob是使用在Android、IOS平台的广告SDK。

常见的广告形式

  • 横幅广告
  • 插页广告
  • 视频广告
    Android平台使用-谷歌广告联盟AdMob-赚取收益的编程方法_第1张图片

横幅广告

gradle依赖

dependencies {
    implementation 'com.google.android.gms:play-services-ads:20.6.0'
    implementation 'androidx.work:work-runtime:2.7.0'
}

JAVA代码

private AdView mBannerAdView = null;
mBannerAdView = findViewById(R.id.adView);

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

xml代码

<com.google.android.gms.ads.AdView
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    ads:adSize="BANNER"
    ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
com.google.android.gms.ads.AdView>

插页广告

JAVA代码

private static final String ADMOB_INTER_AD_UNIT_ID = "ca-app-pub-3940256099942544/1033173712";

private InterstitialAdLoadCallback mInterAdLoadCb = new InterstitialAdLoadCallback() {
    @Override
    public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
        Log.d(TAG, "mInterAdLoadCb: onAdLoaded()");
    }

    @Override
    public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
        // Handle the error
        Log.e(TAG, "mInterAdLoadCb: onAdFailedToLoad(): " + loadAdError.getMessage());
    }
};

AdRequest _adRequest2 = new AdRequest.Builder().build();
InterstitialAd.load(this, ADMOB_INTER_AD_UNIT_ID, _adRequest2, mInterAdLoadCb);

视频广告

JAVA代码

private static final String ADMOB_REWARDED_AD_UNIT_ID = "ca-app-pub-3940256099942544/5224354917";

RewardedAdLoadCallback _adLoadCallback = new RewardedAdLoadCallback() {
    @Override
    public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
        // Ad failed to load.
        Log.e(TAG, "RewardedAdLoadCallback: onAdFailedToLoad(): ERROR= " + loadAdError);
    }

    @Override
    public void onAdLoaded(@NonNull RewardedAd rewardedAd) {
        // Ad successfully loaded.
        Log.d(TAG, "RewardedAdLoadCallback: onAdLoaded()");
    }
};
RewardedAd.load(this, ADMOB_REWARDED_AD_UNIT_ID, new AdRequest.Builder().build(), _adLoadCallback);

demo APP

站内免费试用

你可能感兴趣的:(Android开发,android)