2017-04-22 50 views
-1

您好,我正在尝试在Libgdx中使用RevMob集成横幅广告。但是由于某种原因它没有显示。Revmob在Libgdx

我正在使用以下代码。

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    // setContentView(R.layout.activity_game_new); 

    AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration(); 
    // cfg.useGL20 = false; 

    final RelativeLayout gameLayout = new RelativeLayout(this); 

    RevMobIntegration revmob = new RevMobIntegration(this); 

    RelativeLayout bannerLayout = new RelativeLayout(this); 


    RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(
      RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
    adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
    adParams.addRule(RelativeLayout.CENTER_VERTICAL); 

    bannerLayout.setLayoutParams(adParams); 

    game = new MyGdxGame(GameActivity.this, revmob); 
    game.setRedirectionListener(this); 


    View gameView = initializeForView(game, cfg); 


    requestWindowFeature(android.view.Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
      WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); 

    // Add the libgdx view 
    gameLayout.addView(gameView); 
//  gameLayout.addView(bannerLayout); 

    Log.d("RevMob", "Checking BannerAd"); 
    if (revmob.getBannerAd() != null) { 
     Log.d("RevMob", "Displaying Called"); 
     gameLayout.addView(revmob.getBannerAd()); 
    } 


    // Hook it all up 
    setContentView(gameLayout); 


    this.onPause(); 
    this.onResume(); 
    gameLayout.refreshDrawableState(); 


    initChartboost(); 


//  startRevMobSession(); 

} 

这里是RevMobIntegration类:

public class RevMobIntegration implements RevmobAdInterface { 

    private static final String APPLICATION_ID = "YourAdmobAppIDHere"; 

    // Set this to false when creating the version for the store. 
    private static final boolean DEBUG = true; 

    private RevMobAdsListener listener; 

    private RevMobFullscreen fullscreenAd; 
    private RevMobBanner bannerAd; 

    private Activity application; 
    private RevMob revmob; 

    public RevMobIntegration(Activity _application) { 
    this.application = _application; 

    startRevMobSession(); 
    } 


    public void startRevMobSession() { 
    //RevMob's Start Session method: 
    revmob = RevMob.startWithListener(application, new RevMobAdsListener() { 
    @Override 
    public void onRevMobSessionStarted() { 
     loadBanner(); // Cache the banner once the session is started 
     Log.i("RevMob", "Session Started"); 
    } 

    @Override 
    public void onRevMobSessionNotStarted(String message) { 
     //If the session Fails to start, no ads can be displayed. 
     Log.i("RevMob", "Session Failed to Start"); 
    } 
    }, application.getString(R.string.rev_mob_app_id)); 
    } 


    //RevMob 
    public void loadBanner() { 
    bannerAd = revmob.preLoadBanner(application, new RevMobAdsListener() { 
    @Override 
    public void onRevMobAdReceived() { 
     showBannerAd(true); 
     Log.i("RevMob", "Banner Ready to be Displayed"); //At this point, 
     the banner is ready to be displayed. 
    } 

    @Override 
    public void onRevMobAdNotReceived(String message) { 
     Log.i("RevMob", "Banner Not Failed to Load"); 
    } 

    @Override 
    public void onRevMobAdDisplayed() { 
     Log.i("RevMob", "Banner Displayed"); 
    } 
    }); 
    } 

    @Override 
    public void showBannerAd(boolean show) { 
    if(show) { 
    Log.i("RevMob", "Showing"); 
    if(bannerAd == null) { 
     startRevMobSession(); 
    } else { 
     Log.i("RevMob", "Banner Displayed"); 
     bannerAd.show(); 
     } 
    } else { 
    bannerAd.hide(); 
    } 
    } 

    public RevMobBanner getBannerAd() { return bannerAd; } 


} 

我在我的活动整合了RevMob,它是工作的罚款。但是对于游戏屏幕,广告正在初始化但不显示。 有什么建议吗?

回答

0

看来revmob.getBannerAd()返回null因为bannerAd对象创建时调用loadBanner();。 RevMob需要一段时间才能开始会话。

if (revmob.getBannerAd() != null) { 
    Log.d("RevMob", "Displaying Called"); 
    gameLayout.addView(revmob.getBannerAd()); 
} 

您可以检查此repo澄清,你也可以看看这个class的。

+0

谢谢你是对的。它没有被调用。所以我放了5秒钟的延迟并调用它,用LOG“显示调用”进行检查。但是屏幕上还没有出现广告。还有什么我应该尝试? –

+0

你为什么不试试我的回购? – Aryan

+0

是的,尝试。在RevMobHelper中,您已经注释了以下代码 // if(banner!= null){// layout.addView(banner,topParams); //banner.setBackgroundColor(Color.BLACK); //} 我需要取消注释该权利? –