来
今天简单介绍下国外的一些主流广告平台,主要介绍其中的视频奖励广告
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'
}
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.
}
};
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
}
});
}
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
}
});
}
dependencies {
implementation "'com.applovin:applovin-sdk:+'"
}
<meta-data android:name="applovin.sdk.key"
android:value="" />
AppLovinSdk.initializeSdk(context);
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) {
}
});
/**
* 播放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);
}
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>
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
private RewardedAd rewardedAd;
rewardedAd = new RewardedAd(this,"广告单元 ID");
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);
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.");
}
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();
}
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.+'
}
AudienceNetworkAds.initialize(context);
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();
}
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广告的第五点广告预加载示例,其他广告同理。
谢谢大家支持!
——积木