如何将AdMob添加到屏幕?

时间:2015-06-03 20:17:34

标签: java android admob surfaceview

如何将Admob广告到GameOver屏幕? 我搜索了很多,但我找不到很多!

我的主要活动:

 public class MainActivity extends Activity {

    private GameView gView; 

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

    gView = new GameView(this);
    setContentView(gView);
    }
  }

和我的GameView:

public class GameView extends SurfaceView {

      private boolean gameOver;

    public GameView(Context context) {
        super(context);
        gameOver = false;
      }


       @Override
    protected void onDraw(Canvas canvas) {
      if(!gameOver)
        {
          drawGame(canvas);
        }
        else
        {
          //add Admob Banner here to the Screen
          drawGameOver(canvas);
        }

      }

     public void drawGame(Canvas can){
       }

      public void drawGameOver(Canvas can){
        }



    }

我的整个游戏画在画布上。但我只想在GameOver屏幕上播放AdMob。 我怎么能在那里展示它? 我发现了一些东西: Interstitial ad with admob at gameOverScene 但我认为这不是我的解决方案。或者我如何将其实现到我的代码中?

非常感谢你的帮助!

2 个答案:

答案 0 :(得分:0)

您需要在AdView中创建一个main_activity.xml,如:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id">
    </com.google.android.gms.ads.AdView>

</RelativeLayout>

然后尝试以下代码:

public class MainActivity extends Activity {

    private GameView gView;
    private AdView mAdView;

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

        View _view = getLayoutInflater().inflate(R.layout.main_activity, null);

        mAdView = (AdView) _view.findViewById(R.id.adView);

        gView = new GameView(this);

        setContentView(gView);

    }

    public void showBanner(){

        AdRequest _adRequest = new AdRequest.Builder().build();

        mAdView.loadAd(_adRequest);

    }

}

public class GameView extends SurfaceView {

    private boolean gameOver;
    private Activity mActivity;

    public GameView(Activity activity) {
        super(activity);

        gameOver = false;

    }


    @Override
    protected void onDraw(Canvas canvas) {

        if(!gameOver){

            drawGame(canvas);

        }
        else
        {

            //add Admob here to the Screen
            drawGameOver(canvas);

        }

    }

    public void drawGame(Canvas can){

    }

    public void drawGameOver(Canvas can){

        mActivity.showBanner();

    }

}

有关详细信息,请查看以下链接:https://developers.google.com/mobile-ads-sdk/docs/admob/android/quick-start

答案 1 :(得分:0)

您可以通过编程方式添加横幅广告,并通过收听者在游戏的屏幕中显示或隐藏它们。修改您的代码,如下所示,确保替换&#34; yourAdUnitId&#34;使用您真实的adUnit Id

public class MainActivity extends Activity implements MyAdListener{
private GameView gView; 
AdView adView;
    @Override 
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
 gView = new GameView(this);

AdSize adSize = AdSize.SMART_BANNER;

    DisplayMetrics dm = getResources().getDisplayMetrics();

    double density = dm.density * 160;
    double x = Math.pow(dm.widthPixels / density, 2);
    double y = Math.pow(dm.heightPixels / density, 2);
    double screenInches = Math.sqrt(x + y);

    if (screenInches > 8) { // > 728 X 90
        adSize = AdSize.LEADERBOARD;
    } else if (screenInches > 6) { // > 468 X 60
        adSize = AdSize.MEDIUM_RECTANGLE;
    } else { // > 320 X 50
        adSize = AdSize.BANNER;
    }       
    adView = new AdView(this);
     adView.setAdUnitId("yourAdUnitId"); 
     adView.setAdSize(adSize);
     adView.setVisibility(View.INVISIBLE);
     adView.setBackgroundColor(Color.TRANSPARENT)
    AdRequest adRequest = new AdRequest.Builder().build();
     adView.loadAd(adRequest);     
    RelativeLayout.LayoutParams adViewParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    adViewParams.addRule(RelativeLayout.CENTER_HORIZONTAL | RelativeLayout.ALIGN_PARENT_TOP);
    RelativeLayout layout = new RelativeLayout(this);
    layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT));
    layout.addView(gView);
    layout.addView(adView, adViewParams);
    setContentView(layout);
}

@Override
  public void showBannerAd() {
   runOnUiThread(new Runnable() {
   @Override
   public void run(){
    adView.setVisibility(View.VISIBLE);
    }
  });
}
  }



public class GameView extends SurfaceView {

private boolean gameOver;
private Activity mActivity;

public GameView(Activity activity) {
    super(activity);
    myAdListener = (MyAdListener)activity;
    gameOver = false;

}


@Override
protected void onDraw(Canvas canvas) {

    if(!gameOver){

        drawGame(canvas);

    }
    else
    {

        //show admob
       myAdListener.showBannerAd();
        drawGameOver(canvas);

    }

}

public void drawGame(Canvas can){

}

public void drawGameOver(Canvas can){

}

public interface MyAdListener{
public void showBannerAd();
}

}