Java 类com.facebook.ads.NativeAd 实例源码

项目:sekai    文件:NativeAdHscrollActivity.java   
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.hscroll);

    manager = new NativeAdsManager(this, "YOUR_PLACEMENT_ID", 5);
    manager.setListener(this);
    manager.loadAds(NativeAd.MediaCacheFlag.ALL);

    inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    reloadButton = (Button)findViewById(R.id.reloadHscroll);
    reloadButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            manager.loadAds();
        }
    });
}
项目:aptoide-client    文件:TimelineAdapter.java   
public synchronized void addNativeAd(NativeAd ad, Context context, ArrayList<TimelineListAPKsJson.UserApk> list) {
    if (ad == null) {
        Log.d("pois", "addNativeAd , was null");
        return;
    }
    if (this.ad != null) {
        Log.d("pois","addNativeAd , was old");
        // Clean up the old ad before inserting the new one
        this.ad.unregisterView();
        list.remove(AD_INDEX);
        this.ad = null;
        this.notifyDataSetChanged();
    }
    Log.d("pois","addNativeAd , adding native ad");
    this.ad = ad;
    adView = mInflater.inflate(R.layout.row_timeline_ad_unit, null);
    TimelineListAPKsJson.UserApk ret = new UserAPKAd(ad);
    inflateAd(ad, adView, context);
    list.add(AD_INDEX, ret);
    Log.d("pois","addNativeAd , position on "+AD_INDEX);
    this.notifyDataSetChanged();
}
项目:pubnative-android-sdk    文件:FacebookNetworkAdapter.java   
@Override
protected void request(Context context, Map<String, String> networkData) {
    if (context == null || networkData == null) {
        invokeLoadFail(PNException.ADAPTER_MISSING_DATA);
    } else {
        mContext = context;
        String placementId = networkData.get(Facebook.KEY_PLACEMENT_ID);
        if (TextUtils.isEmpty(placementId)) {
            invokeLoadFail(PNException.ADAPTER_ILLEGAL_ARGUMENTS);
        } else {
            mAdView = null;
            mWrapper = null;

            Facebook.init(context);
            mNativeAd = new NativeAd(context, placementId);
            mNativeAd.setAdListener(this);
            mNativeAd.loadAd();
        }
    }
}
项目:pubnative-android-sdk    文件:FacebookNetworkAdapter.java   
@Override
protected void request(Context context, Map<String, String> networkData) {
    if (context == null || networkData == null) {
        invokeLoadFail(PNException.ADAPTER_MISSING_DATA);
    } else {
        mContext = context;
        String placementId = networkData.get(Facebook.KEY_PLACEMENT_ID);
        if (TextUtils.isEmpty(placementId)) {
            invokeLoadFail(PNException.ADAPTER_ILLEGAL_ARGUMENTS);
        } else {
            Facebook.init(context);
            mAdView = null;
            mWrapper = null;
            mNativeAd = new NativeAd(context, placementId);
            mNativeAd.setAdListener(this);
            mNativeAd.loadAd();
        }
    }
}
项目:pubnative-android-sdk    文件:FacebookNetworkAdapter.java   
@Override
protected void request(Context context, Map<String, String> networkData) {
    if (context == null || networkData == null) {
        invokeLoadFail(PNException.ADAPTER_MISSING_DATA);
    } else {
        mContext = context;
        String placementId = networkData.get(Facebook.KEY_PLACEMENT_ID);
        if (TextUtils.isEmpty(placementId)) {
            invokeLoadFail(PNException.ADAPTER_ILLEGAL_ARGUMENTS);
        } else {
            Facebook.init(context);
            mAdView = null;
            mWrapper = null;
            mNativeAd = new NativeAd(context, placementId);
            mNativeAd.setAdListener(this);
            mNativeAd.loadAd();
        }
    }
}
项目:Sportmix    文件:PostItemAdapter.java   
public synchronized void addNativeAd(NativeAd ad) {
    if (ad == null) {
        return;

    }
    if (this.ad != null) {
        // Clean up the old ad before inserting the new one
        this.ad.unregisterView();
        this.datas.remove(AD_INDEX);
        this.ad = null;
        this.notifyDataSetChanged();
    }
    this.ad = ad;
    View adView = inflater.inflate(R.layout.ad_list, null);
    inflateAd(ad, adView);
    datas.add(AD_INDEX, adView);
    this.notifyDataSetChanged();
}
项目:HereAStory-Android    文件:NativeAdListActivity.java   
public synchronized void addNativeAd(NativeAd ad) {
    if (ad == null) {
        return;
    }
    if (this.ad != null) {
        // Clean up the old ad before inserting the new one
        this.ad.unregisterView();
        this.list.remove(AD_INDEX);
        this.ad = null;
        this.notifyDataSetChanged();
    }
    this.ad = ad;
    View adView = inflater.inflate(R.layout.ad_unit, null);
    NativeAdSampleActivity.inflateAd(ad, adView, NativeAdListActivity.this);
    list.add(AD_INDEX, adView);
    this.notifyDataSetChanged();
}
项目:aptoide-client    文件:FragmentSocialTimeline.java   
@Override
public void onAdLoaded(Ad ad) {
    Logger.d("FragmentSocialTimeline","onAdLoaded , ad: "+ad.toString());
    if(getActivity() != null){
        adapter.addNativeAd((NativeAd) ad,getActivity(),apks);
    }

}
项目:mopub-nativead-adapters    文件:FacebookNativeAd.java   
@Override
public void onAdLoaded(final Ad ad) {
    // This identity check is from Facebook's Native API sample code:
    // https://developers.facebook.com/docs/audience-network/android/native-api
    if (!this.nativeAd.equals(ad) || !this.nativeAd.isAdLoaded()) {
        this.getCustomEventNativeListener().onNativeAdFailed(NativeErrorCode.NETWORK_INVALID_STATE);
        return;
    }

    this.setTitle(this.nativeAd.getAdTitle());
    this.setText(this.nativeAd.getAdBody());

    final NativeAd.Image coverImage = this.nativeAd.getAdCoverImage();
    if (coverImage != null) this.setMainImageUrl(coverImage.getUrl());

    final NativeAd.Image icon = this.nativeAd.getAdIcon();
    if (icon != null) this.setIconImageUrl(icon.getUrl());

    this.setCallToAction(this.nativeAd.getAdCallToAction());
    this.setStarRating(FacebookNativeAd.getStarRatingValue(this.nativeAd.getAdStarRating()));

    this.addExtra(FacebookNativeAd.SOCIAL_CONTEXT_FOR_AD, this.nativeAd.getAdSocialContext());

    final NativeAd.Image adChoicesIconImage = this.nativeAd.getAdChoicesIcon();
    if (adChoicesIconImage != null) {
        this.setPrivacyInformationIconImageUrl(adChoicesIconImage.getUrl());
        this.setPrivacyInformationIconClickThroughUrl(this.nativeAd.getAdChoicesLinkUrl());
    }

    this.preCacheImages(this.getImageUrls());

    this.setImpressionMinTimeViewed(BaseStaticNativeAd.IMPRESSION_MIN_TIME);
}
项目:pubnative-android-sdk    文件:FacebookNativeAdModel.java   
public FacebookNativeAdModel(Context context, NativeAd nativeAd) {
    super(context);
    if (nativeAd != null) {
        mNativeAd = nativeAd;
        mNativeAd.setAdListener(this);
    }
}
项目:pubnative-android-sdk    文件:FacebookNativeAdModel.java   
@Override
public float getStarRating() {
    float starRating = 0;
    if (mNativeAd != null) {
        NativeAd.Rating rating = mNativeAd.getAdStarRating();
        if (rating != null) {
            double ratingScale = rating.getScale();
            double ratingValue = rating.getValue();
            starRating = (float) ((ratingValue / ratingScale) * 5);
        }
    }
    return starRating;
}
项目:pubnative-android-sdk    文件:FacebookNetworkAdapter.java   
@Override
public void onAdLoaded(Ad ad) {
    if (ad == mNativeAd) {
        FacebookNativeAdModel wrapModel = new FacebookNativeAdModel(mContext, (NativeAd) ad);
        wrapModel.setInsightModel(mInsight);
        invokeLoadFinish(wrapModel);
    }
}
项目:react-native-fbads    文件:NativeAdView.java   
/**
 * Called by the view manager when adsManager prop is set. Sends serialised
 * version of a native ad back to Javascript.
 *
 * Automatically registers current view for interactions on an ad so that
 * it becomes clickable.
 *
 * @param nativeAd
 */
public void setNativeAd(NativeAd nativeAd) {
    mNativeAd = nativeAd;

    if (nativeAd == null) {
        mEventEmitter.receiveEvent(getId(), "onAdLoaded", null);
        return;
    }

    NativeAd.Image coverImage = nativeAd.getAdCoverImage();
    NativeAd.Image iconImage = nativeAd.getAdIcon();

    WritableMap event = Arguments.createMap();
    event.putString("title", nativeAd.getAdTitle());
    event.putString("subtitle", nativeAd.getAdSubtitle());
    event.putString("description", nativeAd.getAdBody());
    event.putString("callToActionText", nativeAd.getAdCallToAction());

    // Check as they might be null because of memory issues on low-end devices
    if (coverImage != null) {
        event.putString("coverImage", coverImage.getUrl());
    }

    if (iconImage != null) {
        event.putString("icon", iconImage.getUrl());
    }

    mEventEmitter.receiveEvent(getId(), "onAdLoaded", event);

    mNativeAd.registerViewForInteraction(this);
}
项目:adsnative-sdk-android    文件:FacebookAdNetwork.java   
FacebookNativeAd(final Context context,
                 final NativeAd nativeAd,
                 final CustomEventListener customEventListener) {
    mContext = context.getApplicationContext();
    mFbNativeAd = nativeAd;
    mCustomEventListener = customEventListener;
}
项目:adsnative-sdk-android    文件:FacebookAdNetwork.java   
FacebookNativeAd(final Context context,
                 final NativeAd nativeAd,
                 final CustomEventListener customEventListener) {
    mContext = context.getApplicationContext();
    mFbNativeAd = nativeAd;
    mCustomEventListener = customEventListener;
}
项目:googleads-mobile-android-mediation    文件:FacebookAdapter.java   
@Override
public void requestNativeAd(Context context,
                            MediationNativeListener listener,
                            Bundle serverParameters,
                            NativeMediationAdRequest mediationAdRequest,
                            Bundle mediationExtras) {
    mNativeListener = listener;
    if (!isValidRequestParameters(context, serverParameters)) {
        mNativeListener.onAdFailedToLoad(this, AdRequest.ERROR_CODE_INVALID_REQUEST);
        return;
    }

    // Verify that the request is for both app install and content ads.
    if (!(mediationAdRequest.isAppInstallAdRequested()
            && mediationAdRequest.isContentAdRequested())) {
        Log.w(TAG, "Failed to request native ad. Both app install and content ad should be "
                + "requested");
        mNativeListener.onAdFailedToLoad(this, AdRequest.ERROR_CODE_INVALID_REQUEST);
        return;
    }

    String placementId = serverParameters.getString(PLACEMENT_PARAMETER);

    // Get the optional extras if set by the publisher.
    if (mediationExtras != null) {
        mIsAdChoicesIconExpandable = mediationExtras.getBoolean(
                FacebookExtrasBundleBuilder.KEY_EXPANDABLE_ICON, true);
    }

    mMediaView = new MediaView(context);

    mNativeAd = new NativeAd(context, placementId);
    mNativeAd.setAdListener(new NativeListener(mNativeAd, mediationAdRequest));
    buildAdRequest(mediationAdRequest);
    mNativeAd.loadAd();
}
项目:googleads-mobile-android-mediation    文件:FacebookAdapter.java   
/**
 * Convert rating to a scale of 1 to 5.
 */
private Double getRating(NativeAd.Rating rating) {
    if (rating == null) {
        return null;
    }
    return (MAX_STAR_RATING * rating.getValue()) / rating.getScale();
}
项目:Sportmix    文件:FotomacFragment.java   
@Override
public void onAdsLoaded() {

    //This method will download ad from FB
    //& call addNativeAd method which will add ad to listview

    NativeAd ad = this.listNativeAdsManager.nextNativeAd();
    ad.setAdListener(this);
    if (itemAdapter!=null)
        itemAdapter.addNativeAd(ad);   //postitemadaptere tanımlıyoruz.
}
项目:Sportmix    文件:TrtsporFragment.java   
@Override
public void onAdsLoaded() {

    //This method will download ad from FB
    //& call addNativeAd method which will add ad to listview

    NativeAd ad = this.listNativeAdsManager.nextNativeAd();
    ad.setAdListener(this);
    if (itemAdapter!=null)
        itemAdapter.addNativeAd(ad);   //postitemadaptere tanımlıyoruz.
}
项目:Sportmix    文件:SporxFragment.java   
@Override
public void onAdsLoaded() {

    //This method will download ad from FB
    //& call addNativeAd method which will add ad to listview

    NativeAd ad = this.listNativeAdsManager.nextNativeAd();
    ad.setAdListener(this);
    if (itemAdapter!=null)
    itemAdapter.addNativeAd(ad);   //postitemadaptere tanımlıyoruz.
}
项目:Sportmix    文件:SabahFragment.java   
@Override
public void onAdsLoaded() {

    //This method will download ad from FB
    //& call addNativeAd method which will add ad to listview

    NativeAd ad = this.listNativeAdsManager.nextNativeAd();
    ad.setAdListener(this);
    if (itemAdapter!=null)
        itemAdapter.addNativeAd(ad);   //postitemadaptere tanımlıyoruz.
}
项目:Sportmix    文件:EuroSportFragment.java   
@Override
public void onAdsLoaded() {

    //This method will download ad from FB
    //& call addNativeAd method which will add ad to listview

    NativeAd ad = this.listNativeAdsManager.nextNativeAd();
    ad.setAdListener(this);
    if (itemAdapter!=null)
        itemAdapter.addNativeAd(ad);   //postitemadaptere tanımlıyoruz.
}
项目:Sportmix    文件:LigTvFragment.java   
@Override
public void onAdsLoaded() {

    //This method will download ad from FB
    //& call addNativeAd method which will add ad to listview

    NativeAd ad = this.listNativeAdsManager.nextNativeAd();
    ad.setAdListener(this);
    if (itemAdapter!=null)
        itemAdapter.addNativeAd(ad);   //postitemadaptere tanımlıyoruz.
}
项目:Sportmix    文件:GoalComFragment.java   
@Override
public void onAdsLoaded() {

    //This method will download ad from FB
    //& call addNativeAd method which will add ad to listview

    NativeAd ad = this.listNativeAdsManager.nextNativeAd();
    ad.setAdListener(this);
    if (itemAdapter!=null)
        itemAdapter.addNativeAd(ad);   //postitemadaptere tanımlıyoruz.
}
项目:Sportmix    文件:HaberTurkFragment.java   
@Override
public void onAdsLoaded() {

    //This method will download ad from FB
    //& call addNativeAd method which will add ad to listview

    NativeAd ad = this.listNativeAdsManager.nextNativeAd();
    ad.setAdListener(this);
    if (itemAdapter!=null)
        itemAdapter.addNativeAd(ad);   //postitemadaptere tanımlıyoruz.
}
项目:Sportmix    文件:PostItemAdapter.java   
private void inflateAd(NativeAd nativeAd, View adView) {
    // Create native UI using the ad metadata.
    ImageView nativeAdIcon = (ImageView) adView.findViewById(R.id.native_ad_icon);
    TextView nativeAdTitle = (TextView) adView.findViewById(R.id.native_ad_title);
    TextView nativeAdBody = (TextView) adView.findViewById(R.id.native_ad_body);
    MediaView nativeAdMedia = (MediaView) adView.findViewById(R.id.native_ad_media);
    nativeAdMedia.setAutoplay(AdSettings.isVideoAutoplay());
    TextView nativeAdSocialContext =
            (TextView) adView.findViewById(R.id.native_ad_social_context);
    Button nativeAdCallToAction = (Button) adView.findViewById(R.id.native_ad_call_to_action);

    // Setting the Text
    nativeAdSocialContext.setText(nativeAd.getAdSocialContext());
    nativeAdCallToAction.setText(nativeAd.getAdCallToAction());
    nativeAdCallToAction.setVisibility(View.VISIBLE);
    nativeAdTitle.setText(nativeAd.getAdTitle());
    nativeAdBody.setText(nativeAd.getAdBody());

    // Downloading and setting the ad icon.
    NativeAd.Image adIcon = nativeAd.getAdIcon();
    NativeAd.downloadAndDisplayImage(adIcon, nativeAdIcon);

    // Downloading and setting the cover image.
    NativeAd.Image adCoverImage = nativeAd.getAdCoverImage();
    int bannerWidth = adCoverImage.getWidth();
    int bannerHeight = adCoverImage.getHeight();
    DisplayMetrics metrics = myContext.getResources().getDisplayMetrics();
    int mediaWidth = adView.getWidth() > 0 ? adView.getWidth() : metrics.widthPixels;
    nativeAdMedia.setLayoutParams(new LinearLayout.LayoutParams(
            mediaWidth,
            Math.min(
                    (int) (((double) mediaWidth / (double) bannerWidth) * bannerHeight),
                    metrics.heightPixels / 7))); // eskiden 3
    nativeAdMedia.setNativeAd(nativeAd);

    // Wire up the View with the native ad, the whole nativeAdContainer will be clickable.
    nativeAd.registerViewForInteraction(adView);
}
项目:HereAStory-Android    文件:NativeAdListActivity.java   
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    listNativeAd = new NativeAd(this, "YOUR_PLACEMENT_ID");
    listNativeAd.setAdListener(this);
    listNativeAd.loadAd();

    listView = getListView();
    adapter = new ListViewAdapter(getApplicationContext());
    listView.setAdapter(adapter);
}
项目:mobile-sdk-android    文件:FBNativeAdResponse.java   
public FBNativeAdResponse(NativeAd ad) {
    this.nativeAd = ad;
    runnable = new Runnable() {
        @Override
        public void run() {
            if (coverImage != null) {
                coverImage.recycle();
                coverImage = null;
            }
            if (icon != null) {
                icon.recycle();
                icon = null;
            }
            listener = null;
            expired = true;
            if (nativeAd != null) {
                nativeAd.setAdListener(null);
                nativeAd.destroy();
                nativeAd = null;
            }
            if(nativeElements != null && !nativeElements.isEmpty()){
                nativeElements.clear();
            }
        }
    };
    fbNativeExpireHandler = new Handler(Looper.getMainLooper());
    fbNativeExpireHandler.postDelayed(runnable, Settings.NATIVE_AD_RESPONSE_EXPIRATION_TIME);
}
项目:aptoide-client    文件:TimelineAdapter.java   
public void inflateAd(NativeAd nativeAd, View adView, Context context) {
    // Create native UI using the ad metadata.

    ImageView nativeAdIcon = (ImageView) adView.findViewById(R.id.nativeAdIcon);
    TextView nativeAdTitle = (TextView) adView.findViewById(R.id.nativeAdTitle);
    TextView nativeAdTitleRating = (TextView) adView.findViewById(R.id.nativeAdTitleRating);
    TextView nativeAdBody = (TextView) adView.findViewById(R.id.nativeAdBody);

    ImageView nativeAdImage = (ImageView) adView.findViewById(R.id.nativeAdImage);
    TextView nativeAdSocialContext = (TextView) adView.findViewById(R.id.nativeAdSocialContext);
    FaceBookAdButton nativeAdCallToAction = (FaceBookAdButton) adView.findViewById(R.id.nativeAdCallToAction);
    RatingBar nativeAdStarRating = (RatingBar) adView.findViewById(R.id.nativeAdStarRating);

    // Setting the Text
    nativeAdSocialContext.setText(nativeAd.getAdSocialContext());
    nativeAdCallToAction.setText(nativeAd.getAdCallToAction());
    nativeAdTitle.setText(nativeAd.getAdTitle());
    nativeAdTitleRating.setText(nativeAd.getAdTitle());
    nativeAdBody.setText(nativeAd.getAdBody());

    // Downloading and setting the ad icon.
    NativeAd.Image adIcon = nativeAd.getAdIcon();
    NativeAd.downloadAndDisplayImage(adIcon, nativeAdIcon);

    // Downloading and setting the cover image.
    NativeAd.Image adCoverImage = nativeAd.getAdCoverImage();
    int bannerWidth = adCoverImage.getWidth();
    int bannerHeight = adCoverImage.getHeight();
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);
    int screenWidth = metrics.widthPixels;
    int screenHeight = metrics.heightPixels;
    nativeAdImage.setLayoutParams(new LinearLayout.LayoutParams(
            screenWidth,
            Math.min((int) (((double) screenWidth / (double) bannerWidth) * bannerHeight), screenHeight / 3)
    ));
    NativeAd.downloadAndDisplayImage(adCoverImage, nativeAdImage);

    NativeAd.Rating rating = nativeAd.getAdStarRating();
    if (rating != null) {
        nativeAdStarRating.setVisibility(View.VISIBLE);
        nativeAdStarRating.setNumStars((int) rating.getScale());
        nativeAdStarRating.setRating((float) rating.getValue());
    } else {
        nativeAdStarRating.setVisibility(View.GONE);
    }

    // Wire up the View with the native ad, the whole nativeAdContainer will be clickable
    //nativeAd.registerViewForInteraction(adView);


    // Or you can replace the above call with the following function to specify the clickable areas.
    nativeAd.registerViewForInteraction(adView, Arrays.asList(nativeAdCallToAction));

}
项目:aptoide-client    文件:TimelineAdapter.java   
public UserAPKAd(NativeAd ad){
    this.ad = ad;
}
项目:mopub-nativead-adapters    文件:FacebookNativeAd.java   
public FacebookNativeAd(@NonNull final Context context, @NonNull final CustomEventNative.CustomEventNativeListener customEventNativeListener, @NonNull final String adUnitId) {
    super(context.getApplicationContext(), customEventNativeListener);

    this.nativeAd = new NativeAd(this.getContext(), adUnitId);
}
项目:mopub-nativead-adapters    文件:FacebookNativeAd.java   
@Nullable
private static Double getStarRatingValue(final NativeAd.Rating rating) {
    if (rating == null) return null;

    return BaseStaticNativeAd.MAX_STAR_RATING * rating.getValue() / rating.getScale();
}
项目:pubnative-android-sdk    文件:FacebookNetworkAdapter.java   
@Override
public void onAdLoaded(Ad ad) {
    mWrapper = new FacebookNativeAdModel(mContext, (NativeAd) ad);
    invokeLoadSuccess();
}
项目:pubnative-android-sdk    文件:FacebookNetworkAdapter.java   
@Override
public void onAdLoaded(Ad ad) {
    mWrapper = new FacebookNativeAdModel(mContext, (NativeAd) ad);
    invokeLoadSuccess();
}
项目:pubnative-android-sdk    文件:FacebookNetworkAdapter.java   
protected void createRequest(Context context, String placementId) {
    Facebook.init(context);
    mNativeAd = new NativeAd(context, placementId);
    mNativeAd.setAdListener(this);
    mNativeAd.loadAd();
}
项目:googleads-mobile-android-mediation    文件:FacebookAdapter.java   
private NativeListener(NativeAd nativeAd, NativeMediationAdRequest mediationAdRequest) {
    mNativeAd = nativeAd;
    mMediationAdRequest = mediationAdRequest;
}
项目:HereAStory-Android    文件:NativeAdListActivity.java   
@Override
public void onAdLoaded(Ad ad) {
    adapter.addNativeAd((NativeAd) ad);
}
项目:HereAStory-Android    文件:NativeAdSampleActivity.java   
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_native_ad_demo);

    nativeAdContainer = (LinearLayout) findViewById(R.id.nativeAdContainer);

    LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    adView = inflater.inflate(R.layout.ad_unit, nativeAdContainer);

    nativeAdStatus = (TextView) findViewById(R.id.nativeAdStatus);
    showNativeAdButton = (Button)findViewById(R.id.loadNativeAdButton);
    showNativeAdListButton = (Button)findViewById(R.id.loadNativeAdListButton);

    showNativeAdButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            nativeAdStatus.setText("Requesting an ad...");

            // Create a native ad request with a unique placement ID (generate your own on the Facebook app settings).
            // Use different ID for each ad placement in your app.
            nativeAd = new NativeAd(NativeAdSampleActivity.this, "YOUR_PLACEMENT_ID");

            // Set a listener to get notified when the ad was loaded.
            nativeAd.setAdListener(NativeAdSampleActivity.this);

            // When testing on a device, add its hashed ID to force test ads.
            // The hash ID is printed to log cat when running on a device and loading an ad.
            // AdSettings.addTestDevice("THE HASHED ID AS PRINTED TO LOG CAT");

            // Initiate a request to load an ad.
            nativeAd.loadAd();
        }
    });

    showNativeAdListButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(NativeAdSampleActivity.this, NativeAdListActivity.class);
            NativeAdSampleActivity.this.startActivity(intent);
        }
    });
}
项目:HereAStory-Android    文件:NativeAdSampleActivity.java   
public static void inflateAd(NativeAd nativeAd, View adView, Context context) {
    // Create native UI using the ad metadata.

    ImageView nativeAdIcon = (ImageView) adView.findViewById(R.id.nativeAdIcon);
    TextView nativeAdTitle = (TextView) adView.findViewById(R.id.nativeAdTitle);
    TextView nativeAdBody = (TextView) adView.findViewById(R.id.nativeAdBody);
    ImageView nativeAdImage = (ImageView) adView.findViewById(R.id.nativeAdImage);
    TextView nativeAdSocialContext = (TextView) adView.findViewById(R.id.nativeAdSocialContext);
    Button nativeAdCallToAction = (Button) adView.findViewById(R.id.nativeAdCallToAction);
    RatingBar nativeAdStarRating = (RatingBar) adView.findViewById(R.id.nativeAdStarRating);

    // Setting the Text
    nativeAdSocialContext.setText(nativeAd.getAdSocialContext());
    nativeAdCallToAction.setText(nativeAd.getAdCallToAction());
    nativeAdCallToAction.setVisibility(View.VISIBLE);
    nativeAdTitle.setText(nativeAd.getAdTitle());
    nativeAdBody.setText(nativeAd.getAdBody());

    // Downloading and setting the ad icon.
    NativeAd.Image adIcon = nativeAd.getAdIcon();
    NativeAd.downloadAndDisplayImage(adIcon, nativeAdIcon);

    // Downloading and setting the cover image.
    NativeAd.Image adCoverImage = nativeAd.getAdCoverImage();
    int bannerWidth = adCoverImage.getWidth();
    int bannerHeight = adCoverImage.getHeight();
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);
    int screenWidth = metrics.widthPixels;
    int screenHeight = metrics.heightPixels;
    nativeAdImage.setLayoutParams(new LinearLayout.LayoutParams(
            screenWidth,
            Math.min((int) (((double) screenWidth / (double) bannerWidth) * bannerHeight), screenHeight / 3)
            ));
    NativeAd.downloadAndDisplayImage(adCoverImage, nativeAdImage);

    NativeAd.Rating rating = nativeAd.getAdStarRating();
    if (rating != null) {
        nativeAdStarRating.setVisibility(View.VISIBLE);
        nativeAdStarRating.setNumStars((int) rating.getScale());
        nativeAdStarRating.setRating((float) rating.getValue());
    } else {
        nativeAdStarRating.setVisibility(View.GONE);
    }

    // Wire up the View with the native ad, the whole nativeAdContainer will be clickable
    nativeAd.registerViewForInteraction(adView);

    // Or you can replace the above call with the following function to specify the clickable areas.
    // nativeAd.registerViewForInteraction(nativeAdContainer, Arrays.asList(nativeAdCallToAction, nativeAdImage));
}
项目:mobile-sdk-android    文件:FacebookNativeAd.java   
/**
 * AppNexus SDk calls this method to request a native ad from Facebook
 *
 * @param context The context from which this class is instantiated.
 * @param uid Facebook placement id, app developer needs to set up account with Facebook.
 * @param mBC The controller that passes callbacks to AppNexus SDK
 * @param tp Targeting parameters that were set in AppNexus API.
 * @return AppNexus NativeAdResponse that wraps Facebook ad.
 */

@Override
public void requestNativeAd(Context context, String uid, final MediatedNativeAdController mBC, TargetingParameters tp) {
    NativeAd nativeAd = new NativeAd(context, uid);
    FBNativeAdResponse response = new FBNativeAdResponse(nativeAd);
    nativeAd.setAdListener(new FacebookNativeAdListener(mBC, response));
    nativeAd.loadAd();
}