読者です 読者をやめる 読者になる 読者になる

cocos2dx 広告(Admob)を出したり消したりする

最初は表示を行い、課金した時などに非表示になる仕組みを作る

パッケージクラス名、広告Unitはテスト用なので変更してください。

 

Android MainActivity.java

package com.test.SampleGame;

import org.cocos2dx.lib.Cocos2dxActivity;

import android.graphics.Color;
import android.graphics.Point;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.google.android.gms.ads.*;

public class MainActivity extends Cocos2dxActivity {
    private static final String AD_UNIT_ID = "ca-app-pub-****************/**********";//広告ユニットID
    private static AdView adView = null;
    private static Handler mHandler;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mHandler = new Handler();
        Point p = new Point(0, 0);
        getWindowManager().getDefaultDisplay().getSize(p);
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(p.x, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = (Gravity.BOTTOM | Gravity.CENTER);

        adView = new AdView(this);
        adView.setVisibility(View.GONE);
        addContentView(adView, layoutParams);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (adView != null) {
            adView.resume();
        }
    }

    @Override
    protected void onPause() {
        if (adView != null) {
            adView.pause();
        }
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        if (adView != null) {
            adView.destroy();
        }
        super.onDestroy();
    }

    public static void showAdMob() {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if (adView != null) {
                    adView.setVisibility(View.VISIBLE);
                    if(adView.getAdUnitId()!=AD_UNIT_ID) {
                        adView.setAdUnitId(AD_UNIT_ID);
                        adView.setAdSize(AdSize.BANNER);
                    }
                    adView.setBackgroundColor(Color.BLACK);
                    AdRequest adRequest = new AdRequest.Builder()
                            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                            .addTestDevice("Test Device")
                            .build();
                    adView.loadAd(adRequest);
                }
            }
        });
    }
    public static void removeAdMob(){
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if (adView != null) {
                    adView.setVisibility(View.GONE);
                    adView.destroy();
                }
            }
        });
    }
}

 

AppDelegate.hにstaticメソッドを定義

#ifndef  _APP_DELEGATE_H_
#define  _APP_DELEGATE_H_

#include "cocos2d.h"

class  AppDelegate : private cocos2d::Application
{
public:
    AppDelegate();
    virtual ~AppDelegate();

    virtual void initGLContextAttrs();

    virtual bool applicationDidFinishLaunching();

    virtual void applicationDidEnterBackground();

    virtual void applicationWillEnterForeground();

    static void showAdMob();

    static void removeAdMob();
};

#endif // _APP_DELEGATE_H_

 

AppDelegate.cpp下記を追加

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include "platform/android/jni/JniHelper.h"
#endif

 

void AppDelegate::showAdMob()
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    cocos2d::JniMethodInfo methodInfo;
    if (!cocos2d::JniHelper::getStaticMethodInfo(methodInfo, "com/test/SampleGame/MainActivity", "showAdMob", "()V"))
    {
        return;
    }
    methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID);
    methodInfo.env->DeleteLocalRef(methodInfo.classID);
#endif
}
void AppDelegate::removeAdMob()
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    cocos2d::JniMethodInfo methodInfo;
    if (!cocos2d::JniHelper::getStaticMethodInfo(methodInfo, "com/test/SampleGame/MainActivity", "removeAdMob", "()V"))
    {
        return;
    }
    methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID);
    methodInfo.env->DeleteLocalRef(methodInfo.classID);
#endif
}