안드로이드 스튜디오 구글 애드몹 보상형 광고 삽입하기
- Android Studio
- 2020. 5. 27.
Android Studio Google AdMob Rewarded Ads
보상형 광고는 상호작용하는 사용자에게 인앱 보상을 제공하는 광고입니다.
즉 광고를 시청하고 그에 따른 보상을 사용자에게 제공하는 시스템입니다.
Google 모바일 광고 SDK 추가하기
build.gradle(app)에 밑에있는 구글 SDK를 추가합니다. 광고를 사용할 수 있게 해 줍니다.
dependencies {
implementation 'com.google.android.gms:play-services-ads:19.1.0'
}
AndroidManifest.xml
<application>
<!-- Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 -->
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713"/>
</application>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="보상형 광고 삽입"
android:textSize="30dp"
android:textColor="#000"/>
<Button
android:layout_gravity="center"
android:id="@+id/Ad_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="광고시작"/>
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
boolean isLoading;
private static final String AD_UNIT_ID = "ca-app-pub-3940256099942544/5224354917";
private RewardedAd rewardedAd;
private String TAG = "Google";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
loadRewardedAd();
startrewardAD();
findViewById(R.id.Ad_Button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showRewardedVideo();
}
});
}
private void loadRewardedAd() {
if (rewardedAd == null || !rewardedAd.isLoaded()) {
rewardedAd = new RewardedAd(this, AD_UNIT_ID);
isLoading = true;
rewardedAd.loadAd(
new AdRequest.Builder().build(),
new RewardedAdLoadCallback() {
@Override
public void onRewardedAdLoaded() {
// Ad successfully loaded.
MainActivity.this.isLoading = false;
Toast.makeText(MainActivity.this, "리워드광고성공", Toast.LENGTH_SHORT).show();
Log.i(TAG, "리워드광고성공");
}
@Override
public void onRewardedAdFailedToLoad(int errorCode) {
// Ad failed to load.
MainActivity.this.isLoading = false;
Toast.makeText(MainActivity.this, "리워드광고 실패", Toast.LENGTH_SHORT)
.show();
Log.i(TAG, "리워드광고 실패");
}
});
}
}
private void startrewardAD() {
// Hide the retry button, load the ad, and start the timer.
if (!rewardedAd.isLoaded() && !isLoading) {
loadRewardedAd();
}
}
private void showRewardedVideo() {
if (rewardedAd.isLoaded()) {
RewardedAdCallback adCallback =
new RewardedAdCallback() {
@Override
public void onRewardedAdOpened() {
// Ad opened.
Toast.makeText(MainActivity.this, "onRewardedAdOpened", Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedAdClosed() {
// Ad closed.
Toast.makeText(MainActivity.this, "onRewardedAdClosed", Toast.LENGTH_SHORT).show();
// Preload the next video ad.
MainActivity.this.loadRewardedAd();
}
@Override
public void onUserEarnedReward(RewardItem rewardItem) {
// User earned reward.
Toast.makeText(MainActivity.this, "onUserEarnedReward", Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedAdFailedToShow(int errorCode) {
// Ad failed to display
Toast.makeText(MainActivity.this, "onRewardedAdFailedToShow", Toast.LENGTH_SHORT)
.show();
}
};
rewardedAd.show(this, adCallback);
}
}
}
'Android Studio' 카테고리의 다른 글
안드로이드 스튜디오 버튼 클릭시 ScrollView(스크롤뷰) 상단, 하단 위치 이동하기 (0) | 2020.06.01 |
---|---|
안드로이드 스튜디오 Bottom Navigation 구현하기 (3) | 2020.06.01 |
안드로이드 스튜디오 웹뷰( WebView)설정하기 (0) | 2020.03.13 |
안드로이드 스튜디오 구글애드몹 네이티브 광고(Nativetemplates) 넣기 (1) | 2020.03.13 |
안드로이드 스튜디오 RecylerView and CheckBox , Delete (0) | 2020.03.07 |