Add AdMob Advertise to each and every Screen of Android App

末鹿安然 提交于 2020-01-14 05:40:25

问题


I am able to load the AdMob add using following code into the very first screen (Activity) of the application.

@Override
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout);
        View next = findViewById(R.id.next);
        OnClickListener onClickListener = new OnClickListener() {
            public void onClick(View v) {
                startActivity(new Intent(BannerEssentials.this, NextActivity.class));
            }
        };
        next.setOnClickListener(onClickListener);

        AdView adView = new AdView(this, AdSize.BANNER, "");
        layout.addView(adView);
        AdRequest request = new AdRequest();
        request.addTestDevice(AdRequest.TEST_EMULATOR);
        adView.loadAd(request);            
    }

The next screen (Activity) comes to foreground after pressing the Next button; but the advertise does not appear on this screen.

Is there any (easy) way to add AdMob add to each and every screen(Activity) of the applicaiton?


回答1:


Create a super class and centralize all your admob related code into a protected method:

public class AdmobActivity extends Activity {
  ... ...

  protected void showAds(View root) {
    AdView adView = new AdView(this, AdSize.BANNER, "");
    root.addView(adView);
    AdRequest request = new AdRequest();
    request.addTestDevice(AdRequest.TEST_EMULATOR);
    adView.loadAd(request); 
  }

... ...
}

For all your activities that need show ads, extends AdmobActivity and call showAds() properly in Acticity.onCreate() method.

Hope that help.




回答2:


The reason you aren't seeing any ads is because you did not define a publisher id, but instead provided a blank string. Go to www.admob.com, sign up for a free account, and create an app to get a publisher id to use to get ads.



来源:https://stackoverflow.com/questions/8284194/add-admob-advertise-to-each-and-every-screen-of-android-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!