在片段上显示Admob非页内广告

时间:2016-12-12 12:07:16

标签: android admob

我正在尝试在我的应用上添加一个admob插页广告虽然我已成功将其显示在一个活动上,但是你可以告诉我如何在片段上进行操作吗?谢谢。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ViewGroup view = (ViewGroup) inflater.inflate(R.layout.game_fragment, container, false);
    view.setClipChildren(false);
    ((ViewGroup)view.findViewById(R.id.game_board)).setClipChildren(false);
    mTime = (TextView) view.findViewById(R.id.time_bar_text);
    mTimeImage = (ImageView) view.findViewById(R.id.time_bar_image);
    FontLoader.setTypeface(Shared.context, new TextView[] {mTime}, Font.GROBOLD);
    mBoardView = BoardView.fromXml(getActivity().getApplicationContext(), view);
    FrameLayout frameLayout = (FrameLayout) view.findViewById(R.id.game_container);
    frameLayout.addView(mBoardView);
    frameLayout.setClipChildren(false);

    // build board
    buildBoard();
    Shared.eventBus.listen(FlipDownCardsEvent.TYPE, this);
    Shared.eventBus.listen(HidePairCardsEvent.TYPE, this);
    Shared.eventBus.listen(GameWonEvent.TYPE, this);

    return view;
}

这是我的Activity`public类MainActivity扩展了FragmentActivity {

private ImageView mBackgroundImage;

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

    Shared.context = getApplicationContext();
    Shared.engine = Engine.getInstance();
    Shared.eventBus = EventBus.getInstance();

    setContentView(R.layout.activity_main);
    mBackgroundImage = (ImageView) findViewById(R.id.background_image);

    Shared.activity = this;
    Shared.engine.start();
    Shared.engine.setBackgroundImageView(mBackgroundImage);

    // set background
    setBackgroundImage();

    // set menu
    ScreenController.getInstance().openScreen(Screen.MENU);
}

@Override
protected void onDestroy() {
    Shared.engine.stop();
    super.onDestroy();
}

@Override
public void onBackPressed() {
    if (PopupManager.isShown()) {
        PopupManager.closePopup();
        if (ScreenController.getLastScreen() == Screen.GAME) {
            Shared.eventBus.notify(new BackGameEvent());
        }
    } else if (ScreenController.getInstance().onBack()) {
        super.onBackPressed();
    }
}

private void setBackgroundImage() {
    Bitmap bitmap = Utils.scaleDown(R.drawable.background, Utils.screenWidth(), Utils.screenHeight());
    bitmap = Utils.crop(bitmap, Utils.screenHeight(), Utils.screenWidth());
    bitmap = Utils.downscaleBitmap(bitmap, 2);
    mBackgroundImage.setImageBitmap(bitmap);
}

} `

2 个答案:

答案 0 :(得分:3)

使用以下代码显示插页式广告。

InterestitialAd mInterstitialAd = new InterstitialAd(this);
            mInterstitialAd.setAdUnitId(getResources().getString(R.string.interstitial_ad_unit_id));
            AdRequest adRequest = new AdRequest.Builder().build();
            mInterstitialAd.loadAd(adRequest);
            mInterstitialAd.setAdListener(new AdListener() {
                @Override
                public void onAdLoaded() {
                    super.onAdLoaded();
                    mInterstitialAd.show();
                }
            });

答案 1 :(得分:0)

这是我的活动    公共类MainActivity扩展了FragmentActivity {

private ImageView mBackgroundImage;

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

    Shared.context = getApplicationContext();
    Shared.engine = Engine.getInstance();
    Shared.eventBus = EventBus.getInstance();

    setContentView(R.layout.activity_main);
    mBackgroundImage = (ImageView) findViewById(R.id.background_image);

    Shared.activity = this;
    Shared.engine.start();
    Shared.engine.setBackgroundImageView(mBackgroundImage);

    // set background
    setBackgroundImage();

    // set menu
    ScreenController.getInstance().openScreen(Screen.MENU);
}

@Override
protected void onDestroy() {
    Shared.engine.stop();
    super.onDestroy();
}

@Override
public void onBackPressed() {
    if (PopupManager.isShown()) {
        PopupManager.closePopup();
        if (ScreenController.getLastScreen() == Screen.GAME) {
            Shared.eventBus.notify(new BackGameEvent());
        }
    } else if (ScreenController.getInstance().onBack()) {
        super.onBackPressed();
    }
}

private void setBackgroundImage() {
    Bitmap bitmap = Utils.scaleDown(R.drawable.background, Utils.screenWidth(), Utils.screenHeight());
    bitmap = Utils.crop(bitmap, Utils.screenHeight(), Utils.screenWidth());
    bitmap = Utils.downscaleBitmap(bitmap, 2);
    mBackgroundImage.setImageBitmap(bitmap);
}

}