BbaDdo :: How to show the admob in cocos2d-x (android)?


크리에이티브 커먼즈 라이선스
Creative Commons License

 

 

ODIN SISA??


 

 

It seems to be taken a lot of memory and the cpu usage with admob.

Usually, I load the admob banner with static member variable.

 

If load it when admob should be and remove it?

This is a good idea, I think. Well, not sure.

 

<cocos2d-x  & Android code>

1.Android : Cocos2dxActivity.java

 

.........................

.........................

........................

 

 //admob=======================================================
 private AdView adView = null;
 private final String MY_AD_UNIT_ID = "ca-app-pub-***************** ;
 private LinearLayout adLayout = null;
 private static Cocos2dxActivity  coAct;
 //=============================================================

 

 

 // ===========================================================
 // Constructors
 // ===========================================================
 
 @Override
 protected void onCreate(final Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       sContext = this;
       this.mHandler = new Cocos2dxHandler(this);

       this.init();

 Cocos2dxHelper.init(this, this);
 
  
 
 // ===========================================================
 // Getter & Setter
 // ===========================================================

 // ===========================================================
 // Methods for/from SuperClass/Interfaces
 // ===========================================================

 

.......................

......................

......................

 

 // ===========================================================
 // Methods
 // ===========================================================
 @SuppressLint("NewApi")
 public void init() {
      setContentView(R.layout.game_demo); 
      ViewGroup framelayout = (ViewGroup)findViewById(R.id.gamePlace); 
         
       // Cocos2dxGLSurfaceView 
       this.mGLSurfaceView = this.onCreateView(); 

 

// ...add to FrameLayout   
framelayout.addView(this.mGLSurfaceView);

 

// Switch to supported OpenGL (ARGB888) mode on emulator
if (isAndroidEmulator()) {
           this.mGLSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
 }

 this.mGLSurfaceView.setCocos2dxRenderer(new Cocos2dxRenderer());

 

       

//=========================================
// admob layout
//=========================================
coAct = this;
adLayout = (LinearLayout)findViewById(R.id.admob);
adLayout.setVisibility(View.INVISIBLE);
//=========================================

 

}

  

public Cocos2dxGLSurfaceView onCreateView() {
     return new Cocos2dxGLSurfaceView(this);
}

....................

....................

....................

  

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

 

 

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

      Cocos2dxHelper.onPause();
      this.mGLSurfaceView.onPause();
 }

 

@Override
protected void onDestroy() { 
     // Destroy the AdView.
     if (adView != null) {
          adView.destroyDrawingCache();
          adView.destroy();
          adView = null;

          RecycleUtils.recursiveRecycle(((AdView)adView));
          System.gc();
     }
    
     adLayout.destroyDrawingCache();
     adLayout = null;
     super.onDestroy();
 } 
  

 

 


 //====================================================
 //admob show or not  <=== > cocos2d-x native jni
 //====================================================
 static void showAD(final boolean b){
    coAct.runOnUiThread(new Runnable(){
       @SuppressLint("NewApi")
       @Override
       public void run() {
           if(b && coAct.adView == null){ //.getVisibility()== View.INVISIBLE){
               coAct.adLayout.setVisibility(View.VISIBLE);
               try{
                      //adView     
                     coAct.adView = new AdView(coAct);
                     coAct.adView.setAdSize(AdSize.MEDIUM_RECTANGLE);
                     coAct.adView.setAdUnitId(coAct.MY_AD_UNIT_ID); 
                     coAct.adView.setAlwaysDrawnWithCacheEnabled(true);
            
                      //add   adView    
                     coAct.adView.loadAd(new AdRequest.Builder().build()); 
                     coAct.adLayout.addView(coAct.adView); 
               }catch (Exception e) { };
      }
      else if(!b && coAct.adView != null && coAct.adView instanceof AdView)    

{

   coAct.adLayout.removeView(coAct.adView);

               coAct.adView.setVisibility(View.GONE);
               coAct.adView.setEnabled(false);
               coAct.adView.destroyDrawingCache();
               coAct.adView.removeAllViews();
               ((AdView)coAct.adView).destroy();
               coAct.adView = null;
     
               //RecycleUtils.recursiveRecycle(((AdView)coAct.adView));
               System.gc();
               coAct.adLayout.setVisibility(View.INVISIBLE);
        }
   }
  });

 }  

 //=========================================================

 

.......................

.......................

.......................

 

 

 

 

 

 

2. game_demo.xml

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/gamePlace"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/admob"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="#00000000"
        android:clickable="false"
        android:visibility="visible" >
    </LinearLayout>

</RelativeLayout>

 

 

 

 

 

3. AdJni.h   AdJni.cpp

AdJni.h  ===================

===========================

#ifndef __ANDROID_ADM_JNI_H__
#define __ANDROID_ADM_JNI_H__

extern "C" 
{
         extern void showADJNI(const bool b);
}

#endif

 

 

AdJni.cpp=======================

================================

#include "AdJni.h"
#include "JniHelper.h"
#include <jni.h>

using namespace cocos2d;

extern "C"
{
     void showADJNI(const bool b)
     {
          JniMethodInfo t;
          if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/Cocos2dxActivity",

                       "showAD", "(Z)V"))
        {
               t.env->CallStaticVoidMethod(t.classID,t.methodID, b);
               t.env->DeleteLocalRef(t.classID);
        }
 }

}

 

 

 

 

 

 

저작자 표시 비영리 변경 금지
신고
1 2 3 4 5 6 7 ··· 16 

카운터

Total : 14,066 / Today : 1 / Yesterday : 1
get rsstistory!

티스토리 툴바