1

我在我的应用程序上实现了 heyZap 中介(我正在使用 Android Studio)。当我尝试展示原生广告时,我没有找到任何合适的方法——只有在不将广告绑定到视图的情况下展示广告的方法。

他们如何在不绑定到客户端视图的情况下支持原生广告?

谢谢你,埃雷兹。

4

1 回答 1

0

您需要提供自己的视图来展示原生广告。

这是一个例子:

final View nativeAdView = inflater.inflate(R.layout.native_ad, null);

final TextView nativeNetwork = (TextView) nativeAdView.findViewById(R.id.native_network);
final TextView nativeTitle = (TextView) nativeAdView.findViewById(R.id.native_title);
final TextView nativeBody = (TextView) nativeAdView.findViewById(R.id.native_body);

final ImageView nativeIconImage = (ImageView) nativeAdView.findViewById(R.id.native_icon_image);
final ImageView nativeCoverImage = (ImageView) nativeAdView.findViewById(R.id.native_cover_image);

final ImageView nativeAdChoicesImage = (ImageView) nativeAdView.findViewById(R.id.native_ad_choices_image);

nativeAd.setListener(new NativeListener() {

    @Override
    public void onError(NativeError nativeError) {
        addLog("Native onError: " + nativeError.getErrorMessage());
    }

    @Override
    public void onAdLoaded(final NativeAd nativeAd) {
        addLog("Native onAdLoaded: " + nativeAd.getTitle());

        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                nativeAd.registerView(nativeAdView);
                nativeNetwork.setText(nativeAd.getNetwork());
                nativeTitle.setText(nativeAd.getTitle());
                nativeBody.setText(nativeAd.getBody());

                final Image icon = nativeAd.getIcon();

                new DownloadImageTask(nativeIconImage)
                        .execute(icon.getUrl());

                nativeIconImage.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Intent i = new Intent(Intent.ACTION_VIEW);
                        i.setData(Uri.parse(icon.getUrl()));
                        startActivity(i);
                    }
                });

                final Image coverImage = nativeAd.getCoverImage();

                new DownloadImageTask(nativeCoverImage)
                        .execute(coverImage.getUrl());

                nativeCoverImage.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Intent i = new Intent(Intent.ACTION_VIEW);
                        i.setData(Uri.parse(coverImage.getUrl()));
                        startActivity(i);
                    }
                });

                if (nativeAd.getAdChoicesImage() != null) {
                    new DownloadImageTask(nativeAdChoicesImage)
                            .execute(nativeAd.getAdChoicesImage().getUrl());
                    if (nativeAd.getAdChoicesUrl() != null) {
                        nativeAdChoicesImage.setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent i = new Intent(Intent.ACTION_VIEW);
                                i.setData(Uri.parse(nativeAd.getAdChoicesUrl()));
                                startActivity(i);
                            }
                        });
                    }
                }

                nativeDoImpression.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        nativeAd.doImpression();
                    }
                });

                nativeDoClick.setText(nativeAd.getCallToAction());
                nativeDoClick.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        nativeAd.doClick(v);
                    }
                });

                // rootContainer is where the nativeAd should be displayed
                rootContainer.addView(nativeAdView);
            }
        });
    }

    @Override
    public void onAdClicked(NativeAd nativeAd) {
        addLog("Native onAdClicked");
    }

    @Override
    public void onAdShown(NativeAd nativeAd) {
        addLog("Native onAdShown");
    }
});

nativeAd.setAdmobListener(new NativeAd.AdmobListener() {
    @Override
    public void onAppInstallAdLoaded(final NativeAd heyzapNativeAd, final NativeAppInstallAd appInstallAd) {
        final View nativeAdView = inflater.inflate(R.layout.native_ad, null);kd

        final TextView nativeNetwork = (TextView) nativeAdView.findViewById(R.id.native_network);
        final TextView nativeTitle = (TextView) nativeAdView.findViewById(R.id.native_title);
        final TextView nativeBody = (TextView) nativeAdView.findViewById(R.id.native_body);

        final ImageView nativeIconImage = (ImageView) nativeAdView.findViewById(R.id.native_icon_image);
        final ImageView nativeCoverImage = (ImageView) nativeAdView.findViewById(R.id.native_cover_image);

        final TextView nativeDoImpression = (TextView) nativeAdView.findViewById(R.id.native_do_impression);
        final TextView nativeDoClick = (TextView) nativeAdView.findViewById(R.id.native_do_click);
        addLog("Native Admob onAppInstallAdLoaded: " + appInstallAd.getHeadline());

        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                nativeNetwork.setText(HeyzapAds.Network.ADMOB);
                nativeTitle.setText(appInstallAd.getHeadline());
                nativeBody.setText(appInstallAd.getBody());

                final String iconUrl = appInstallAd.getIcon().getUri().toString();
                Logger.log("bbb app install ad icon Url", iconUrl);

                new DownloadImageTask(nativeIconImage)
                        .execute(iconUrl);

                nativeIconImage.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if (iconUrl.equals("")) return;
                        Intent i = new Intent(Intent.ACTION_VIEW);
                        i.setData(Uri.parse(iconUrl));
                        startActivity(i);
                    }
                });

                String ciu = "";

                if (appInstallAd.getImages().size() > 0) {
                    ciu = appInstallAd.getImages().get(0).getUri().toString();
                }

                final String coverImageUrl = ciu;

                new DownloadImageTask(nativeCoverImage)
                        .execute(coverImageUrl);

                nativeCoverImage.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if (coverImageUrl.equals("")) return;
                        Intent i = new Intent(Intent.ACTION_VIEW);
                        i.setData(Uri.parse(coverImageUrl));
                        startActivity(i);
                    }
                });

                nativeDoImpression.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        heyzapNativeAd.doImpression();
                    }
                });

                nativeDoClick.setText(appInstallAd.getCallToAction());

                NativeAppInstallAdView wrapper = new NativeAppInstallAdView(rootView.getContext());
                wrapper.setCallToActionView(nativeDoClick);
                wrapper.addView(nativeAdView);

                wrapper.setNativeAd(appInstallAd);

                Logger.log("bbb Admob AppInstallAd add view to root container");
                rootContainer.removeAllViews();
                rootContainer.addView(wrapper);
            }
        });
    }

    @Override
    public void onContentAdLoaded(final NativeAd heyzapNativeAd, final NativeContentAd contentAd) {
        final View nativeAdView = inflater.inflate(R.layout.native_ad, null);

        final TextView nativeNetwork = (TextView) nativeAdView.findViewById(R.id.native_network);
        final TextView nativeTitle = (TextView) nativeAdView.findViewById(R.id.native_title);
        final TextView nativeBody = (TextView) nativeAdView.findViewById(R.id.native_body);

        final ImageView nativeIconImage = (ImageView) nativeAdView.findViewById(R.id.native_icon_image);
        final ImageView nativeCoverImage = (ImageView) nativeAdView.findViewById(R.id.native_cover_image);

        final TextView nativeDoImpression = (TextView) nativeAdView.findViewById(R.id.native_do_impression);
        final TextView nativeDoClick = (TextView) nativeAdView.findViewById(R.id.native_do_click);
        addLog("Native Admob onAppInstallAdLoaded: " + contentAd.getHeadline());

        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                nativeNetwork.setText(HeyzapAds.Network.ADMOB);
                nativeTitle.setText(contentAd.getHeadline());
                nativeBody.setText(contentAd.getBody());

                String iu = "";

                if (contentAd.getLogo() != null) {
                    iu = contentAd.getLogo().getUri().toString();
                }

                final String iconUrl = iu;

                new DownloadImageTask(nativeIconImage)
                        .execute(iconUrl);

                nativeIconImage.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if (iconUrl.equals("")) return;
                        Intent i = new Intent(Intent.ACTION_VIEW);
                        i.setData(Uri.parse(iconUrl));
                        startActivity(i);
                    }
                });

                String ciu = "";

                if (contentAd.getImages().size() > 0) {
                    ciu = contentAd.getImages().get(0).getUri().toString();
                }

                final String coverImageUrl = ciu;

                new DownloadImageTask(nativeCoverImage)
                        .execute(coverImageUrl);

                nativeCoverImage.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if (coverImageUrl.equals("")) return;
                        Intent i = new Intent(Intent.ACTION_VIEW);
                        i.setData(Uri.parse(coverImageUrl));
                        startActivity(i);
                    }
                });

                nativeDoImpression.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        heyzapNativeAd.doImpression();
                    }
                });

                nativeDoClick.setText(contentAd.getCallToAction());

                NativeContentAdView wrapper = new NativeContentAdView(rootView.getContext());
                wrapper.setCallToActionView(nativeDoClick);
                wrapper.addView(nativeAdView);

                wrapper.setNativeAd(contentAd);

                rootContainer.removeAllViews();
                rootContainer.addView(wrapper);
            }
        });
    }
});

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;

    public DownloadImageTask(ImageView bmImage) {
        this.bmImage = bmImage;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
    }
}

您需要rootContainer自己提供。AdMob 原生广告的视图注册到网络的方式与其他网络不同。

于 2016-11-28T22:34:38.610 回答