Android应用在国外之各种广告集成(Vungle,AppLovin,Google,Facebook)


今天简单介绍下国外的一些主流广告平台,主要介绍其中的视频奖励广告

  • Vungle
    领先的In-app视频广告平台,被Business Insider评为15家热门的未上市广告技术公司。主要应用在游戏类app上,其中娱乐场类游戏占比较高。
集成方式:
  1. 添加依赖
dependencies { 
  // Vungle SDK
  implementation 'com.vungle:publisher-sdk-android:6.4.11'

  // Either appcompat-v7 or support-v4 is need for SDK operation 
  implementation 'com.android.support:appcompat-v7:28.0.0'  // Either
  implementation 'com.android.support:support-v4:27.1.1'    // Or

  // Recommended for SDK to be able to get Android Advertising ID
  implementation 'com.google.android.gms:play-services-basement:16.0.0'
  implementation 'com.google.android.gms:play-services-ads-identifier:16.0.0'
}
  1. 初始化
Vungle.init("YOUR_VUNGLE_APP_ID", getApplicationContext(), new InitCallback() {
    @Override
    public void onSuccess() {
        // Initialization has succeeded and SDK is ready to load an ad or play one if there
        // is one pre-cached already
    }

    @Override
    public void onError(Throwable throwable) {
        // Initialization error occurred - throwable.getLocalizedMessage() contains error message
    }

    @Override
    public void onAutoCacheAdAvailable(String placementId) {
        // Callback to notify when an ad becomes available for the cache optimized placement
        // NOTE: This callback works only for the cache optimized placement. Otherwise, please use
        // LoadAdCallback with loadAd API for loading placements.
    }
};
  1. 请求广告
if (Vungle.isInitialized()) {
    Vungle.loadAd("YOUR_REWARD_Placement_reference_id", new LoadAdCallback() {
        @Override
        public void onAdLoad(String placementReferenceId) { }

        @Override
        public void onError(String placementReferenceId, Throwable throwable) {
            // Load ad error occurred - throwable.getLocalizedMessage() contains error message
        }
    });
}
  1. 播放广告
if (Vungle.canPlayAd("YOUR_REWARD_Placement_reference_id")) {
    AdConfig adConfig = new AdConfig();
    // 静音播放
    // adConfig.setMuted(true); 
    // 自动控制广告方向
    // adConfig.setAutoRotate(true);

    Vungle.playAd("YOUR_REWARD_Placement_reference_id", adConfig, new PlayAdCallback() {
        @Override
        public void onAdStart(String placementReferenceId) { }

        @Override
        public void onAdEnd(String placementReferenceId, boolean completed, boolean isCTAClicked) { 
            // Load Next ad
        }
        
        @Override
        public void onError(String placementReferenceId, Throwable throwable) {
            // Play ad error occurred - throwable.getLocalizedMessage() contains error message
        }
    });
}
  • Applovin
    AppLovin是一家移动广告公司,主要经营广告网络,利用移动网络帮助广告主向特征类似的消费者进行精准投放。
  1. 添加依赖和Key
dependencies {
    implementation "'com.applovin:applovin-sdk:+'"
}

<meta-data android:name="applovin.sdk.key"
            android:value=""  />
  1. 初始化
AppLovinSdk.initializeSdk(context);
  1. 请求广告(加载广告)
private AppLovinIncentivizedInterstitial incentivizedInterstitial;

incentivizedInterstitial = AppLovinIncentivizedInterstitial.create(AppContext.getInstance());
        incentivizedInterstitial.preload(new AppLovinAdLoadListener() {
            @Override
            public void adReceived(AppLovinAd appLovinAd) {
                Log.e(TAG, "adReceived");
            }

            @Override
            public void failedToReceiveAd(int errorCode) {
            }
        });
  1. 播放广告
    /**
     * 播放Applovin广告
     */
    private void playAppLovinAds(final Activity mActivity) {

        if (!incentivizedInterstitial.isAdReadyToDisplay()){
            return;
        }

        AppLovinAdRewardListener adRewardListener = ;

        // Video Playback Listener
        AppLovinAdVideoPlaybackListener adVideoPlaybackListener = ;

        // Ad Dispaly Listener
        AppLovinAdDisplayListener adDisplayListener =;

        // Ad Click Listener
        AppLovinAdClickListener adClickListener = ;

        incentivizedInterstitial.show(mActivity, adRewardListener, adVideoPlaybackListener, adDisplayListener, adClickListener);
    }
  • Google
  1. 添加依赖和Key
allprojects {
    repositories {
        google()
        jcenter()
    }
}

dependencies {
    implementation 'com.google.android.gms:play-services-ads:18.2.0'
}

<manifest>
    <application>
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>
    </application>
</manifest>
  1. 初始化
 MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
            }
        });

private RewardedAd rewardedAd;
rewardedAd = new RewardedAd(this,"广告单元 ID");
  1. 请求广告(加载广告)
RewardedAdLoadCallback adLoadCallback = new RewardedAdLoadCallback() {
            @Override
            public void onRewardedAdLoaded() {
                // Ad successfully loaded.
            }

           @Override
            public void onRewardedAdFailedToLoad(int errorCode) {
                // Ad failed to load.
            }
        };
        rewardedAd.loadAd(new AdRequest.Builder().build(), adLoadCallback);
  1. 播放广告
if (rewardedAd.isLoaded()) {
            Activity activityContext = ...;
            RewardedAdCallback adCallback = new RewardedAdCallback() {
                public void onRewardedAdOpened() {
                    // Ad opened.
                }

                public void onRewardedAdClosed() {
                    // Ad closed.
                }

                public void onUserEarnedReward(@NonNull RewardItem reward) {
                    // User earned reward.
                }

                public void onRewardedAdFailedToShow(int errorCode) {
                    // Ad failed to display
                }
            };
            rewardedAd.show(activityContext, adCallback);
        } else {
            Log.d("TAG", "The rewarded ad wasn't loaded yet.");
        }
  1. 预加载广告(示例)
public RewardedAd createAndLoadRewardedAd() {
        RewardedAd rewardedAd = new RewardedAd(this,
                "ca-app-pub-3940256099942544/5224354917");
        RewardedAdLoadCallback adLoadCallback = new RewardedAdLoadCallback() {
            @Override
            public void onRewardedAdLoaded() {
                // Ad successfully loaded.
            }

            @Override
            public void onRewardedAdFailedToLoad(int errorCode) {
                // Ad failed to load.
            }
        };
        rewardedAd.loadAd(new AdRequest.Builder().build(), adLoadCallback);
        return rewardedAd;
}

@Override
public void onRewardedAdClosed() {
    this.rewardedAd = createAndLoadRewardedAd();
}
  • Facebook
  1. 添加依赖和Key
repositories {
    // Ensure that you have mavenCentral() in list of repositories.
    mavenCentral() 
}
dependencies { 
    implementation 'com.android.support:support-annotations:28.0.0' // Required Dependency by Audience Network SDK
    implementation 'com.facebook.android:audience-network-sdk:5.+'
}
  1. 初始化
AudienceNetworkAds.initialize(context);
  1. 请求广告(加载广告)
private void loadInstreamAd() {
        // Instantiate an InstreamVideoAdView object. 
        // NOTE: the placement ID will eventually identify this as your App, you can ignore it for
        // now, while you are testing and replace it later when you have signed up.
        // While you are using this temporary code you will only get test ads and if you release
        // your code like this to the Google Play your users will not receive ads (you will get a no fill error).
        adView = new InstreamVideoAdView(
                this,
                "YOUR_PLACEMENT_ID",
                new AdSize(
                        pxToDP(adContainer.getMeasuredWidth()),
                        pxToDP(adContainer.getMeasuredHeight()))
        );
        // set ad listener to handle events
        adView.setAdListener(new InstreamVideoAdListener() {
            @Override
            public void onError(Ad ad, AdError adError) {
                // Instream video ad failed to load
                Log.e(TAG, "Instream video ad failed to load: " + adError.getErrorMessage());
            }

            @Override
            public void onAdLoaded(Ad ad) {
                // Instream video ad is loaded and ready to be displayed
                Log.d(TAG, "Instream video ad is loaded and ready to be displayed!");

                // Race condition, load() called again before last ad was displayed
                 if(adView == null || !adView.isAdLoaded()) {
                    return;
                }
            }


            @Override
            public void onAdVideoComplete(Ad ad) {
                // Instream Video View Complete - the video has been played to the end.
                // You can use this event to continue your video playing
                Log.d(TAG, "Instream video completed!");
            }


            @Override
            public void onAdClicked(Ad ad) {
                Log.d(TAG, "Instream video ad clicked!");
            }

            @Override
            public void onLoggingImpression(Ad ad) {
                // Instream Video ad impression - the event will fire when the
                // video starts playing
                Log.d(TAG, "Instream video ad impression logged!");
            }
        });
        adView.loadAd();
    }
  1. 播放广告
private void showInstreamVideoAdWithDelay() {  
        // Check if adView has been loaded successfully
        if (adView == null || !adView.isAdLoaded() ) {
            return;
        }
        // Check if ad is already expired or invalidated, and do not show ad if that is the case.
        if (adView.isAdInvalidated()) {
            return;
        }
        adView.show();
    }

####至此,四个广告平台的集成都已完毕。
要注意的是,如果需要多次播放广告的话,建议每次播放广告完成之后马上去预加载下一个广告,每个广告平台都是,否则有可能无法继续播放广告(相当于每次加载的广告只能播放一次)。具体做法可以参考google广告的第五点广告预加载示例,其他广告同理。

谢谢大家支持!
——积木

发布了8 篇原创文章 · 获赞 3 · 访问量 2921

猜你喜欢

转载自blog.csdn.net/liuzhengisme/article/details/101687603