admob广告只展示一次

时间:2018-02-11 14:28:35

标签: kotlin admob ads interstitial

我在我的活动中有adob插页式广告。 我的广告只会在点击按钮时显示一次,但在用户返回同一活动时不会显示

如何在活动开启时实施插页式广告? 我的代码只有在Onclicklistner中添加show()时才有效。

这是我的代码

class ProfileActivity : AppCompatActivity() {
    private lateinit var mInterstitialAd: InterstitialAd
    lateinit var profileimage:ImageView
    lateinit var username:EditText
    lateinit var cityname:EditText
    lateinit var aboutMe:EditText
    lateinit var addbutton:ImageButton
    lateinit var saveProfileImage:Button
    var GALLERY_IMAGE =0
      var ref = FirebaseDatabase.getInstance().reference

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_profile)
        val h = supportActionBar
        h!!.title = "My Profile"
        profileimage=findViewById(R.id.imageViewProfile)
        aboutMe=findViewById(R.id.aboutId)
        addbutton=findViewById(R.id.addprofilepic)
        saveProfileImage=findViewById(R.id.Saveprofileimage)
        username = findViewById(R.id.usernameId)
        cityname =findViewById(R.id.cityId)
        addbutton.setOnClickListener {
            val z =Intent(Intent.ACTION_PICK)
           z.type=("image/*")
            startActivityForResult(z,GALLERY_IMAGE)
            if (mInterstitialAd.isLoaded) {
                mInterstitialAd.show()
            } else {
                Log.d("TAG", "The interstitial wasn't loaded yet.")
            }
        }


        // Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713
        MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511712")
        mInterstitialAd = InterstitialAd(this)
        mInterstitialAd.adUnitId = "ca-app-pub-3940256099942544/1033173712"
        mInterstitialAd.loadAd(AdRequest.Builder().build())
//        mInterstitialAd.show()

    }

mInterstitialAd.show()在添加oncreate时不起作用,但仅适用于setonclicklistner .. 可以任意解释我为什么?

2 个答案:

答案 0 :(得分:0)

把这一行

MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511712")

之后

setContentView(R.layout.activity_profile)

答案 1 :(得分:0)

您的广告只展示一次,因为您只是第一次设置而不重新加载广告。

您需要添加adListener才能重新加载广告:

class MainActivity : Activity() {

    private lateinit var mInterstitialAd: InterstitialAd

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        MobileAds.initialize(this,
            "ca-app-pub-3940256099942544~3347511713")

        mInterstitialAd = InterstitialAd(this)
        mInterstitialAd.adUnitId = "ca-app-pub-3940256099942544/1033173712"
        mInterstitialAd.loadAd(AdRequest.Builder().build())

        mInterstitialAd.adListener = object : AdListener() {
              override fun onAdClosed() {
                     mInterstitialAd.loadAd(AdRequest.Builder().build())
              }
        }
    }
}

如果它仍然不起作用,可能是您的插页式广告尚未准备就绪。你可以通过这样做来检查它是否准备就绪:

mMyButton.setOnClickListener {
    if (mInterstitialAd.isLoaded) {
        mInterstitialAd.show()
    } else {
        Log.d("TAG", "The interstitial wasn't loaded yet.")
    }
}

希望它有所帮助!