본문 바로가기
Etc

adMob - reward, libgdx

by ses jeon 2019. 11. 30.

First, modify mainPage to get the timing.

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
        stage = new Stage(new StretchViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()));
        VerticalGroup verticalGroup = new VerticalGroup();
        verticalGroup.setFillParent(true);
        stage.addActor(verticalGroup);
 
        FileHandle skinFile = Gdx.files.internal("skins/glassy/skin/glassy-ui.json");
        skin = new Skin(skinFile);
 
        button = new Button(skin);
        Container container1 = new Container();
        container1.setActor(button);
       container1.padTop(500f);
        verticalGroup.addActor(container1);
 
        button.addListener(new ClickListener(){
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                System.out.println("click");
                return super.touchDown(event, x, y, pointer, button);
            }
        });
...
        InputMultiplexer multiplexer = new InputMultiplexer();
        multiplexer.addProcessor(stage);
        multiplexer.addProcessor(camController);
        Gdx.input.setInputProcessor(multiplexer);
...
 
 
    @Override
    public void render(float delta) {
...
        stage.act(delta);
        stage.draw();
 
    }
cs

To add a button, use skin. See the libgdx section of this blog for more information.
Then add a button listener and add an InputMultiplexer to accept it.
Multiplexers are prioritized in the order of input devices you add. In the case of the above source, the stage will be accepted first, and then the camControl will be received.

3. Implement reward (legacy)
In the case of reward advertisement, the callback processing part in the core side is necessary in the user side and libgdx. For example, what happens after the ad is done?

We need a reward listener, so we need two interfaces.

1
2
3
4
5
6
public interface RewardService {
    boolean hasVideoLoaded();
    void loadRewardedVideoAd();
    void showRewardedVideoAd();
    void setVideoEventListener(RewardServiceListener listener);
}
cs
1
2
3
4
5
6
public interface RewardServiceListener {
    void onRewardedEvent(String type, int amount);
    void onRewardedVideoAdLoadedEvent();
    void onRewardedVideoAdClosedEvent();
}
 
cs

 

The Android side now implements a class that implements it.

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
public class RewardAd implements RewardService {
    Activity activity;
    RewardedVideoAd adRewardedVideoView;
    private boolean is_video_ad_loaded;
    public RewardAd(Activity activity) {
        this.activity = activity;
        adRewardedVideoView = MobileAds.getRewardedVideoAdInstance(this.activity);
//        adRewardedVideoView.setRewardedVideoAdListener(this);
        loadRewardedVideoAd();
    }
 
    @Override
    public boolean hasVideoLoaded() {
        if(is_video_ad_loaded) {
            return true;
        }
        activity.runOnUiThread(new Runnable() {
            public void run() {
                if (!adRewardedVideoView.isLoaded()) {
                    loadRewardedVideoAd();
                }
            }
        });
        return false;
    }
 
    @Override
    public void loadRewardedVideoAd() {
        adRewardedVideoView.loadAd("ca-app-pub-3940256099942544/5224354917",
                new AdRequest.Builder().build()); //google reward test id
    }
 
    @Override
    public void showRewardedVideoAd() {
        activity.runOnUiThread(new Runnable() {
            public void run() {
                if (adRewardedVideoView.isLoaded()) {
                    adRewardedVideoView.show();
                } else {
                    loadRewardedVideoAd();
                }
            }
        });
    }
 
    @Override
    public void setVideoEventListener(RewardServiceListener listener) {
 
    }
}
cs

If you implement RewardService first, you can see that it is similar to Interstitial. You can load the ad first, then show () when it's done loading, and once it shows () it will load it again.

The difference with Interstitial is in the listener. Reward ads must return call back to the user after the ad ends.
So you need to run setRewardedVideoAdListener on line 8. Try to add

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
public class RewardAd implements RewardService, RewardedVideoAdListener {
    Activity activity;
    RewardedVideoAd adRewardedVideoView;
    private RewardServiceListener adRewardedVideoListener;
    private boolean is_video_ad_loaded;
    public RewardAd(Activity activity) {
        this.activity = activity;
        adRewardedVideoView = MobileAds.getRewardedVideoAdInstance(this.activity);
        adRewardedVideoView.setRewardedVideoAdListener(this);
        loadRewardedVideoAd();
    }
 
    @Override
    public boolean hasVideoLoaded() {
        if(is_video_ad_loaded) {
            return true;
        }
        activity.runOnUiThread(new Runnable() {
            public void run() {
                if (!adRewardedVideoView.isLoaded()) {
                    loadRewardedVideoAd();
                }
            }
        });
        return false;
    }
 
    @Override
    public void loadRewardedVideoAd() {
        adRewardedVideoView.loadAd("ca-app-pub-3940256099942544/5224354917",
                new AdRequest.Builder().build()); //google reward test id
    }
 
    @Override
    public void showRewardedVideoAd() {
        activity.runOnUiThread(new Runnable() {
            public void run() {
                if (adRewardedVideoView.isLoaded()) {
                    adRewardedVideoView.show();
                } else {
                    loadRewardedVideoAd();
                }
            }
        });
    }
 
    @Override
    public void setVideoEventListener(RewardServiceListener listener) {
        adRewardedVideoListener = listener;
    }
 
    /*** RewardedVideoAdListener ***/
    @Override
    public void onRewardedVideoAdLoaded() {
        if(adRewardedVideoListener != null) {
            adRewardedVideoListener.onRewardedVideoAdLoadedEvent();
        }
        is_video_ad_loaded = true;
    }
 
    @Override
    public void onRewardedVideoAdOpened() {
 
    }
 
    @Override
    public void onRewardedVideoStarted() {
 
    }
 
    @Override
    public void onRewardedVideoAdClosed() {
        is_video_ad_loaded = false;
        loadRewardedVideoAd();
        if(adRewardedVideoListener != null) {
            adRewardedVideoListener.onRewardedVideoAdClosedEvent();
        }
    }
 
    @Override
    public void onRewarded(RewardItem rewardItem) {
        if(adRewardedVideoListener != null) {
            adRewardedVideoListener.onRewardedEvent(rewardItem.getType(), rewardItem.getAmount());
        }
    }
 
    @Override
    public void onRewardedVideoAdLeftApplication() {
 
    }
 
    @Override
    public void onRewardedVideoAdFailedToLoad(int i) {
 
    }
 
    @Override
    public void onRewardedVideoCompleted() {
 
    }
    /*** RewardedVideoAdListener ***/
}
cs

We've set up a listener called adRewardedVideoListener that the user can receive, and added the is_video_ad_loaded variable to the callback that can receive whether the video is loaded or closed.

Add a class to androidLauncher.

 

Now let's use the rewardService we received.

Initially we modified mainPage. From there, connect the rewardService to the button event.

 

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
        RewardServiceListener rewardServiceListener = new RewardServiceListener() {
            @Override
            public void onRewardedEvent(String type, int amount) {
                System.out.println(type + ": " + amount);
            }
 
            @Override
            public void onRewardedVideoAdLoadedEvent() {
 
            }
 
            @Override
            public void onRewardedVideoAdClosedEvent() {
 
            }
        };
        GLOBAL.rewardService.setVideoEventListener(rewardServiceListener);
 
        button.addListener(new ClickListener(){
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                if (GLOBAL.rewardService.hasVideoLoaded()) {
                    GLOBAL.rewardService.showRewardedVideoAd();
                }
                return super.touchDown(event, x, y, pointer, button);
            }
        });
cs

showRwardedVideoAd () is executed when button is clicked. And the important part is to set up a new rewardServiceListener. This part may be different for each part of the execution. For now it prints type and amount. The type and amount can be set individually by the user when creating a reward ad.

 

 

댓글