美文网首页
Google AdMob简单集成,主要代码,包含提前缓存一个广告

Google AdMob简单集成,主要代码,包含提前缓存一个广告

作者: wenju | 来源:发表于2020-07-14 15:20 被阅读0次

    1.主要流程不说了,官方很详细(如果要在谷歌市场上架,请一定按照规定集成,否则有下架风险)
    主要代码分享一下

    private String TAG = "AdmobLog------>";
    private RewardedAd rewardedAd;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
    
            }
        });
    
        rewardedAd = new RewardedAd(this,
                "ca-app-pub-3940256099942544/5224354917");
    
        RewardedAdLoadCallback adLoadCallback = new RewardedAdLoadCallback() {
            @Override
            public void onRewardedAdLoaded() {
                // Ad successfully loaded.
                Log.d(TAG,"加载成功");
            }
    
            @Override
            public void onRewardedAdFailedToLoad(int errorCode) {
                // Ad failed to load.
                Log.d(TAG,"加载失败");
            }
        };
        rewardedAd.loadAd(new AdRequest.Builder().build(), adLoadCallback);
    }
    
    public void button(View view) {
        if (rewardedAd.isLoaded()) {
            Activity activityContext = MainActivity.this;
            RewardedAdCallback adCallback = new RewardedAdCallback() {
                @Override
                public void onRewardedAdOpened() {
                    // Ad opened.
                    Log.d(TAG,"广告打开");
                }
    
                @Override
                public void onRewardedAdClosed() {
                    // Ad closed.
                    Log.d(TAG,"广告关闭");
                }
    
                @Override
                public void onUserEarnedReward(@NonNull RewardItem reward) {
                    // User earned reward.
                    Log.d(TAG,"获得奖励");
                }
    
                @Override
                public void onRewardedAdFailedToShow(int errorCode) {
                    // Ad failed to display.
                    Log.d(TAG,"失败播放");
                }
            };
            rewardedAd.show(activityContext, adCallback);
            onRewardedAdClosed();
        } else {
            Log.d(TAG, "奖励广告尚未加载");
        }
    }
    
    public RewardedAd createAndLoadRewardedAd() {
        RewardedAd rewardedAd = new RewardedAd(this,
                "ca-app-pub-3940256099942544/5224354917");
        RewardedAdLoadCallback adLoadCallback = new RewardedAdLoadCallback() {
            @Override
            public void onRewardedAdLoaded() {
                // Ad successfully loaded.
                Log.d(TAG,"预加载成功");
            }
    
            @Override
            public void onRewardedAdFailedToLoad(int errorCode) {
                // Ad failed to load.
                Log.d(TAG,"预加载失败");
            }
        };
        rewardedAd.loadAd(new AdRequest.Builder().build(), adLoadCallback);
        return rewardedAd;
    }
    
    public void onRewardedAdClosed() {
        this.rewardedAd = createAndLoadRewardedAd();
    }
    

    相关文章

      网友评论

          本文标题:Google AdMob简单集成,主要代码,包含提前缓存一个广告

          本文链接:https://www.haomeiwen.com/subject/xsqfhktx.html