0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AdMobをAndroidに組み込んでみよう(Video広告編)

Posted at

AdMobをAndroifに組み込んでみようという事で覚書

「AdMobをAndroidに組み込んでみよう(環境設定編)」の続きです。
https://qiita.com/sanoh/items/79d04b021d18bdff0e06
■Step1.画面レイアウトを設定しよう

・activity_main.xmlをひらき「Button」を作成、名前を「mMyButton」とします。(レイアウトエディタの使い方は省きます)2020-06-16 (18).png
ボタンの配置はこのようにしておくとよいでしょう
2020-06-16 (19).png
■Setp2.プログラム

・MainActivityを編集します。

package com.toox.admod03

import android.app.Activity
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.annotation.NonNull
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.rewarded.*
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    private lateinit var rewardedAd: RewardedAd
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        rewardedAd = createAndLoadRewardedAd("ca-app-pub-3940256099942544/5224354917")
        val adLoadCallback = object : RewardedAdLoadCallback() {
            override fun onRewardedAdLoaded() { // Ad successfully loaded.
            }
            override fun onRewardedAdFailedToLoad(errorCode: Int) { // Ad failed to load.
            }
        }
        rewardedAd.loadAd(AdRequest.Builder().build(), adLoadCallback)
        mMyButton.setOnClickListener {
            if (rewardedAd.isLoaded) {
                val activityContext: Activity = this@MainActivity
                val adCallback = object : RewardedAdCallback() {
                    override fun onRewardedAdOpened() { //開くとき
                    }
                    override fun onRewardedAdClosed() { //ムービーとじるときに、次のしこみ
                        rewardedAd =
                            createAndLoadRewardedAd("ca-app-pub-3940256099942544/5224354917")
                    }
                    override fun onUserEarnedReward(@NonNull reward: RewardItem) { //  最後までみたらくる
                    }
                    override fun onRewardedAdFailedToShow(errorCode: Int) { //  失敗
                    }
                }
                rewardedAd.show(activityContext, adCallback)
            }
        }
    }
    fun createAndLoadRewardedAd(adUnitId:String): RewardedAd {
        val rewardedAd = RewardedAd(this, adUnitId)
        val adLoadCallback = object : RewardedAdLoadCallback() {
            override fun onRewardedAdLoaded() { // Ad successfully loaded.
            }
            override fun onRewardedAdFailedToLoad(errorCode: Int) { // Ad failed to load.
            }
        }
        rewardedAd.loadAd(AdRequest.Builder().build(), adLoadCallback)
        return rewardedAd
    }
}

2020-06-16 (20).png
■Step3.実行

プログラムを実行すると
2020-06-16 (21).png
ボタンを押すと
2020-06-16 (22).png
となります

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?