简体   繁体   中英

How to add two different admob app ID in same app?

Hello here is the code i am using to display two different adunit ids from two different admob account . But banners ads are not seen in app.

enter code here
private void setUpAds(){
    MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");         

    AdView bannerAdView = new AdView(this);
    int idSelection = random.nextInt(2);
    // Uniform distribution
    if (idSelection == 0) {
        adId = BANNER_ID_1;
    } else {
        adId = BANER_ID_2;
    }
    // Or you can use weighted distribution
    idSelection = random.nextInt(10);
    // %80 chance first, %20 second
    if (idSelection < 8) {
        adId = BANNER_ID_1;
    } else {
        adId = BANER_ID_2;
    }
    bannerAdView.setAdUnitId(adId);

    bannerAdView.setAdSize(AdSize.BANNER);
    AdRequest adRequest6 = new AdRequest.Builder().addTestDevice("E4D1201527AD69E0FD7A0551277A5232").build();
    bannerAdView.loadAd(adRequest6);

}



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

Create both ad units, and use a Random to pick a banner id. You can also use weighted selection which one id is more likely to be selected.

private static final String BANNER_ID_1 = "ca....";
private static final String BANER_ID_2 = "ca....";
private Random random = new Random();
private String adId = BANNER_ID_1;

private AdView bannerAdView;


private void setUpAds() {
    bannerAdView = new AdView(context);
    int idSelection = random.nextInt(2);
    // Uniform distribution
    if (idSelection == 0) {
        adId = BANNER_ID_1;
    } else {
        adId = BANER_ID_2;
    }
    // Or you can use weighted distribution
    idSelection = random.nextInt(10);
    // %80 chance first, %20 second
    if (idSelection < 8) {
        adId = BANNER_ID_1;
    } else {
        adId = BANER_ID_2;
    }

    bannerAdView.setAdUnitId(adId);

    // Or you can use weighted distribution
    idSelection = random.nextInt(10);
    // %80 chance first, %20 second
    if (idSelection < 8)

    {
        adId = BANNER_ID_1;
    } else {
        adId = BANER_ID_2;
    }

    bannerAdView.setAdUnitId(adId);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM