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 に答える