안드로이드 스튜디오 구글 애드몹 보상형 광고 삽입하기

Android Studio Google AdMob Rewarded Ads

 

광고자세히알아보기

 

 

보상형 광고  |  Android  |  Google Developers

보상형 광고는 상호작용하는 사용자에게 인앱 보상을 제공하는 광고입니다. 이 가이드에는 AdMob의 보상형 광고를 Android 앱에 통합하는 방법이 나와 있습니다. 고객 성공사례인 우수사례 1과 우��

developers.google.com

 

보상형 광고는 상호작용하는 사용자에게 인앱 보상을 제공하는 광고입니다. 

즉 광고를 시청하고 그에 따른 보상을 사용자에게 제공하는 시스템입니다.

 

 

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);
        }
    }

}

 

수고하셨습니다 ★

더 많은 정보

 https://deumdroid.tistory.com/ 

 

댓글

Designed by JB FACTORY