coco2d安卓游戏的插页式广告

时间:2017-05-26 20:25:07

标签: eclipse android-studio admob cocos2d-x interstitial

我在展示插页式广告时面临很多困难,这是我第一次使用Cocos2D游戏。 这是所有主要的java代码

public class FlyingPanda extends Activity implements AdListener {
    /** Called when the activity is first created. */
    public static CCGLSurfaceView mGLSurfaceView;
    private boolean isCreated = false; 

    private static final String ADMOB_PUBLISH_ID = "xxxxxxxxxxxxxxxxxx";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        if( !isCreated ){
            isCreated = true;
        } else {
            return;
        }

        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

        RelativeLayout layout = new RelativeLayout(this);
        layout.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        mGLSurfaceView = new CCGLSurfaceView(this);

        // Create the adView
        AdView adView = new AdView(this, AdSize.BANNER, ADMOB_PUBLISH_ID);

        // Initiate a generic request to load it with an ad
        adView.loadAd(new AdRequest());
        adView.setAdListener(this);

        // Add the adView to it
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
        params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
        adView.setLayoutParams(params);

        layout.addView(mGLSurfaceView);
        layout.addView(adView);

        setContentView(layout);

        Common.game_initialize();
        getScaledCoordinate();
        CCDirector.sharedDirector().attachInView(mGLSurfaceView); 

        // attach the OpenGL view to a window
        Common.sound_engine = SoundEngine.sharedEngine();
        loadSound();

我应该在admob设置中添加什么?我会像横幅一样定义一个名为interstitial的变量或者什么?

////////////////////////////////////////////////////////////////////////////////
// Admob Setting
////////////////////////////////////////////////////////////////////////////////

如何在所有公共课程中加载插页式广告?

@Override
public void onDismissScreen(Ad ad) {
    // TODO Auto-generated method stub

}

@Override
public void onFailedToReceiveAd(Ad ad, ErrorCode errorCode) {
    // TODO Auto-generated method stub

}

@Override
public void onLeaveApplication(Ad ad) {
    // TODO Auto-generated method stub

}

@Override
public void onPresentScreen(Ad ad) {
    // TODO Auto-generated method stub

}

@Override
public void onReceiveAd(Ad ad) {

1 个答案:

答案 0 :(得分:1)

MainActivity.java:

public class AppActivity extends Cocos2dxActivity {
    private static final String ADMOB_INTERSTITIAL = "ca-app-pub-2575683230512628/5833084223";
    private InterstitialAd admobInterstitial;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        _appActivity = this;

        admobInterstitial = new InterstitialAd(this);
        admobInterstitial.setAdUnitId(ADMOB_INTERSTITIAL);
        admobInterstitial.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                admobInterstitial.loadAd(newAdRequest());
            }

        });
        admobInterstitial.loadAd(newAdRequest());
    }

    public static void showInterstitial(final String adSdk){
        Log.d("ADMOB INTERSTITIAL", "showInterstitial: " + adSdk);
        _appActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if(adSdk.equalsIgnoreCase("admob")){
                    if(_appActivity.admobInterstitial != null){
                        if(_appActivity.admobInterstitial.isLoaded()){
                            _appActivity.admobInterstitial.show();
                        }
                    }
                }
                else if(adSdk.equalsIgnoreCase("chartboost")){
                }
                else if(adSdk.equalsIgnoreCase("revmob")){
                }
                else{
                    Log.w("INTERSTITIAL", "unknown ad to show: " + adSdk);
                }
            }
        });
    }

    private AdRequest newAdRequest(){
        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .addTestDevice("YOUR_DEVICE_ID") 
                .build();       

        return adRequest;
    }

}

c ++代码(.c文件):

const char* AppActivityClassName = "org/cocos2dx/cpp/AppActivity";

bool NativeHelper::instanceFlag = false;
NativeHelper* NativeHelper::instance = NULL;

NativeHelper* NativeHelper::getInstance(){
    if(!instanceFlag){
        instance = new NativeHelper();
        instanceFlag = true;
    }

    return instance;
}

void NativeHelper::showInterstitial(string adSdk){
    cocos2d::JniMethodInfo t;
    if (cocos2d::JniHelper::getStaticMethodInfo(t, AppActivityClassName, "showInterstitial", "(Ljava/lang/String;)V")){

        jstring stringArg = t.env->NewStringUTF(adSdk.c_str());
        t.env->CallStaticVoidMethod(t.classID, t.methodID, stringArg);

        t.env->DeleteLocalRef(t.classID);
        t.env->DeleteLocalRef(stringArg);
    }
}

c ++代码(.h文件):

class NativeHelper
{
public:
    static void showInterstitial(string adSdk);
private:    
    static bool instanceFlag;
    static NativeHelper* instance;
}

您也可以使用SDKBOX,但这是更灵活的解决方案,您始终拥有最新的sdk。