Skip to content

Instantly share code, notes, and snippets.

@tklee1975
Last active August 29, 2015 18:50
Show Gist options
  • Save tklee1975/cc2c7427d8dc1d0a6e07 to your computer and use it in GitHub Desktop.
Save tklee1975/cc2c7427d8dc1d0a6e07 to your computer and use it in GitHub Desktop.
Cocos2d-x Admob Android Code
package org.cocos2dx.cpp;
import android.app.Activity;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
public class AdHelper {
public static final String AD_UNIT_ID = "<your ad unit id>";
public static AdView createAdView(Activity activity) {
if(activity == null) {
return null;
}
AdView adView = new AdView(activity);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
adView.setVisibility(View.GONE);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("HASH_DEVICE_ID")
.build();
adView.loadAd(adRequest);
adView.setBackgroundColor(Color.BLACK);
LinearLayout.LayoutParams adParams = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
//adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
activity.addContentView(adView, adParams);
return adView;
}
public static void showAdView(AdView view) {
if(view == null) {
return;
}
if(view.isEnabled() == false) {
view.setEnabled(true);
}
if(view.getVisibility() != View.VISIBLE) {
view.setVisibility(View.VISIBLE);
}
}
public static void hideAdView(AdView view) {
if(view == null) {
return;
}
// Make the Ad disable
if(view.isEnabled()) {
view.setEnabled(false);
}
if(view.getVisibility() == View.VISIBLE) {
view.setVisibility(View.GONE);
}
}
}
//
// AndroidHelper.cpp
// SDKTest
//
// Created by Ken Lee on 17/8/15.
//
//
#include "AndroidHelper.h"
#include "cocos2d.h"
USING_NS_CC;
#pragma mark - Android
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include "platform/android/jni/JniHelper.h"
#include <jni.h>
void AndroidHelper::callVoidStaticMethod(const char *className, const char *method)
{
cocos2d::JniMethodInfo t;
if (cocos2d::JniHelper::getStaticMethodInfo(t, className, method, "()V"))
{
t.env->CallStaticVoidMethod(t.classID, t.methodID);
t.env->DeleteLocalRef(t.classID);
}
}
#else
void AndroidHelper::callVoidMethod()
{
log("AndroidHelper::callVoidMethod: not supporting");
}
#endif
//
// AndroidHelper.h
// SDKTest
//
// Created by Ken Lee on 17/8/15.
//
//
#ifndef __SDKTest__AndroidHelper__
#define __SDKTest__AndroidHelper__
#include <stdio.h>
#include <string>
class AndroidHelper
{
public:
static void callVoidStaticMethod(const char *className, const char *method);
};
#endif /* defined(__SDKTest__AndroidHelper__) */
/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011 Zynga Inc.
Copyright (c) 2013-2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
package org.cocos2dx.cpp;
import org.cocos2dx.lib.Cocos2dxActivity;
import android.content.Context;
import android.os.Bundle;
import com.google.android.gms.ads.AdView;
public class AppActivity extends Cocos2dxActivity {
private static AppActivity _appActivity;
private AdView mAdView = null;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
mAdView = AdHelper.createAdView(this);
hideAd();
_appActivity = this;
}
private AdView getAdView() {
return mAdView;
}
public static void hideAd() {
if(_appActivity == null) {
return;
}
_appActivity.runOnUiThread(new Runnable() {
@Override
public void run()
{
AdHelper.hideAdView(_appActivity.getAdView());
}
});
}
public static void showAd() {
if(_appActivity == null) {
return;
}
_appActivity.runOnUiThread(new Runnable() {
@Override
public void run()
{
AdHelper.showAdView(_appActivity.getAdView());
}
});
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
public static Context getAppContext() {
return _appActivity;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment