【问题标题】:Implementing native ads in android using Admob? is it possible?使用 Admob 在 android 中实现原生广告?可能吗?
【发布时间】:2015-05-27 17:37:10
【问题描述】:

我正在尝试在我的 Android 应用程序中实现原生广告。但我只想使用 admob 来做到这一点。我搜索了很多解决方案,但找不到确切的解决方案。

我知道可以使用MoPub

我想做的是: 在列表项中显示广告,这意味着ListView/RecyclerView 项之一可以是一个广告,如下图所示。

我找到了一些链接和参考资料,但这并不能解释原生广告的正确实施。

Link 1:原生广告概览

Link 2:DFP Android 指南 > 定位

Link 3:DFP 快速入门指南

如果使用 admob 无法做到这一点,MoPub 是我目前最好的解决方案。

任何帮助和指导都会有所帮助。谢谢。

【问题讨论】:

  • the official docs 有什么问题?
  • @TimCastelijns:这没什么问题,但我无法使用 admob 实现原生广告。你能分享任何例子吗?怎么办?
  • 目前处于测试阶段。您可以自己实现它,但不能发布应用程序,因为您无法为您的应用程序获取生产广告 ID。希望他们能在本月或 10 月发布。
  • 2016 年 1 月,仍未发布......他们甚至没有给出发布日期。
  • 阅读这篇文章-developine.com/integrate-firebase-advance-native-admob-ads-android-kotlin-tutorial/

标签: android admob native ads mopub


【解决方案1】:

最近我遇到了同样的问题。然后我决定将我的解决方案发布到admobadapter。希望对你有帮助。

基本用法可能如下所示:

    ListView lvMessages;
    AdmobAdapterWrapper adapterWrapper;    

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initListViewItems();
    }

    /**
     * Inits an adapter with items, wrapping your adapter with a {@link AdmobAdapterWrapper} and setting the listview to this wrapper
     * FIRST OF ALL Please notice that the following code will work on a real devices but emulator!
     */
    private void initListViewItems() {
        lvMessages = (ListView) findViewById(R.id.lvMessages);

        //creating your adapter, it could be a custom adapter as well
        ArrayAdapter<String> adapter  = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1);

        adapterWrapper = new AdmobAdapterWrapper(this);
        adapterWrapper.setAdapter(adapter); //wrapping your adapter with a AdmobAdapterWrapper.
        //here you can use the following string to set your custom layouts for a different types of native ads
        //adapterWrapper.setInstallAdsLayoutId(R.layout.your_installad_layout);
        //adapterWrapper.setcontentAdsLayoutId(R.layout.your_installad_layout);

        //Sets the max count of ad blocks per dataset, by default it equals to 3 (according to the Admob's policies and rules)
        adapterWrapper.setLimitOfAds(3);

        //Sets the number of your data items between ad blocks, by default it equals to 10.
        //You should set it according to the Admob's policies and rules which says not to
        //display more than one ad block at the visible part of the screen,
        // so you should choose this parameter carefully and according to your item's height and screen resolution of a target devices
        adapterWrapper.setNoOfDataBetweenAds(10);

        //It's a test admob ID. Please replace it with a real one only when you will be ready to deploy your product to the Release!
        //Otherwise your Admob account could be banned
        //String admobUnitId = getResources().getString(R.string.banner_admob_unit_id);
        //adapterWrapper.setAdmobReleaseUnitId(admobUnitId);

        lvMessages.setAdapter(adapterWrapper); // setting an AdmobAdapterWrapper to a ListView

        //preparing the collection of data
        final String sItem = "item #";
        ArrayList<String> lst = new ArrayList<String>(100);
        for(int i=1;i<=100;i++)
            lst.add(sItem.concat(Integer.toString(i)));

        //adding a collection of data to your adapter and rising the data set changed event
        adapter.addAll(lst);
        adapter.notifyDataSetChanged();
    }

结果将如下所示

【讨论】:

  • 真的很简单很好的adpater,但我一般有一个关于admob原生广告的问题。在我创建广告的 admob 中,我只有横幅和星际选项。我如何请求原生广告?
  • @TadejVengust,嗨,谢谢。实际上,没有办法从 Google Ads 服务器请求真正的原生广告,只能进行测试。原生广告功能仍处于测试版状态。目前,您可以使用 Google Ads 文档中保留的 admob 单元 id 'ca-app-pub-3940256099942544/2247696110' 来请求它。请参考git repo 获取工作示例...
  • 感谢您的回答。所以基本上我不能使用然后我发布的应用程序,直到它们完成 beta 测试?另一方面 - 如果我正确阅读了您的帖子,admobadapter 是您的解决方案吗?我发布了一个功能的 git 请求,如果您有时间看一下,它会真正帮助我吗?
  • @TadejVengust 欢迎您!是的,我想我们已经就这个功能进行了对话。你是莫曼,对吧?然后我们可以继续讨论您的问题:)
  • 请不要从其他线程复制您的答案。请改为链接。
【解决方案2】:

尝试使用其他提供不同类型原生广告的广告网络。开发人员可以自定义广告的放置位置和使用位置。例如:如果您需要每 15 行在第二个单元格放置广告,您可以这样使用。

Avocarrot 提供了这一点。

 AvocarrotInstream myAd = new AvocarrotInstream(<yourListAdapter>);
  myAd.initWithKey( "<your API Key>" );
  myAd.setSandbox(true);
  myAd.setLogger(true ,"ALL"); 

// Populate with In-Stream ads
 myAd.loadAdForPlacement(this,  "<your Placement Name>" );
// Bind the adapter to your list view component
<yourListView>.setAdapter(myAd);// here you are integrating ads to listview
 myAd.setFrequency(2,15); // every 15 cells starting from the 2nd cell. 

这里是Documentation,它提供列表广告和Feed广告。

【讨论】:

    【解决方案3】:

    原生广告与其他 DFP/AdMob 广告一起包含在 Google Play 服务中。确保您在 build.gradle 中将以下列为依赖项(请注意,截至本文发布时,7.5.0 是最高版本)。

    compile "com.google.android.gms:play-services-base:7.5.0"
    compile "com.google.android.gms:play-services-ads:7.5.0"
    

    然后就可以展示原生广告了

    AdLoader adLoader = new AdLoader.Builder(context, "/6499/example/native")
        .forAppInstallAd(new OnAppInstallAdLoadedListener() {
            @Override
            public void onAppInstallAdLoaded(NativeAppInstallAd appInstallAd) {
                // Show the app install ad.
            }
        })
        .forContentAd(new OnContentAdLoadedListener() {
            @Override
            public void onContentAdLoaded(NativeContentAd contentAd) {
                // Show the content ad.
            }
        })
        .withAdListener(new AdListener() {
            @Override
            public void onAdFailedToLoad(int errorCode) {
                // Handle the failure by logging, altering the UI, etc.
            }
        })
        .withNativeAdOptions(new NativeAdOptions.Builder()
                // Methods in the NativeAdOptions.Builder class can be
                // used here to specify individual options settings.
                .build())
        .build();
    

    Click here for complete documentation.

    【讨论】:

    • listview @James 中有没有完整的例子?
    【解决方案4】:

    作为此线程的补充,您现在可以按照 Google 提供的使用 NativeExpressAdView 的指南非常轻松地为 Admob 实施 NativeAds。 有关更多信息,请查看谷歌文档: https://firebase.google.com/docs/admob/android/native-express?hl=en

    【讨论】:

      【解决方案5】:

      将此代码添加到您的 Listview 适配器

             builder.forAppInstallAd(new NativeAppInstallAd.OnAppInstallAdLoadedListener() {
                  @Override
                  public void onAppInstallAdLoaded(NativeAppInstallAd ad) {
                      FrameLayout frameLayout =
                              (FrameLayout) findViewById(R.id.fl_adplaceholder);
                      NativeAppInstallAdView adView = (NativeAppInstallAdView) getLayoutInflater()
                              .inflate(R.layout.ad_app_install, null);
                      populateAppInstallAdView(ad, adView);
                      frameLayout.removeAllViews();
                      frameLayout.addView(adView);
                  }
              });
      
             AdLoader adLoader = builder.withAdListener(new AdListener() {
              @Override
              public void onAdFailedToLoad(int errorCode) {
                  Toast.makeText(MainActivity.this, "Failed to load native ad: "
                          + errorCode, Toast.LENGTH_SHORT).show();
              }
            }).build();
      
              adLoader.loadAd(new AdRequest.Builder().build());
      

      对 listview Adapter 进行一些更改,您将从以下链接获得 populateAppInstallAdView() 方法

      此示例中涵盖了所有内容,请通过此示例 https://github.com/googleads/googleads-mobile-android-examples/tree/master/admob

      【讨论】:

        【解决方案6】:

        此外,Tooleap Ads SDK 还提供了一种简单的方式来实施 Admob 的原生广告。

        不需要您使用传统的 listView 适配器并在您的内容中显示广告,而是将 admob 原生广告显示为一个小的浮动气泡。按下它可以看到完整的原生广告。

        这是在您的 activity 类中使用他们的 SDK 的示例:

        BubbleImageAd = new BubbleImageAd(this);
        bubbleImageAd.setAdUnitId("YOUR_AD_UNIT_ID");
        bubbleImageAd.loadAndShowAd(this);
        

        您可以查看here

        【讨论】:

          【解决方案7】:

          嗯,这个帖子可能已经过时了。但从 2015 年 5 月开始,截至目前,AdMob 确实支持原生广告(尽管仍处于测试阶段)。

          https://support.google.com/admob/answer/6239795

          此外,它在测试阶段仅对有限数量的开发人员开放。

          【讨论】:

            【解决方案8】:
            Admob in your android these are the codes needed. 
                <com.google.android.gms.ads.AdView
                          android:layout_alignParentBottom="true"
                            xmlns:ads="http://schemas.android.com/apk/res-auto"
                            android:id="@+id/adView"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            ads:adSize="SMART_BANNER"
                            ads:adUnitId="ca-app-pub-4549020480017205/6066702579"
                            />
            

            //在你的java类文件中

                 AdView mAdView = (AdView) findViewById(R.id.adView);
                AdRequest adRequest = new AdRequest.Builder().build();
                mAdView.loadAd(adRequest);
            

            【讨论】:

              【解决方案9】:

              目前仅限于选定的发布商。您需要联系您所在地区的 Google 客户经理进行实施。

              【讨论】:

                【解决方案10】:

                是的,您可以在 xml 文件中使用以下代码

                <com.google.android.gms.ads.NativeExpressAdView
                                    android:id="@+id/adView"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_centerHorizontal="true"
                                    android:layout_alignParentBottom="true"
                                    ads:adSize="320x300"
                                    ads:adUnitId="@string/ad_unit_id">
                

                mAdView.setVideoOptions(new VideoOptions.Builder()
                    .setStartMuted(true)
                    .build());
                mVideoController = mAdView.getVideoController();
                mVideoController.setVideoLifecycleCallbacks(new VideoController.VideoLifecycleCallbacks() {
                @Override
                public void onVideoEnd() {
                    Log.d(LOG_TAG, "Video playback is finished.");
                    super.onVideoEnd();
                }
                });
                
                
                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());
                

                【讨论】:

                  【解决方案11】:

                  ⟩⟩ 在项目结构中,导航到 activity_main.xml 并将以下代码粘贴到您的布局中。

                  <com.google.android.gms.ads.NativeExpressAdView
                                          android:id="@+id/adView"
                                          android:layout_width="wrap_content"
                                          android:layout_height="wrap_content"
                                          android:layout_centerHorizontal="true"
                                          android:layout_alignParentBottom="true"
                                          ads:adSize="320x300"
                                          ads:adUnitId="@string/ad_unit_id">
                  </com.google.android.gms.ads.NativeExpressAdView>
                  

                  在同一个文件中,即activity_main.xml,在标题部分添加下面的代码行

                  xmlns:ads="http://schemas.android.com/apk/res-auto"
                  

                  ⟩⟩ 现在打开 MainActivity.java 并在公共类中添加以下代码行

                  private static String LOG_TAG = "EXAMPLE";
                  NativeExpressAdView mAdView;
                  VideoController mVideoController;
                  

                  ⟩⟩ 然后在 MainActivity.java 下,在 onCreate() 方法中添加下面几行代码。

                  // Locate the NativeExpressAdView.
                  mAdView = (NativeExpressAdView) 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());
                  

                  ⟩⟩ 现在打开 values 文件夹中的 string.xml 文件并粘贴下面的代码行。

                  <string name="ad_unit_id">ca-app-pub-39402560999xxxxx/21772xxxxx</string>
                  

                  ⟩⟩ 然后打开 Manifest 文件并为其添加 Internet 权限。

                  <uses-permission android:name="android.permission.INTERNET" />
                  

                  资源 How to insert AdMob Native Ad in your Android App

                  【讨论】:

                    【解决方案12】:

                    要在我们的应用中添加原生模板,我们必须遵循一些基本步骤:

                    1-首先,我们要下载Native Templates,所以到developers.google

                    2-然后点击Download Native Templates,现在你将被引导到Github

                    3-然后从GitHub下载zip文件并将zip文件解压到任意文件夹并记住文件夹的位置,我们以后会用到

                    4-现在进入Android studio并点击File->New->Import Module,现在你会看到一个新窗口(Import-Module from Source)现在点击浏览图标和选择 nativetemplates 文件夹并点击完成并等待 Gradle 构建完成。

                    5-现在打开 Gradle Scripts->build.gradle (Module: app) 部分并导入 nativetemplates 项目并点击“立即同步”显示在顶部,如下所示:

                    //adding native templates
                    implementation project(':nativetemplates')
                    

                    https://www.studytonight.com/post/how-to-add-admob-native-ad-in-android-app#

                    【讨论】:

                      【解决方案13】:

                      这对我有用:

                      1. 在项目库中复制 Native 模板。

                      2. Build.gradle(Module:Appname) - 添加这个:

                        implementation project(':nativetemplates')
                        implementation 'com.google.android.gms:play-services-ads:20.4.0'
                        
                      3. Setting.gradle(Appname) - 添加这个:

                        include ':nativetemplates'
                        
                      4. 在 Mainfest - 更新如前。

                        申请前:

                        <meta-data
                         android:name="com.google.android.gms.ads.APPLICATION_ID"
                         android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>
                        

                        添加此权限:

                        <uses-permission android:name="android.permission.INTERNET" />
                        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
                        <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
                        
                      5. 在布局文件中添加:

                        <com.google.android.ads.nativetemplates.TemplateView
                         android:id="@+id/my_template"
                        app:gnt_template_type="@layout/gnt_medium_template_view"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" >
                        </com.google.android.ads.nativetemplates.TemplateView>
                        
                      6. 在程序文件中:

                        MobileAds.initialize(this)
                        //build ad
                        val adLoader = AdLoader.Builder(this,"ca-app-pub-3940256099942544/2247696110")
                            .forNativeAd {
                                val style  = NativeTemplateStyle.Builder().withMainBackgroundColor(ColorDrawable(Color.WHITE))
                                    .build()
                                val template = findViewById<TemplateView>(R.id.my_template)
                                template.setStyles(style)
                                template.setNativeAd(it)
                            }.build()
                        //show ad
                        adLoader.loadAd(AdRequest.Builder().build())
                        

                      如果仍未加载,请检查您的 admod 凭据。

                      【讨论】:

                        猜你喜欢
                        • 2021-08-01
                        • 1970-01-01
                        • 1970-01-01
                        • 2021-06-09
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        相关资源
                        最近更新 更多