본문 바로가기
안드로이드 웹앱 콘테츠 개발자 양성(국비지원)/광고 달기

Android Studio 모바일 광고 플랫폼 2 (광고 달기)

by 차누감 2019. 9. 23.

Android Studio 모바일 광고 플랫폼 1 (광고 달기)에

버튼을 2개 추가하여 전면, 동영상 광고 달기

<전면 광고 버튼 실행 화면>

<보상형 동영상 광고 버튼 실행 화면>

 

--------------------------------------------------------------------------------------------------------------------

만약 그냥 전면 광고로 바로 달려고 해도 이 작업은 필수이다.

 

dependencies는 안드로이드 스튜디오에서 제공하지 않는 것들, 새로 추가한 것을 써준다.

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

<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-" />

 

--------------------------------------------------------------------------------------------------------------------

 

activity_main.xml 코드 추가 (버튼 2개) 

 

MainActivity.java 코드 추가 (버튼을 눌렀을 때 액션 내용, 새로운 화면을 띄운다.)

그리고 새로운 창을 띄우는 것이기 때문에 class 두개를 (Interstial  <-전면 광고, rewarded <- 보상형 광고)추가한다.

그리고 xml도 자동으로 생성 되도록 아래 사진처럼 마우스 오른쪽 메뉴를 이용한다.

코드 작성 전에 전면 광고 단위를 먼저 생성해주겠다.

이제 새로 추가한 Interstial.java와 Interstial.xml 코드를 작성해보자.

activity_interstial.xml 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Interstial">
 
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="전면광고 보이기"
        android:onClick="clickbtn"
        android:layout_centerInParent="true"
        />
</RelativeLayout>
 
 

 

화면 구성

Interstial.java 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.tistory.firewallgogo.ex37admobtest;
 
 
 
 
public class Interstial extends AppCompatActivity {
 
    private InterstitialAd interstitialAd;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_interstial);
 
        interstitialAd = new InterstitialAd(this); //여기는 전면단위
        interstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712"); //샘플코드로 만든 후
 
        //광고요청자 객체 생성
        AdRequest adRequest = new AdRequest.Builder().build();
 
        //광고로드에 대한 리너스를 추가
        interstitialAd.setAdListener(new AdListener(){
            @Override
            public void onAdLoaded() {
                super.onAdLoaded();
 
                interstitialAd.show();
            }
 
            @Override
            public void onAdFailedToLoad(int i) { //로드 실패했을 경우
                super.onAdFailedToLoad(i);
                Toast.makeText(Interstial.this,"로드실패",Toast.LENGTH_SHORT).show();
            }
        });
 
    }
 
    public void clickbtn(View view) {
        AdRequest adRequest = new AdRequest.Builder().build();
    }
//
//        if(interstitialAd.isLoaded()){
//            interstitialAd.show();
//        }else{
//            Toast.makeText(this,"아직 광고가 load되지 않았다.", Toast.LENGTH_SHORT).show();
//        }
//    }
 
}
 
 
 

<전면 광고 버튼 실행 화면>

이제 보상형 광고 버튼을 만들어 보자.

activity_rewarded.xml 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".rewarded">
 
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="보상형 광고 동영상 보이기"
        android:onClick="clickbtn"
        android:layout_centerInParent="true"
        />
</RelativeLayout>
 
 

화면 구성

rewarded.java 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.tistory.firewallgogo.ex37admobtest;
 
import androidx.annotation.NonNull;
 
 
 
public class rewarded extends AppCompatActivity {
 
    private RewardedAd rewardedAd;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rewarded);
 
        rewardedAd = new RewardedAd(this"ca-app-pub-3940256099942544/5224354917");
        
        //광고 로드 및 콜백리스터 추가
        rewardedAd.loadAd(new AdRequest.Builder().build(), adLoadCallback);
 
    }//oncreate method..
 
        //보상형 광고 로드
    RewardedAdLoadCallback adLoadCallback = new RewardedAdLoadCallback(){
        @Override
        public void onRewardedAdLoaded() {
            super.onRewardedAdLoaded();
 
            rewardedAd.show(rewarded.this, rewardedAdCallback);
        }
 
        @Override
        public void onRewardedAdFailedToLoad(int i) {
            super.onRewardedAdFailedToLoad(i);
            Toast.makeText(rewarded.this"로드 실패",Toast.LENGTH_SHORT).show();
        }
    };
    
        //보상형 광고 리스너 생성하기
    RewardedAdCallback rewardedAdCallback = new RewardedAdCallback() {
            @Override
            public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
                //사용자가 기존시간 이상 동영상을 시청하여 보상을 받을 때 자동 실행
                //파라미터로 전달된 rewardItem객체가 보상값을 가지고 있음.
 
                //보상타입
                String type = rewardItem.getType();
 
                //보상수량
                int num = rewardItem.getAmount();
                Toast.makeText(rewarded.this,type+" : "+num, Toast.LENGTH_SHORT).show();
            }
        };
 
    public void clickbtn(View view) {
        rewardedAd.show(rewarded.this, rewardedAdCallback);
    }
}//ACTIVITY
 
 
 

<보상형 동영상 광고 버튼 실행 화면>

-----------------------------------------------------------------------------------------------------------------------------------

MainActivity.java와 activity_main.xml 코드도 복사가 되도록 다시 올린다.

activity_main.xml 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
 
    android:id="@+id/adview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    ads:adSize="BANNER"
    ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
    >
    <Button
        android:id="@+id/btn01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="전면광고"
        android:onClick="clickbtn"
        />
    <Button
        android:id="@+id/btn02"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="보상형 동영상 광고"
        android:onClick="clickbtn2"
        android:layout_below="@+id/btn01"
        />
</RelativeLayout>
 
 

MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.tistory.firewallgogo.ex37admobtest;
 
 
 
 
public class MainActivity extends AppCompatActivity {
 
    AdView adView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        MobileAds.initialize(thisnew OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
                Toast.makeText(MainActivity.this,"광고 초기화 완료",Toast.LENGTH_SHORT).show();
            }
        });
 
        adView = findViewById(R.id.adview);
 
        //광고요청자 객체 생성 : 광고를 ADMOB 서버로부터 읽어오는 객체
        AdRequest adRequest = new AdRequest.Builder().build();
    }
 
    public void clickbtn(View view) {
        //전면광고 보여주는 activity로 이동
        Intent intent = new Intent(this, Interstial.class);
        startActivity(intent);
    }
 
    public void clickbtn2(View view) {
        //동영상 광고 보여주는 activity로 이동.
        Intent intent = new Intent(this, rewarded.class);
        startActivity(intent);
    }
}
 
 
 

댓글