Android/Android 일반

애드몹(Admob) 보상형 광고 예제

lipnus 2019. 4. 19. 13:27
반응형

애드몹(Admob) 보상형 광고예제

Document: https://developers.google.com/admob/android/quick-start?hl=en

Admob: https://apps.admob.com

Github: https://github.com/lipnus/admob_reward_ad_sample





Gradle

implementation 'com.google.android.gms:play-services-ads:17.2.0'


Manifest

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"

android:value="@string/admob_app_id"/>



<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>



<resources>

<string name="admob_app_id">ca-app-pub-3940256099942544~3347511713</string>

<string name="admob_reward_id">ca-app-pub-3940256099942544/5224354917</string>

</resources>

이 예제는 문서에 적혀있는 테스트아이디를 사용

 


MainActivity.kt

package com.empo.android.rewardadtest

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
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
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity(), RewardedVideoAdListener {


private lateinit var mRewardedVideoAd: RewardedVideoAd

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

MobileAds.initialize(this, getString(R.string.admob_app_id))


// Use an activity context to get the rewarded video instance.
mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this)
mRewardedVideoAd.rewardedVideoAdListener = this

loadRewardedVideoAd()
initLayout()
}

fun initLayout(){
button.setOnClickListener {

if (mRewardedVideoAd.isLoaded) {
mRewardedVideoAd.show()
}
}

button2.setOnClickListener {
loadRewardedVideoAd()
}
}




private fun loadRewardedVideoAd() {
Toast.makeText(this, "loadRewardVideoAd()", Toast.LENGTH_LONG).show()
mRewardedVideoAd.loadAd( getString(R.string.admob_reward_id), AdRequest.Builder().build())
}



override fun onRewardedVideoAdClosed() {
Log.d("SSS", "onRewardVideoAdClosed()")
}

override fun onRewardedVideoAdLeftApplication() {
Log.d("SSS", "onRewardedVideoAdLeftApplication()")

}

override fun onRewardedVideoAdLoaded() {
Log.d("SSS", "onRewardedVideoAdLoaded()")
}

override fun onRewardedVideoAdOpened() {
Log.d("SSS", "onRewardedVideoAdOpened()")
}

override fun onRewardedVideoCompleted() {
Log.d("SSS", "onRewardedVideoCompleted()")
}

override fun onRewarded(rItem: RewardItem?) {
Log.d("SSS", "onRewarded( type:${rItem?.type}, type:${rItem?.amount})")
Toast.makeText(this, "보상지급 type:${rItem?.type}, type:${rItem?.amount}", Toast.LENGTH_LONG).show()
}

override fun onRewardedVideoStarted() {
Log.d("SSS", "onRewardedVideoStarted()")
}

override fun onRewardedVideoAdFailedToLoad(p0: Int) {
Log.d("SSS", "onRewardedVideoAdFailedToLoad($p0)")
}

}


class MainActivity : AppCompatActivity(), RewardedVideoAdListener { ...

인터페이스를 이용해 리스너 달아준다



button.setOnClickListener {
if (mRewardedVideoAd.isLoaded) {
mRewardedVideoAd.show()
}
}

광고실행, 결과는 콜백으로 받는다.



리워드 광고 진행 시의 생명주기

2019-04-19 13:28:39.326 27684-27684/com.empo.android.rewardadtest D/SSS: onRewardedVideoAdLoaded()

2019-04-19 13:28:42.603 27684-27684/com.empo.android.rewardadtest D/SSS: onRewardedVideoAdOpened()

2019-04-19 13:28:43.017 27684-27684/com.empo.android.rewardadtest D/SSS: onRewardedVideoStarted()

2019-04-19 13:28:47.861 27684-27684/com.empo.android.rewardadtest D/SSS: onRewardedVideoCompleted()

2019-04-19 13:28:47.863 27684-27684/com.empo.android.rewardadtest D/SSS: onRewarded(com.google.android.gms.internal.ads.zzatf@369fece)

2019-04-19 13:28:58.090 27684-27684/com.empo.android.rewardadtest D/SSS: onRewardVideoAdClosed()


onReward()는 광고시청 시 일정 시간 시청하면 발동된다.(닫는 시점이 아님) 

광고를 종료하지 않아도 5초정도 지나면 중간에 자동으로 발현.


리워드 광고 다시 로드

button2.setOnClickListener {
loadRewardedVideoAd()
}

한번 시청한 후에 또 보기 위해서는 다시 로드 해주어야 한다.

반응형

'Android > Android 일반' 카테고리의 다른 글

Bottom Navigation과 ViewPager 연결하기  (0) 2019.04.23
타이틀바(Action bar) 없애기  (0) 2019.04.19
dp px 변환 함수  (0) 2019.04.08
[Anotation] @RequiresApi, @TargetApi  (0) 2019.04.03
플레이스토어 연결하기  (0) 2019.04.01