繁体   English   中英

Android原生广告adob

[英]Android native ad admob

我一直在尝试从admob实施原生广告。

我从Github那里得到了一些示例代码。 我试图实现这个代码,演示代码工作正常。 原生广告正确显示。

然后我创建了自己的Admob帐户并获得了Ad Unit ID 以前的原生广告演示代码无法使用此ID,我收到错误代码为0的错误,尽管我创建的Ad Unit ID适用于横幅广告。

有人可以帮我吗?

伙计们,我面临同样的问题,最后我有一个解决方案,希望这将是你们... :) 在此输入图像描述

在这里,当你试图创建一个adUnitId设置此图像中确保的宽度和高度 ,这里将是相同的宽度和高度,像这样的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="360x320"
  ads:adUnitId="@string/NativeAdId">
</com.google.android.gms.ads.NativeExpressAdView>

还有一件事你必须记住,你要制作广告的宽度和高度将适合你的xml布局,所以为此,首先在你的xml中尝试它,然后为此创建一个Native添加。

试试吧,随意告诉我你这样做后是否会遇到任何问题。 谢谢

请尝试以下代码:

XML文件:

 <com.google.android.gms.ads.AdView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_alignParentBottom="true"
        android:id="@+id/adView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-xxxxxxxxx" /> 

.java类:

AdView adView = (AdView) this.findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder()
        // Add a test device to show Test Ads
        //.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        //.addTestDevice("B2D638A0BEECBE3464024D83BE163E0E")
        .build();
        // Load ads into Banner Ads
        adView.loadAd(adRequest);

确保打开注释行:

.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("B2D638A0BEECBE3464024D83BE163E0E")

当您处于调试模式时。在调试模式下点击真实广告可能会导致您的Admob帐户被阻止。您在您的logcat中获取testDevice id ,将其替换为我的。

在manifest.xml中定义以下内容

<activity
            android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
            android:theme="@android:style/Theme.Translucent" />

还有许可

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

你准备好了。

**注意:**如果广告没有显示,请至少等待30分钟。

最近我坚持同样的问题。 然后我决定将我的解决方案发布到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();
    }

结果将如下所示

当我第一次从我新创建的admob帐户实施admob时,我的帐户花了2天时间才最终获得广告,所以不要担心,只是等待...如果您的帐户不是新创建的,那么看看您是否添加了此权限或不

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM