본문 바로가기
Etc

adMob - interstitial, libgdx

by ses jeon 2019. 11. 30.

Second, let's connect an interstitial.
The initial setting is the same as the banner ad in the previous post.

2. Interstitial
Unlike banners, interstitials will display their screens according to the Google advertise sdk design at any timing, regardless of the current game screen. In other words, we will create a class separately for all elements because it is separate from the current user's view. (neatly)

두번째로 전면광고를 연결해보겠습니다.

초기 설정은 이전 포스트의 배너 광고와 동일합니다.

 

2. 전면 광고

배너와는 다르게 전면광고는 어떤 타이밍에 현재의 게임 화면과는 관계없이 구글 advertise sdk의 설계대로 따로 화면을 띄우게 됩니다. 즉 현재 사용자의 view와는 분리가 되기 때문에 모든 요소를 따로 클래스를 만들겠습니다. (깔끔하게)

 

1
2
3
4
public interface InterstitialService {
    boolean isInterstitialLoaded();
    void showInterstitial();
}
cs

First add an interface to the core.

Implement this interface on android side.

먼저 core에 interface를 추가합니다.

 

이 인터페이스를 android쪽에서 구현합니다.

 

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
public class IntersitialAd implements InterstitialService {
    Activity activity;
    private InterstitialAd interstitialAd;
 
    public IntersitialAd(Activity activity) {
        this.activity = activity;
        interstitialAd = new InterstitialAd(this.activity);
        interstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712"); //google intersititial test id
        interstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {}
 
            @Override
            public void onAdClosed() {
                loadIntersitialAd();
            }
        });
        loadIntersitialAd();
    }
 
    private void loadIntersitialAd(){
        AdRequest interstitialRequest = new AdRequest.Builder().build();
        interstitialAd.loadAd(interstitialRequest);
    }
 
    @Override
    public boolean isInterstitialLoaded() {
        return interstitialAd.isLoaded();
    }
 
    @Override
    public void showInterstitial() {
 
    }
}
cs

Initialize and load a new interstitial with loadIntersitialAd (). And onAdClosed, the code that loads a new interstitial when the interstitial finishes and closes.

Here is what the ad shows.

초기화를 시키고, loadIntersitialAd()로 새로운 전면광고를 로드합니다. 그리고 onAdClosed, 즉 전면광고가 끝나고 닫을때 새로운 전면광고를 로드하는 코드입니다.

 

다음은 광고를 보여주는 부분입니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
    @Override
    public void showInterstitial() {
        activity.runOnUiThread(new Runnable() {
            public void run() {
                if (interstitialAd.isLoaded()) {
                    interstitialAd.show();
                }else {
                    loadIntersitialAd();
                }
            }
        });
    }
cs

If the interstitial is loaded, run show (), or else load it again. This part runs in a new thread.

Add this part to MyGdxGame.

만약 전면광고가 로드되어있으면 show(), 즉 실행시키고, 아니면 다시 load를 시킵니다. 이부분은 새로운 thread에서 실행시킵니다.

 

이부분을 MyGdxGame에 추가시킵니다.

It's done Now let's decide when to run the ad.

When the loading screen is in progress, we will stop the 50% progress and run the interstitial.

We will modify the loading data of the previous example to get 10 data.
(If the data is too small, it may not run.)

다 되었습니다. 이제 광고를 실행시킬 타이밍을 정해보겠습니다.

 

로딩화면이 진행될때 50%가 진행될때 멈추고 전면광고를 실행시키도록 하겠습니다.

 

이전 예제의 로딩 데이터를 좀 수정해서, 데이터들을 10개정도 받도록 수정하겠습니다.

(데이터가 너무 작을 경우 실행이 안될수 있습니다)

We've added a flag to only run once if it's 50% or higher.

50% 이상이면 실행시키는데 한번만 실행시키게 flag를 추가시켰습니다.

 

 

댓글