前面一篇文章我们讲到的是admob的广告变现包含admob的banner广告以及插屏广告,具体操作请转到:admob广告变现
本篇文章主要讲的是admob的激励视频广告以及admob的原生广告,这里先吐槽一下applovin广告主,本人接入applovin广告,单价低不说,刚刚跑到$10,他就直接停账号,不说任何理由,不给任何解释。发邮件给他们技术支持,也不会给回复。烂平台。
1、激励视频广告:
激励视频广告效果其实还是不错的,参加过几次线下的Google Admob交流会,都比较提倡使用激励视频广告,激励视频广告用的最多的情况就是游戏里面,如:当用户观看激励视频后,奖励用户一条生命或者奖励用户金币等等,如果配合firebase的一些功能做的话,效果更佳。激励视频的效果图如下所示:
会有视频倒计时提示。代码实现:
添加依赖:
implementation 'com.google.firebase:firebase-ads:16.0.1'
当然,你也可以使用google admob文档里的最新依赖。
代码封装,本次封装其实和插屏广告的封装类似:
package com.znv.linkup.firebase;
import android.app.ProgressDialog;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.reward.RewardItem;
import com.google.android.gms.ads.reward.RewardedVideoAd;
import com.google.android.gms.ads.reward.RewardedVideoAdListener;
public class ReWardVideoUtils {
private RewardedVideoAd mRewardVideo;
private Context mContext;
private static ReWardVideoUtils mRewardVideoUtils;
private static String TAG = ReWardVideoUtils.class.getSimpleName();
private boolean isReLoad = false;
private AdsInfoBean mAdsInfoBean;
private ReWardInterface mReWardInterface;
public static ReWardVideoUtils getInstance() {
if (null == mRewardVideoUtils) {
synchronized (ReWardVideoUtils.class) {
mRewardVideoUtils = new ReWardVideoUtils();
}
}
return mRewardVideoUtils;
}
public void initRewardVideo(Context context, final AdsInfoBean adsInfoBean) {
if (null == adsInfoBean) {
Log.e(TAG, "adsinfo can't be null");
return;
}
mContext = context;
if (CustomCtrlControl.verifyAdsInfoReward(adsInfoBean)) {
mAdsInfoBean = adsInfoBean;
MobileAds.initialize(mContext, mAdsInfoBean.getmAppId());
mRewardVideo = MobileAds.getRewardedVideoAdInstance(mContext);
mRewardVideo.setRewardedVideoAdListener(new RewardedVideoAdListener() {
@Override
public void onRewardedVideoAdLoaded() {
Log.d(TAG, "激励视频加载成功");
if (null!=mReWardInterface){
mReWardInterface.reWardLoadSuccess();
}
}
@Override
public void onRewardedVideoAdOpened() {
}
@Override
public void onRewardedVideoStarted() {
}
@Override
public void onRewardedVideoAdClosed() {
Log.d(TAG, "激励视频广告被关闭");
loadRewardAd();
if (null != mReWardInterface) {
mReWardInterface.adClose();
}
}
@Override
public void onRewarded(RewardItem rewardItem) {
if (null != mReWardInterface) {
mReWardInterface.rewardUser();
}
}
@Override
public void onRewardedVideoAdLeftApplication() {
}
@Override
public void onRewardedVideoAdFailedToLoad(int i) {
Log.e(TAG, "激励视频加载失败-->" + i);
if (!isReLoad) {
isReLoad = true;
loadRewardAd();
}
//重新加载
if (null != mReWardInterface) {
mReWardInterface.adLoadFailed();
}
}
@Override
public void onRewardedVideoCompleted() {
Log.e(TAG, "激励视频播放完成-->");
if (null != mReWardInterface) {
mReWardInterface.videoComplete();
}
}
});
loadRewardAd();
}
}
private void loadRewardAd() {
if (null != mAdsInfoBean && !TextUtils.isEmpty(mAdsInfoBean.getmRewardId())) {
mRewardVideo.loadAd(mAdsInfoBean.getmRewardId(), new AdRequest.Builder().build());
}
}
public void showReWardAd(ReWardInterface reWardInterface) {
mReWardInterface = reWardInterface;
if (canShowReWardAd()) {
mRewardVideo.show();
isReLoad = false;
} else {
loadRewardAd();
}
}
private boolean canShowReWardAd() {
return null != mRewardVideo && mRewardVideo.isLoaded();
}
public RewardedVideoAd getmRewardVideo() {
return mRewardVideo;
}
public interface ReWardInterface {
void videoComplete();
void adClose();
void adLoadFailed();
void rewardUser();
void reWardLoadSuccess();
}
}
具体实现可以看代码
二、原生广告:(官方链接)
原生广告不像其他广告直接到admob后台就可以创建的,还需要手动申请
官方介绍:原生广告是通过平台原本就有的界面组件向用户呈现的广告素材资源。这种广告使用您在构建布局时已经采用的同类视图进行展示,而且能以和周围视觉设计相称的形式呈现,让用户有浑然一体的使用体验。从代码编写的角度来说,这意味着当原生广告加载时,您的应用会收到一个包含其素材资源的 NativeAd
对象,然后就由此应用(而不是 SDK)负责展示它们了。
其实就是将插屏广告的素材拿到手后,自己写布局,然后填充。拆分重组的过程,值得注意的是,ad标识一定不能少。政策链接
github示例代码:传送门:Github
原生广告包含:应用下载类以及内容类广告,区别是:应用下载类是跳转到Googleplay市场,内容类是打开网页。
代码演示:以下代码是在依赖版本15以下,15以上的可以参考google官方示例:
package com.happlay.tech.videoshow2.nativeAd;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdLoader;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.VideoController;
import com.google.android.gms.ads.VideoOptions;
import com.google.android.gms.ads.formats.MediaView;
import com.google.android.gms.ads.formats.NativeAd;
import com.google.android.gms.ads.formats.NativeAdOptions;
import com.google.android.gms.ads.formats.NativeAppInstallAd;
import com.google.android.gms.ads.formats.NativeAppInstallAdView;
import com.google.android.gms.ads.formats.NativeContentAd;
import com.google.android.gms.ads.formats.NativeContentAdView;
import java.util.List;
public class NativeAdUtils {
public static void showNative(final AdsInfoBean adsInfoBean,final FrameLayout frameLayout,final ProgressBar progressBar,final Context mContext,final boolean isAppinstallAd,final boolean isConstentAd){
if (!CustomCtrlControl.verifyAdsInfoNative(adsInfoBean)) {
frameLayout.setVisibility(View.GONE);
progressBar.setVisibility(View.GONE);
} else {
frameLayout.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.VISIBLE);
MobileAds.initialize(mContext, adsInfoBean.getmAppId());
AdLoader.Builder builder = new AdLoader.Builder(mContext, adsInfoBean.getmNativeAd());
//应用下载类广告
if (isAppinstallAd) {
builder.forAppInstallAd(new NativeAppInstallAd.OnAppInstallAdLoadedListener() {
@Override
public void onAppInstallAdLoaded(NativeAppInstallAd nativeAppInstallAd) {
NativeAppInstallAdView adView = (NativeAppInstallAdView) LayoutInflater.from(mContext).inflate(R.layout.ad_appinstall, null);
populateAppinstallAd(nativeAppInstallAd, adView);
frameLayout.removeAllViews();
frameLayout.addView(adView);
}
});
}
//content 广告
if (isConstentAd) {
builder.forContentAd(new NativeContentAd.OnContentAdLoadedListener() {
@Override
public void onContentAdLoaded(NativeContentAd nativeContentAd) {
NativeContentAdView adView = (NativeContentAdView) LayoutInflater.from(mContext).inflate(R.layout.ad_content, null);
populateContentAd(nativeContentAd, adView);
frameLayout.removeAllViews();
frameLayout.addView(adView);
}
});
}
//视频类广告相关信息,主要是在应用下载类广告中
VideoOptions videoOptions = new VideoOptions.Builder()
.setStartMuted(true)
.build();
NativeAdOptions adOptions = new NativeAdOptions.Builder()
.setVideoOptions(videoOptions)
.build();
builder.withNativeAdOptions(adOptions);
AdLoader adLoader = builder.withAdListener(new AdListener() {
@Override
public void onAdFailedToLoad(int errorCode) {
Firebase.getInstance(mContext).logEvent("原生广告","load","failed->"+errorCode);
}
@Override
public void onAdClosed() {
super.onAdClosed();
Firebase.getInstance(mContext).logEvent("原生广告","close");
}
@Override
public void onAdLeftApplication() {
super.onAdLeftApplication();
//for test load other ads,when the users click the ads
showNative(adsInfoBean, frameLayout, progressBar,mContext,isAppinstallAd, isConstentAd);
Firebase.getInstance(mContext).logEvent("原生广告","refresh","刷新广告");
}
@Override
public void onAdOpened() {
super.onAdOpened();
}
@Override
public void onAdLoaded() {
super.onAdLoaded();
}
@Override
public void onAdClicked() {
super.onAdClicked();
Firebase.getInstance(mContext).logEvent("原生广告","click");
}
@Override
public void onAdImpression() {
super.onAdImpression();
//广告显示
progressBar.setVisibility(View.GONE);
Firebase.getInstance(mContext).logEvent("原生广告","显示","显示成功");
}
}).build();
adLoader.loadAd(new AdRequest.Builder()/**.addTestDevice("6791DA02E742EA510F011C29AFEE8646")**/.build());
//执行此方法会连续请求至少一次最多三次的广告
// adLoader.loadAds(new AdRequest.Builder().addTestDevice("6791DA02E742EA510F011C29AFEE8646").build(), 3);
}
}
private static void populateContentAd(NativeContentAd nativeContentAd, NativeContentAdView adView) {
adView.setHeadlineView(adView.findViewById(R.id.contentad_headline));
adView.setImageView(adView.findViewById(R.id.contentad_image));
adView.setBodyView(adView.findViewById(R.id.contentad_body));
adView.setCallToActionView(adView.findViewById(R.id.contentad_action));
adView.setLogoView(adView.findViewById(R.id.contentad_logo));
adView.setAdvertiserView(adView.findViewById(R.id.contentad_advertiser));
((TextView) adView.getHeadlineView()).setText(nativeContentAd.getHeadline());
((TextView) adView.getBodyView()).setText(nativeContentAd.getBody());
((TextView) adView.getCallToActionView()).setText(nativeContentAd.getCallToAction());
((TextView) adView.getAdvertiserView()).setText(nativeContentAd.getAdvertiser());
List images = nativeContentAd.getImages();
if (images.size() > 0) {
((ImageView) adView.getImageView()).setImageDrawable(images.get(0).getDrawable());
}
NativeAd.Image logoImage = nativeContentAd.getLogo();
if (null == logoImage) {
adView.getLogoView().setVisibility(View.INVISIBLE);
// Toast.makeText(mac, "logoImage is null", Toast.LENGTH_SHORT).show();
} else {
adView.getLogoView().setVisibility(View.VISIBLE);
((ImageView) adView.getLogoView()).setImageDrawable(logoImage.getDrawable());
}
adView.setNativeAd(nativeContentAd);
}
private static void populateAppinstallAd(NativeAppInstallAd nativeAppInstallAd, NativeAppInstallAdView adView) {
VideoController controller = nativeAppInstallAd.getVideoController();
controller.setVideoLifecycleCallbacks(new VideoController.VideoLifecycleCallbacks() {
public void onVideoEnd() {
// Publishers should allow native ads to complete video playback before refreshing
// or replacing them with another ad in the same UI location.
super.onVideoEnd();
}
});
MediaView mediaView = adView.findViewById(R.id.ad_media);
ImageView mainImageView = adView.findViewById(R.id.ad_image);
if (controller.hasVideoContent()) {
adView.setMediaView(mediaView);
mainImageView.setVisibility(View.GONE);
// videoStatus.setText(String.format(Locale.getDefault(),
// "Video status: Ad contains a %.2f:1 video asset.",
// controller.getAspectRatio()));
} else {
adView.setImageView(mainImageView);
mediaView.setVisibility(View.GONE);
// At least one image is guaranteed.
List images = nativeAppInstallAd.getImages();
mainImageView.setImageDrawable(images.get(0).getDrawable());
}
adView.setHeadlineView(adView.findViewById(R.id.ad_headline));
adView.setBodyView(adView.findViewById(R.id.ad_body));
adView.setCallToActionView(adView.findViewById(R.id.ad_call_to_action));
adView.setIconView(adView.findViewById(R.id.ad_app_icon));
adView.setPriceView(adView.findViewById(R.id.ad_price));
adView.setStarRatingView(adView.findViewById(R.id.ad_stars));
adView.setStoreView(adView.findViewById(R.id.ad_store));
// adView.setAdvertiserView(adView.findViewById(R.id.ad_advertiser));
// Some assets are guaranteed to be in every UnifiedNativeAd.
((TextView) adView.getHeadlineView()).setText(nativeAppInstallAd.getHeadline());
((TextView) adView.getBodyView()).setText(nativeAppInstallAd.getBody());
((Button) adView.getCallToActionView()).setText(nativeAppInstallAd.getCallToAction());
// These assets aren't guaranteed to be in every UnifiedNativeAd, so it's important to
// check before trying to display them.
if (nativeAppInstallAd.getIcon() == null) {
adView.getIconView().setVisibility(View.GONE);
} else {
((ImageView) adView.getIconView()).setImageDrawable(
nativeAppInstallAd.getIcon().getDrawable());
adView.getIconView().setVisibility(View.VISIBLE);
}
if (nativeAppInstallAd.getPrice() == null) {
adView.getPriceView().setVisibility(View.INVISIBLE);
} else {
adView.getPriceView().setVisibility(View.VISIBLE);
((TextView) adView.getPriceView()).setText(nativeAppInstallAd.getPrice());
}
if (nativeAppInstallAd.getStore() == null) {
adView.getStoreView().setVisibility(View.INVISIBLE);
} else {
adView.getStoreView().setVisibility(View.VISIBLE);
((TextView) adView.getStoreView()).setText(nativeAppInstallAd.getStore());
}
if (nativeAppInstallAd.getStarRating() == null) {
adView.getStarRatingView().setVisibility(View.INVISIBLE);
} else {
((RatingBar) adView.getStarRatingView())
.setRating(nativeAppInstallAd.getStarRating().floatValue());
adView.getStarRatingView().setVisibility(View.VISIBLE);
}
//
// if (nativeAppInstallAd.getAdvertiser() == null) {
// adView.getAdvertiserView().setVisibility(View.INVISIBLE);
// } else {
// ((TextView) adView.getAdvertiserView()).setText(nativeAd.getAdvertiser());
// adView.getAdvertiserView().setVisibility(View.VISIBLE);
// }
adView.setNativeAd(nativeAppInstallAd);
}
}
写的比较简陋,不懂的可以留言,大家一起学习学习。今天就到这里了。谢谢。
贡献学习资料自取:自取