Java Code Examples for com.google.android.gms.ads.AdRequest#Builder

The following examples show how to use com.google.android.gms.ads.AdRequest#Builder . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AdmobPlus.java    From admob-plus with MIT License 6 votes vote down vote up
private AdRequest.Builder createAdRequestBuilder(PluginCall call) {
    AdRequest.Builder builder = new AdRequest.Builder();

    JSArray testDevices = call.getArray("testDevices", new JSArray());
    for (int i = 0; i < testDevices.length(); i++) {
        try {
            builder.addTestDevice(testDevices.getString(i));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    if (call.getBoolean("childDirected") != null) {
        builder.tagForChildDirectedTreatment(call.getBoolean("childDirected"));
    }

    Boolean nonPersonalizedAds = call.getBoolean("nonPersonalizedAds");
    if (nonPersonalizedAds != null && nonPersonalizedAds) {
        Bundle extras = new Bundle();
        extras.putString("npa", "1");
        builder.addNetworkExtrasBundle(AdMobAdapter.class, extras);
    }

    return builder;
}
 
Example 2
Source File: Action.java    From admob-plus with MIT License 6 votes vote down vote up
public AdRequest buildAdRequest() {
    Bundle extras = new Bundle();
    AdRequest.Builder builder = new AdRequest.Builder();
    JSONArray testDevices = this.opts.optJSONArray("testDevices");
    if (testDevices != null) {
        for (int i = 0; i < testDevices.length(); i++) {
            String testDevice = testDevices.optString(i);
            if (testDevice != null) {
                builder.addTestDevice(testDevice);
            }
        }
    }
    if (this.opts.has("childDirected")) {
        builder.tagForChildDirectedTreatment(opts.optBoolean("childDirected"));
    }
    if (this.opts.has("npa")) {
        extras.putString("npa", opts.optString("npa"));
    }
    if (this.opts.has("underAgeOfConsent")) {
        extras.putBoolean("tag_for_under_age_of_consent", opts.optBoolean("underAgeOfConsent"));
    }
    return builder.addNetworkExtrasBundle(AdMobAdapter.class, extras).build();
}
 
Example 3
Source File: AdsManager.java    From text_converter with GNU General Public License v3.0 6 votes vote down vote up
public static void showFullScreenAdsIfRequired(final StateActivity activity) {
    if (Premium.isPremiumUser(activity)) return;
    final InterstitialAd interstitialAd = new InterstitialAd(activity.getApplicationContext());
    interstitialAd.setAdUnitId(AdConstants.AdUnitId.AD_UNIT_ID_INTERSTITIAL);
    AdRequest.Builder request = new AdRequest.Builder();
    if (BuildConfig.DEBUG) {
        request.addTestDevice(TEST_DEVICE_ID);
    }
    interstitialAd.loadAd(request.build());
    interstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            super.onAdLoaded();
            if (!activity.isFinishing() && activity.isActivityVisible()
                    && !Premium.isPremiumUser(activity)) {
                interstitialAd.show();
            }
        }
    });
}
 
Example 4
Source File: AdMob.java    From GodotAds with Apache License 2.0 6 votes vote down vote up
private void requestNewRewardedVideo() {
	if (_config == null) { return; }

	AdRequest.Builder adRB = new AdRequest.Builder();

	if (BuildConfig.DEBUG) {
		adRB.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
		adRB.addTestDevice(Utils.getDeviceId(activity));
	}

	String ad_unit_id = _config.optString("RewardedVideoAdId", "");

	if (ad_unit_id.length() <= 0) {
		Utils.d("GodotAds", "AdMob:RewardedVideo:UnitId:NotProvided");
		ad_unit_id = activity.getString(R.string.gads_rewarded_video_ad_unit_id);	
	}

	mrv.loadAd(ad_unit_id, adRB.build());
}
 
Example 5
Source File: GooglePlayServicesInterstitial.java    From mobile-sdk-android with Apache License 2.0 6 votes vote down vote up
private AdRequest buildRequest(TargetingParameters targetingParameters) {
    AdRequest.Builder builder = new AdRequest.Builder();

    Bundle bundle = new Bundle();

    if (targetingParameters.getAge() != null) {
        bundle.putString("Age", targetingParameters.getAge());
    }
    if (targetingParameters.getLocation() != null) {
        builder.setLocation(targetingParameters.getLocation());
    }
    for (Pair<String, String> p : targetingParameters.getCustomKeywords()) {
        if (p.first.equals("content_url")) {
            if (!StringUtil.isEmpty(p.second)) {
                builder.setContentUrl(p.second);
            }
        } else {
            bundle.putString(p.first, p.second);
        }
    }

    //Since AdMobExtras is deprecated so we need to use below method
    builder.addNetworkExtrasBundle(com.google.ads.mediation.admob.AdMobAdapter.class, bundle);

    return builder.build();
}
 
Example 6
Source File: GooglePlayServicesBanner.java    From mobile-sdk-android with Apache License 2.0 5 votes vote down vote up
public static AdRequest buildRequest(TargetingParameters targetingParameters) {
    AdRequest.Builder builder = new AdRequest.Builder();

    if (targetingParameters != null) {
        if (targetingParameters.getLocation() != null) {
            builder.setLocation(targetingParameters.getLocation());
        }
        Bundle bundle = new Bundle();

        if (targetingParameters.getAge() != null) {
            bundle.putString("Age", targetingParameters.getAge());
        }

        for (Pair<String, String> p : targetingParameters.getCustomKeywords()) {
            if (p.first.equals("content_url")) {
                if (!StringUtil.isEmpty(p.second)) {
                    builder.setContentUrl(p.second);
                }
            } else {
                bundle.putString(p.first, p.second);
            }
        }

        builder.addNetworkExtrasBundle(AdMobAdapter.class, bundle);
    }


    return builder.build();
}
 
Example 7
Source File: AdmobPlus.java    From admob-plus with MIT License 5 votes vote down vote up
@PluginMethod()
public void interstitial_load(final PluginCall call) {
    final Interstitial interstitial = Ad.createInterstitial(call.getInt("id"), this);
    final AdRequest.Builder builder = createAdRequestBuilder(call);
    bridge.executeOnMainThread(new Runnable() {
        @Override
        public void run() {
            interstitial.load(call.getString("adUnitId"), builder.build());
            call.resolve();
        }
    });
}
 
Example 8
Source File: RNAdMobBannerViewManager.java    From react-native-admob with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void loadBanner() {
    AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
    if (testDevices != null) {
        for (int i = 0; i < testDevices.length; i++) {
            String testDevice = testDevices[i];
            if (testDevice == "SIMULATOR") {
                testDevice = AdRequest.DEVICE_ID_EMULATOR;
            }
            adRequestBuilder.addTestDevice(testDevice);
        }
    }
    AdRequest adRequest = adRequestBuilder.build();
    this.adView.loadAd(adRequest);
}
 
Example 9
Source File: AdmobFetcherBase.java    From admobadapter with Apache License 2.0 5 votes vote down vote up
/**
 * Setup and get an ads request
 */
protected synchronized AdRequest getAdRequest() {
    AdRequest.Builder adBldr = new AdRequest.Builder();
    for (String id : getTestDeviceIds()) {
        adBldr.addTestDevice(id);
    }
    return adBldr.build();
}
 
Example 10
Source File: TestAdMobClassicActivity.java    From UltimateRecyclerView with Apache License 2.0 5 votes vote down vote up
private AdView createadmob() {
    AdView mAdView = new AdView(this);
    mAdView.setAdSize(AdSize.MEDIUM_RECTANGLE);
    mAdView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
    mAdView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    // Create an ad request.
    AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
    if (admob_test_mode)
        // Optionally populate the ad request builder.
        adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
    // Start loading the ad.
    mAdView.loadAd(adRequestBuilder.build());
    return mAdView;
}
 
Example 11
Source File: MainActivity.java    From BusyBox with Apache License 2.0 5 votes vote down vote up
private void setupBanners() {
    AdRequest.Builder builder = new AdRequest.Builder();
    if (App.isDebuggable()) {
        builder.addTestDevice(DeviceUtils.getDeviceId());
    }

    adViewTiers[currentAdViewIndex] = new AdView(this);
    adViewTiers[currentAdViewIndex].setAdSize(AdSize.SMART_BANNER);
    adViewTiers[currentAdViewIndex]
        .setAdUnitId(getResources().getStringArray(R.array.banners_id)[currentAdViewIndex]);
    adViewTiers[currentAdViewIndex].setAdListener(new AdListener() {
        @Override public void onAdFailedToLoad(int errorCode) {
            if (currentAdViewIndex != (adViewTiers.length - 1)) {
                currentAdViewIndex++;
                setupBanners();
            } else if (adContainer.getVisibility() == View.VISIBLE) {
                Technique.SLIDE_OUT_DOWN.getComposer().hideOnFinished().playOn(adContainer);
            }
        }

        @Override public void onAdLoaded() {
            adContainer.setVisibility(View.VISIBLE);
            if (adContainer.getChildCount() != 0) {
                adContainer.removeAllViews();
            }
            adContainer.addView(adViewTiers[currentAdViewIndex]);
            Analytics.newEvent("on_ad_loaded")
                .put("id", adViewTiers[currentAdViewIndex].getAdUnitId()).log();
        }
    });

    adViewTiers[currentAdViewIndex].loadAd(builder.build());
}
 
Example 12
Source File: MainActivity.java    From BusyBox with Apache License 2.0 5 votes vote down vote up
private void setupBanners() {
    AdRequest.Builder builder = new AdRequest.Builder();
    if (App.isDebuggable()) {
        builder.addTestDevice(DeviceUtils.getDeviceId());
    }

    adViewTiers[currentAdViewIndex] = new AdView(this);
    adViewTiers[currentAdViewIndex].setAdSize(AdSize.SMART_BANNER);
    adViewTiers[currentAdViewIndex]
        .setAdUnitId(getResources().getStringArray(R.array.banners_id)[currentAdViewIndex]);
    adViewTiers[currentAdViewIndex].setAdListener(new AdListener() {
        @Override public void onAdFailedToLoad(int errorCode) {
            if (currentAdViewIndex != (adViewTiers.length - 1)) {
                currentAdViewIndex++;
                setupBanners();
            } else if (adContainer.getVisibility() == View.VISIBLE) {
                Technique.SLIDE_OUT_DOWN.getComposer().hideOnFinished().playOn(adContainer);
            }
        }

        @Override public void onAdLoaded() {
            adContainer.setVisibility(View.VISIBLE);
            if (adContainer.getChildCount() != 0) {
                adContainer.removeAllViews();
            }
            adContainer.addView(adViewTiers[currentAdViewIndex]);
            Analytics.newEvent("on_ad_loaded")
                .put("id", adViewTiers[currentAdViewIndex].getAdUnitId()).log();
        }
    });

    adViewTiers[currentAdViewIndex].loadAd(builder.build());
}
 
Example 13
Source File: AdUtil.java    From NMSAlphabetAndroidApp with MIT License 5 votes vote down vote up
private static AdRequest getAdRequest(Context context){
    AdRequest.Builder request = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
    if(BuildConfig.DEBUG) {
        request.addTestDevice(Util.getDeviceId(context));
    }
    return request.build();
}
 
Example 14
Source File: AdMob.java    From GodotAds with Apache License 2.0 5 votes vote down vote up
private void requestNewInterstitial() {
	AdRequest.Builder adRB = new AdRequest.Builder();

	if (BuildConfig.DEBUG) {
		adRB.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
		adRB.addTestDevice(Utils.getDeviceId(activity));
	}

	AdRequest adRequest = adRB.build();

	mInterstitialAd.loadAd(adRequest);
}
 
Example 15
Source File: TestAdvancedAdmobActivity.java    From UltimateRecyclerView with Apache License 2.0 4 votes vote down vote up
private RelativeLayout createadmob() {

        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;
        }

        adSize = AdSize.MEDIUM_RECTANGLE;
        final AdView mAdView = new AdView(this);
        mAdView.setAdSize(adSize);
        mAdView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
        // Create an ad request.
        AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
        if (admob_test_mode)
            // Optionally populate the ad request builder.
            adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
        // Start loading the ad.
        mAdView.loadAd(adRequestBuilder.build());
        DisplayMetrics displaymetrics = new DisplayMetrics();
        final RelativeLayout layout = AdGoogleDisplaySupport.initialSupport(this, displaymetrics);
        final double ratio = AdGoogleDisplaySupport.ratioMatching(displaymetrics);
        final int ad_height = AdGoogleDisplaySupport.defaultHeight(displaymetrics);
        AdGoogleDisplaySupport.panelAdjust(mAdView, (int) (ad_height * ratio));
        // get display info
        /*  G.display_w = displayMetrics.widthPixels;
        G.display_h = displayMetrics.heightPixels;
        G.scale = Math.max(G.display_w/1280.0f, G.display_h/800.0f);*/
        mAdView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                super.onAdLoaded();
                int h = mAdView.getLayoutParams().height;
                AdGoogleDisplaySupport.scale(mAdView, ratio);
                AdGoogleDisplaySupport.panelAdjust(mAdView, (int) (h * ratio));
                //  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
            }
        });
        layout.addView(mAdView);
        return layout;
    }
 
Example 16
Source File: AdMob.java    From cordova-plugin-admob-free with MIT License 4 votes vote down vote up
public AdRequest buildAdRequest() {
    AdRequest.Builder builder = new AdRequest.Builder();
    if (config.isTesting || isRunningInTestLab()) {
        builder = builder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR).addTestDevice(getDeviceId());
    }

    if (config.testDeviceList != null) {
        Iterator<String> iterator = config.testDeviceList.iterator();
        while (iterator.hasNext()) {
            builder = builder.addTestDevice(iterator.next());
        }
    }

    Bundle bundle = new Bundle();
    bundle.putInt("cordova", 1);
    if (config.adExtras != null) {
        Iterator<String> it = config.adExtras.keys();
        while (it.hasNext()) {
            String key = it.next();
            try {
                bundle.putString(key, config.adExtras.get(key).toString());
            } catch (JSONException exception) {
                Log.w(TAG, String.format("Caught JSON Exception: %s", exception.getMessage()));
            }
        }
    }
    builder = builder.addNetworkExtrasBundle(AdMobAdapter.class, bundle);

    if (config.gender != null) {
        if ("male".compareToIgnoreCase(config.gender) != 0) {
            builder.setGender(AdRequest.GENDER_MALE);
        } else if ("female".compareToIgnoreCase(config.gender) != 0) {
            builder.setGender(AdRequest.GENDER_FEMALE);
        } else {
            builder.setGender(AdRequest.GENDER_UNKNOWN);
        }
    }
    if (config.location != null) {
        builder.setLocation(config.location);
    }
    if ("yes".equals(config.forFamily)) {
        builder.setIsDesignedForFamilies(true);
    } else if ("no".equals(config.forFamily)) {
        builder.setIsDesignedForFamilies(false);
    }
    if ("yes".equals(config.forChild)) {
        builder.tagForChildDirectedTreatment(true);
    } else if ("no".equals(config.forChild)) {
        builder.tagForChildDirectedTreatment(false);
    }
    if (config.contentURL != null) {
        builder.setContentUrl(config.contentURL);
    }

    return builder.build();
}
 
Example 17
Source File: AdMobMediation.java    From cordova-admob-pro with MIT License votes vote down vote up
public abstract AdRequest.Builder joinAdRequest(AdRequest.Builder builder);