2016-11-23 9 views
1

私は自分のアプリケーション(私はAndroidスタジオを使用しています)にheyZapメディエーションを実装しました。ネイティブ広告を表示しようとすると、適切な方法が見つからず、広告をビューにバインドせずに表示する方法しかありませんでした。heyZapネイティブメディエーション

クライアント側のビューにバインドせずにネイティブ広告をサポートするにはどうすればよいですか?

ありがとうございます。 Erez。

答えて

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ネイティブ広告では他のネットワークとは異なる方法でネットワークに登録されます。

関連する問題