@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Locate the NativeExpressAdView. mAdView = findViewById(R.id.adView); // Set its video options. mAdView.setVideoOptions(new VideoOptions.Builder() .setStartMuted(true) .build()); // The VideoController can be used to get lifecycle events and info about an ad's video // asset. One will always be returned by getVideoController, even if the ad has no video // asset. mVideoController = mAdView.getVideoController(); mVideoController.setVideoLifecycleCallbacks(new VideoController.VideoLifecycleCallbacks() { @Override public void onVideoEnd() { Log.d(LOG_TAG, "Video playback is finished."); super.onVideoEnd(); } }); // Set an AdListener for the AdView, so the Activity can take action when an ad has finished // loading. mAdView.setAdListener(new AdListener() { @Override public void onAdLoaded() { if (mVideoController.hasVideoContent()) { Log.d(LOG_TAG, "Received an ad that contains a video asset."); } else { Log.d(LOG_TAG, "Received an ad that does not contain a video asset."); } } }); mAdView.loadAd(new AdRequest.Builder().build()); }
private void initData() { //初始化firebase 统计 FirebaseAnalytics.getInstance(getApplicationContext()); //创建广告请求 adRequest = new AdRequest.Builder() .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)//所有的模拟器 .addTestDevice("74BAABC1A0E77EB8C34896404447DBEC")//nexus 5 .addTestDevice("E3A8BB93EE8D9CF7C0B5351AB456C4C5")//我的锤子M1L .build(); //横幅广告 adv_banner.loadAd(adRequest); //插页广告 interstitialAd = new InterstitialAd(this); interstitialAd.setAdUnitId(getString(R.string.insert_ad_unit_id));//设置单元id //增加监听 interstitialAd.setAdListener(new AdListener() { @Override public void onAdClosed() { //关闭了这个广告先加载下一个广告 requestNewInterstitial(); } }); //加载插页广告 requestNewInterstitial(); neadv_native.setAdListener(new AdListener() { @Override public void onAdFailedToLoad(int i) { Log.d("AdListener", "onAdFailedToLoad i:" + i); super.onAdFailedToLoad(i); } }); // Set its video options. neadv_native.setVideoOptions(new VideoOptions.Builder() .setStartMuted(true) .build()); // The VideoController can be used to get lifecycle events and info about an ad's video // asset. One will always be returned by getVideoController, even if the ad has no video // asset. mVideoController = neadv_native.getVideoController(); mVideoController.setVideoLifecycleCallbacks(new VideoController.VideoLifecycleCallbacks() { @Override public void onVideoEnd() { Log.d("onVideoEnd", "Video playback is finished."); super.onVideoEnd(); } }); // Set an AdListener for the AdView, so the Activity can take action when an ad has finished // loading. neadv_native.setAdListener(new AdListener() { @Override public void onAdLoaded() { if (mVideoController.hasVideoContent()) { Log.d("onAdLoaded", "Received an ad that contains a video asset."); } else { Log.d("onAdLoaded", "Received an ad that does not contain a video asset."); } } }); //加载原生广告 neadv_native.loadAd(new AdRequest.Builder().build()); }
/** * Populates a {@link NativeAppInstallAdView} object with data from a given * {@link NativeAppInstallAd}. * * @param nativeAppInstallAd the object containing the ad's assets * @param adView the view to be populated */ private void populateAppInstallAdView(NativeAppInstallAd nativeAppInstallAd, NativeAppInstallAdView adView) { VideoController videoController = nativeAppInstallAd.getVideoController(); // Assign native ad object to the native view. adView.setNativeAd(nativeAppInstallAd); adView.setHeadlineView(adView.findViewById(R.id.appinstall_headline)); adView.setBodyView(adView.findViewById(R.id.appinstall_body)); adView.setCallToActionView(adView.findViewById(R.id.appinstall_call_to_action)); adView.setIconView(adView.findViewById(R.id.appinstall_app_icon)); adView.setPriceView(adView.findViewById(R.id.appinstall_price)); adView.setStarRatingView(adView.findViewById(R.id.appinstall_stars)); adView.setStoreView(adView.findViewById(R.id.appinstall_store)); // Some assets are guaranteed to be in every NativeAppInstallAd. ((TextView) adView.getHeadlineView()).setText(nativeAppInstallAd.getHeadline()); ((TextView) adView.getBodyView()).setText(nativeAppInstallAd.getBody()); ((Button) adView.getCallToActionView()).setText(nativeAppInstallAd.getCallToAction()); ((ImageView) adView.getIconView()).setImageDrawable(nativeAppInstallAd.getIcon() .getDrawable()); MediaView sampleMediaView = adView.findViewById(R.id.appinstall_media); ImageView imageView = adView.findViewById(R.id.appinstall_image); if (videoController.hasVideoContent()) { imageView.setVisibility(View.GONE); adView.setMediaView(sampleMediaView); } else { sampleMediaView.setVisibility(View.GONE); adView.setImageView(imageView); List<NativeAd.Image> images = nativeAppInstallAd.getImages(); if (images.size() > 0) { ((ImageView) adView.getImageView()).setImageDrawable(images.get(0).getDrawable()); } } // Some aren't guaranteed, however, and should be checked. if (nativeAppInstallAd.getPrice() == null) { adView.getPriceView().setVisibility(View.INVISIBLE); } else { adView.getPriceView().setVisibility(View.VISIBLE); ((TextView) adView.getPriceView()).setText(nativeAppInstallAd.getPrice()); } if (nativeAppInstallAd.getStore() == null) { adView.getStoreView().setVisibility(View.INVISIBLE); } else { adView.getStoreView().setVisibility(View.VISIBLE); ((TextView) adView.getStoreView()).setText(nativeAppInstallAd.getStore()); } if (nativeAppInstallAd.getStarRating() == null) { adView.getStarRatingView().setVisibility(View.INVISIBLE); } else { ((RatingBar) adView.getStarRatingView()) .setRating(nativeAppInstallAd.getStarRating().floatValue()); adView.getStarRatingView().setVisibility(View.VISIBLE); } // Handle the fact that this could be a Sample SDK native ad, which includes a // "degree of awesomeness" field. Bundle extras = nativeAppInstallAd.getExtras(); if (extras.containsKey(SampleCustomEvent.DEGREE_OF_AWESOMENESS)) { TextView degree = (TextView) adView.findViewById(R.id.appinstall_degreeofawesomeness); degree.setVisibility(View.VISIBLE); degree.setText(extras.getString(SampleCustomEvent.DEGREE_OF_AWESOMENESS)); } }
/** * Populates a {@link NativeContentAdView} object with data from a given * {@link NativeContentAd}. * * @param nativeContentAd the object containing the ad's assets * @param adView the view to be populated */ private void populateContentAdView(NativeContentAd nativeContentAd, NativeContentAdView adView) { adView.setHeadlineView(adView.findViewById(R.id.contentad_headline)); adView.setBodyView(adView.findViewById(R.id.contentad_body)); adView.setCallToActionView(adView.findViewById(R.id.contentad_call_to_action)); adView.setLogoView(adView.findViewById(R.id.contentad_logo)); adView.setAdvertiserView(adView.findViewById(R.id.contentad_advertiser)); // Some assets are guaranteed to be in every NativeContentAd. ((TextView) adView.getHeadlineView()).setText(nativeContentAd.getHeadline()); ((TextView) adView.getBodyView()).setText(nativeContentAd.getBody()); ((TextView) adView.getCallToActionView()).setText(nativeContentAd.getCallToAction()); ((TextView) adView.getAdvertiserView()).setText(nativeContentAd.getAdvertiser()); // Get the video controller for the ad. One will always be provided, even if the ad doesn't // have a video asset. VideoController vc = nativeContentAd.getVideoController(); MediaView mediaView = adView.findViewById(R.id.contentad_media); ImageView mainImageView = adView.findViewById(R.id.contentad_image); // Apps can check the VideoController's hasVideoContent property to determine if the // NativeContentAd has a video asset. if (vc.hasVideoContent()) { mainImageView.setVisibility(View.GONE); adView.setMediaView(mediaView); mVideoStatus.setText(String.format(Locale.getDefault(), "Video status: Ad contains a %.2f:1 video asset.", vc.getAspectRatio())); // Create a new VideoLifecycleCallbacks object and pass it to the VideoController. The // VideoController will call methods on this object when events occur in the video // lifecycle. vc.setVideoLifecycleCallbacks(new VideoController.VideoLifecycleCallbacks() { public void onVideoEnd() { // Publishers should allow native ads to complete video playback before // refreshing or replacing them with another ad in the same UI location. mRefresh.setEnabled(true); mVideoStatus.setText("Video status: Video playback has ended."); super.onVideoEnd(); } }); } else { mediaView.setVisibility(View.GONE); adView.setImageView(mainImageView); // At least one image is guaranteed. List<NativeAd.Image> images = nativeContentAd.getImages(); mainImageView.setImageDrawable(images.get(0).getDrawable()); mRefresh.setEnabled(true); mVideoStatus.setText("Video status: Ad does not contain a video asset."); } // These assets aren't guaranteed to be in every NativeContentAd, so it's important to // check before trying to display them. NativeAd.Image logoImage = nativeContentAd.getLogo(); if (logoImage == null) { adView.getLogoView().setVisibility(View.INVISIBLE); } else { ((ImageView) adView.getLogoView()).setImageDrawable(logoImage.getDrawable()); adView.getLogoView().setVisibility(View.VISIBLE); } // Assign native ad object to the native view. adView.setNativeAd(nativeContentAd); }
/** * Populates a {@link View} object with data from a {@link NativeCustomTemplateAd}. This method * handles a particular "simple" custom native ad format. * * @param nativeCustomTemplateAd the object containing the ad's assets * @param adView the view to be populated */ private void populateSimpleTemplateAdView(final NativeCustomTemplateAd nativeCustomTemplateAd, View adView) { TextView headline = adView.findViewById(R.id.simplecustom_headline); TextView caption = adView.findViewById(R.id.simplecustom_caption); headline.setText(nativeCustomTemplateAd.getText("Headline")); caption.setText(nativeCustomTemplateAd.getText("Caption")); FrameLayout mediaPlaceholder = adView.findViewById(R.id.simplecustom_media_placeholder); // Get the video controller for the ad. One will always be provided, even if the ad doesn't // have a video asset. VideoController vc = nativeCustomTemplateAd.getVideoController(); // Apps can check the VideoController's hasVideoContent property to determine if the // NativeCustomTemplateAd has a video asset. if (vc.hasVideoContent()) { mediaPlaceholder.addView(nativeCustomTemplateAd.getVideoMediaView()); mVideoStatus.setText(String.format(Locale.getDefault(), "Video status: Ad contains a %.2f:1 video asset.", vc.getAspectRatio())); // Create a new VideoLifecycleCallbacks object and pass it to the VideoController. The // VideoController will call methods on this object when events occur in the video // lifecycle. vc.setVideoLifecycleCallbacks(new VideoController.VideoLifecycleCallbacks() { public void onVideoEnd() { // Publishers should allow native ads to complete video playback before // refreshing or replacing them with another ad in the same UI location. mRefresh.setEnabled(true); mVideoStatus.setText("Video status: Video playback has ended."); super.onVideoEnd(); } }); } else { ImageView mainImage = new ImageView(this); mainImage.setAdjustViewBounds(true); mainImage.setImageDrawable(nativeCustomTemplateAd.getImage("MainImage").getDrawable()); mainImage.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { nativeCustomTemplateAd.performClick("MainImage"); } }); mediaPlaceholder.addView(mainImage); mRefresh.setEnabled(true); mVideoStatus.setText("Video status: Ad does not contain a video asset."); } }