网站首页
google 接入广告踩的坑
发布时间:2020-04-23 09:47查看次数:2999
layaair 打包Android 上传Google 接入横幅/视频广告方法
1.添加SDK库~~
gradle implementation 'com.google.android.gms:play-services-ads:19.1.0'
2.添加广告APP ID
AndroidManifest
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-6348357344820014~********" />
3.就是根据Google的 示例方法 视频广告接入顺利
4.主要BUG在banner广告位置
因为横幅广告要位于 游戏上层所以要加入浮层
参考论坛给的方法 自定义一个布局里边有两个容器
一个容器放置 游戏 一个容器放置广告
布局代码如下
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:ads="http://schemas.android.com/apk/res-auto">
<FrameLayout
android:id="@+id/game_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"/>
<FrameLayout
android:id="@+id/ad_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:layout_marginBottom="0dp" />
5.由于视频广告会堵塞主UI线程 所以初始化使用线程初始化
6.为方便后续操作 封装了一个 自定义的 广告类~~
package demo; import android.app.Activity; import android.util.DisplayMetrics; import android.util.Log; import android.view.Display; import android.view.View; import android.view.WindowManager; import android.widget.FrameLayout; import com.demaxiya.shuniandamaoxian.R; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdSize; import com.google.android.gms.ads.AdView; import com.google.android.gms.ads.MobileAds; import com.google.android.gms.ads.rewarded.RewardedAd; import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback; public class MyGoogLeAds { public static MyGoogLeAds mSelf = null; private Activity mActivity = null; public RewardedAd rewardedAd; private int status = 0; //广告播放状态 private AdView adView; private FrameLayout adContainerView; //banner容器 public MyGoogLeAds(Activity mainActivity) { mActivity = mainActivity; MyGoogLeAds.mSelf = this; MobileAds.initialize(this.mActivity); AdRequest request = new AdRequest.Builder().addTestDevice("720BEDB04B1A5C87F6219AD944A5FE67").build(); adContainerView = mActivity.findViewById(R.id.ad_view); mActivity.runOnUiThread(new Runnable() { @Override public void run() { MyGoogLeAds.mSelf.initThis(); } }); } public void initThis() { this.rewardedAd = new RewardedAd(this.mActivity, "ca-app-pub-3940256099942544/5224354917"); RewardedAdLoadCallback adLoadCallback = new RewardedAdLoadCallback() { @Override public void onRewardedAdLoaded() { // Ad successfully loaded. MyGoogLeAds.mSelf.status = 1; Log.e("888", "==============激励视频 加载成功"); } @Override public void onRewardedAdFailedToLoad(int errorCode) { // Ad failed to load. Log.e("888", "==============激励视频 加载失败!%d" + errorCode); switch (errorCode) { case 2: Log.e("888", "==============激励视频 网络错误?"); break; } MyGoogLeAds.mSelf.status = 2; } }; this.rewardedAd.loadAd(new AdRequest.Builder().build(), adLoadCallback); } public RewardedAd getRewardedAd() throws InterruptedException { if (this.rewardedAd == null){ this.rewardedAd = new RewardedAd(this.mActivity, "ca-app-pub-3940256099942544/5224354917"); } return this.rewardedAd; } public void reSetAd() { if (this.rewardedAd.isLoaded() != true) { this.initThis(); } } /** * 展示横幅广告 */ public void showBannerAd(){ this.initBanner(); } /** * 关闭横幅广告 */ public void closeBanner(){ adContainerView.removeAllViews(); adContainerView.setVisibility(View.INVISIBLE); } /** * 初始化横幅广告 */ private void initBanner(){ // Step 1 - Create an AdView and set the ad unit ID on it. adView = new AdView(mActivity); adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111"); adContainerView.addView(adView); adContainerView.setVisibility(View.VISIBLE); loadBanner(); } /** * 加载横幅广告 */ private void loadBanner() { AdRequest adRequest = new AdRequest.Builder().addTestDevice("720BEDB04B1A5C87F6219AD944A5FE67").build(); AdSize adSize = getAdSize(); adView.setAdSize(adSize); adView.loadAd(adRequest); } /** * 获取屏幕宽度 * @return */ private AdSize getAdSize() { // Step 2 - Determine the screen width (less decorations) to use for the ad width. Display display = mActivity.getWindowManager().getDefaultDisplay(); DisplayMetrics outMetrics = new DisplayMetrics(); display.getMetrics(outMetrics); float widthPixels = outMetrics.widthPixels; float density = outMetrics.density; int adWidth = (int) (widthPixels / density); // Step 3 - Get adaptive ad size and return for setting on the ad view. return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(mActivity, adWidth); } }
安卓反射机制接口
2020年4月25日11:08:50 深圳 宝安 德玛西亚
关键字词:javascript##