Google Play服务:广告加载问题 [英] Google Play Services: Ads loading issue

查看:220
本文介绍了Google Play服务:广告加载问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

描述:
我按照Google的最新指南将我的应用与Google Play服务SDK集成在一起。
之前它已经与独立的admob SDK集成。



问题: -
横幅广告和完整广告显示正确当其中一个显示。但是,当两者都必须显示时,它们会产生不可预知的结果。



不可预测的结果?
是的,有时只有横幅正在加载,有时只显示完整的广告。这是非常罕见的,他们都显示出来。



修正: -
1.我试图给一些延迟之间的横幅和间质性,并得到更好的结果。但大多数情况下仍然无法预测结果。


  1. 我在完整广告中使用了Asynch任务。在Background()中延迟2秒,并在PostExecute()中加载广告。它提供了更好的结果。但仍有10次尝试,只有7次显示广告。其余的3次横幅或全部或全部都没有加载。
  2. 以下是主要活动

      package com.example.adtest_new; 
    导入android.app.Activity;
    导入android.content.Context;
    导入android.os.AsyncTask;
    导入android.os.Bundle;
    导入android.widget.Toast;

    import com.google.android.gms.ads.AdListener;
    import com.google.android.gms.ads.AdRequest;
    导入com.google.android.gms.ads.AdView;
    import com.google.android.gms.ads.InterstitialAd;

    public class MainActivity extends Activity
    {
    Context myCont = null;
    private interstitialAd interstitial = null;
    AdRequest adr;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    //从activity_main.xml获取视图
    setContentView(R.layout.activity_main);

    myCont = this;

    // -------------- BANNER AD ------------------------ ----------
    //在activity_main.xml中找到横幅广告
    AdView adView =(AdView)this.findViewById(R.id.adView);
    //请求广告
    AdRequest adRequest = new AdRequest.Builder()。build();
    //将广告加载到横幅广告
    adView.loadAd(adRequest);
    // -------------- BANNER AD ---------------------------- ------

    //修复1(没有工作)
    尝试{Thread.sleep(2 * 1000);} catch(Exception e){}

    // Fix-2(没有工作)
    //在这里调用Asynch任务(在后台()中延迟2秒并在postexecute()中加载完整广告)

    // -------------- FULL AD ------------------------------ ----
    //准备插页广告
    interstitial = new InterstitialAd(MainActivity.this);

    //插入广告单元ID
    interstitial.setAdUnitId(ca-app-pub-465697675827xxxx / xxxxxxxxxx);
    adr = new AdRequest.Builder()。build();
    //将广告加载到插页式广告

    //准备插页式广告监听器
    interstitial.setAdListener(新的AdListener()
    {
    public void onAdLoaded ()
    {
    //System.out.println(!!! Full Ad loaded !!!);
    Toast.makeText(myCont,!!! Full Ad loaded! !!,Toast.LENGTH_SHORT).show();
    //调用displayInterstitial()函数
    displayInterstitial();
    }
    });
    interstitial.loadAd(adr);
    // -------------- FULL AD ---------------------------- ------
    }

    public void displayInterstitial(){
    //如果广告已加载,则显示插页式广告,否则不显示任何内容。
    if(interstitial.isLoaded())
    {
    interstitial.show();
    }
    }

    }



    activity_main.xml

     <?xml version =1.0encoding = UTF-8 >?; 
    < LinearLayout xmlns:android =http://schemas.android.com/apk/res/android
    android:id =@ + id / openedWindows
    android:layout_width =fill_parent
    android:layout_height =fill_parent
    android:background =#ffffff
    android:orientation =vertical>

    $ b< TextView
    android:id =@ + id / completePath
    android:layout_width =wrap_content
    android:layout_height = wrap_content
    android:layout_gravity =left
    android:layout_marginTop =0dp
    android:layout_marginLeft =3dp
    android:text =/ mnt / sdcard
    android:textColor =#ff2d8df1
    android:textSize =16dip
    android:textStyle =bold
    android:typeface =sans/>

    < View
    android:id =@ + id / bar0
    android:layout_width =fill_parent
    android:layout_height =1dp
    android:layout_marginTop =0dp
    android:background =#ffb2cb39
    android:layout_marginBottom =0dp/>



    < View
    android:id =@ + id / bar1
    android:layout_width =fill_parent
    android :layout_height =1dp
    android:layout_marginTop =0dp
    android:background =#ffb2cb39
    android:layout_marginBottom =1dp/>


    < RelativeLayout
    android:id =@ + id / temp
    android:layout_width =match_parent
    android:layout_height = wrap_content
    android:background =@ android:color / transparent
    android:layout_marginTop =5dp
    android:layout_marginBottom =5dp
    android:orientation =vertical >

    < Button
    android:id =@ + id / selectAll
    android:layout_width =wrap_content
    android:layout_height =25dp
    android:layout_gravity =left
    android:layout_marginBottom =1dp
    android:layout_marginLeft =15dp
    android:layout_alignParentLeft =true @ drawable / button_green
    android:textSize =15dip
    android:text =BUTTON1
    android:paddingLeft =10px
    android:paddingRight =10px
    android:textColor =#ffffff
    android:textStyle =bold/>

    清单

     <?xml version =1.0encoding =utf-8?> 
    < manifest xmlns:android =http://schemas.android.com/apk/res/android
    package =com.example.adtest_new
    android:versionCode = 1
    android:versionName =1.0>

    < uses-sdk
    android:minSdkVersion =9
    android:targetSdkVersion =21/>

    < application
    android:allowBackup =true
    android:icon =@ drawable / ic_launcher
    android:label =@ string / app_name
    android:theme =@ style / AppTheme>
    < activity
    android:name =。MainActivity
    android:label =@ string / app_name>
    < intent-filter>

    < category android:name =android.intent.category.LAUNCHER/>
    < / intent-filter>
    < / activity>

    < activity android:name =com.google.android.gms.ads.AdActivity
    android:configChanges =keyboard | keyboardHidden | orientation | screenLayout | uiMode | screenSize | smallestScreenSize />
    < meta-data android:name =com.google.android.gms.version
    android:value =@ integer / google_play_services_version/>

    < / application>

    <使用权限android:name =android.permission.ACCESS_WIFI_STATE/>
    < uses-permission android:name =android.permission.INTERNET/>
    < uses-permission android:name =android.permission.ACCESS_NETWORK_STATE/>

    < / manifest>

    请让我知道如何解决此问题?
    独立admob SDK对两种广告类型均正常工作。
    正在加载真实的广告(即不是测试广告),因为你没有声明测试设备。我不是100%确定我要说什么,但这可能是问题的原因。在展示真实广告时,我认为您并不总是会收到来自Admob / Google Play服务的可用广告,因此有时您不会显示广告(我认为加载错误代码为3,即无填充)。您可以在宣布测试设备时进行测试,这会导致使用始终可用的测试广告。

    另外,建议在开发时使用测试设备,因为如果您点击自己的广告上的很多内容(错误或仅仅是测试),您可能会被禁止访问AdMob。



    要了解如何添加测试设备,请参阅


    测试广告



    您应该在开发过程中使用测试广告,以避免产生错误的
    印象。此外,您始终可以依靠可用的
    测试广告。通过将散列设备ID传递给
    来设置测试广告AdRequest.Builder.addTestDevice:AdRequest request = new
    AdRequest.Builder()
    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)//所有模拟器
    .addTestDevice(AC98C820A50B4AD8A2106EDE96FB87D4)//我的Galaxy Nexus测试手机
    .build();


    logcat将打印dev



    ice的MD5哈希ID为方便起见,例如:使用
    AdRequest.Builder.addTestDevice(AC98C820A50B4AD8A2106EDE96FB87D4)到
    在此设备上获取测试广告。 / p>

    https://developers.google.com/mobile-ads-sdk/docs/admob/android/banner



    <请注意,有些人认为如果手机不是英文版,测试广告无法正常工作()编辑:当然,真正的应用用户将会使用真实的广告,所以他们也可能遇到并不总是可用广告的问题,但这是正常的。编辑2:顺便说一下,你可以按时重新计划新加载(对于插页式广告,横幅会定期重新加载)来尝试应对我的猜测。

    可能与您的问题稍有不同,但人们通常强烈建议不要显示AdListener.onAdLoaded方法中的广告,而是在您希望显示广告时手动调用此广告(如果isLoaded为true)。但这只是一个人体工程学的考虑,没有一个错误 - 明智的一个


    Description:- I am integrating my App with "Google Play Service" SDK as per latest guidelines from Google. earlier it was integrated with standalone admob SDK.

    ISSUE:- Banner ads and Full ads are shown properly when one of them are shown. But when both has to be shown, they give unpredictable results.

    Unpredictable results? Yes, sometime only Banner is loading and sometime only Full ad is shown. it is very rare that both of them are shown.

    FIX:- 1. I tried giving some delay between Banner and interstitial and got little better results. But still results are unpredictable most of the time.

    1. I used Asynch task for Full ads. Gave some delay of 2 seconds in Background() and loaded the ad in PostExecute(). It give much better results. But still out of 10 attempts, only 7 times both the ads are shown. rest of the 3 times either Banner or Full or none of them is loaded.

    Following is the code of Main activity

    package com.example.adtest_new; 
    import android.app.Activity;
    import android.content.Context;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.widget.Toast;
    
    import com.google.android.gms.ads.AdListener;
    import com.google.android.gms.ads.AdRequest;
    import com.google.android.gms.ads.AdView;
    import com.google.android.gms.ads.InterstitialAd;
    
    public class MainActivity extends Activity 
    {
    Context myCont=null;
    private InterstitialAd interstitial = null;
    AdRequest adr;
    
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        //Get the view from activity_main.xml
        setContentView(R.layout.activity_main);
    
        myCont = this;
    
        //--------------BANNER AD----------------------------------     
        //Locate the Banner Ad in activity_main.xml
        AdView adView = (AdView) this.findViewById(R.id.adView); 
        // Request for Ads
        AdRequest adRequest = new AdRequest.Builder().build(); 
        // Load ads into Banner Ads
        adView.loadAd(adRequest);
        //--------------BANNER AD----------------------------------         
    
    //Fix-1 (Did not work)
        try{Thread.sleep(2*1000);}catch(Exception e){}          
    
    //Fix-2 (Did not work)
    //Called Asynch task here(put delay of 2 seconds in background() and loaded full ad in postexecute())
    
        //--------------FULL AD----------------------------------   
        //Prepare the Interstitial Ad
        interstitial = new InterstitialAd(MainActivity.this);        
    
        //Insert the Ad Unit ID
        interstitial.setAdUnitId("ca-app-pub-465697675827xxxx/xxxxxxxxxx");
        adr = new AdRequest.Builder().build();
        //Load ads into Interstitial Ads 
    
        //Prepare an Interstitial Ad Listener
        interstitial.setAdListener(new AdListener() 
        {
            public void onAdLoaded() 
            {
                //System.out.println("!!! Full Ad loaded  !!!");
                Toast.makeText(myCont, "!!! Full Ad loaded  !!!", Toast.LENGTH_SHORT).show();
                //Call displayInterstitial() function
                displayInterstitial();
            }
        });        
        interstitial.loadAd(adr);    
        //--------------FULL AD----------------------------------   
    }    
    
    public void displayInterstitial() {
        // If Ads are loaded, show Interstitial else show nothing.
        if (interstitial.isLoaded()) 
        {
            interstitial.show();
        }
    }
    

    }

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/openedWindows"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    android:orientation="vertical" >
    
    
    <TextView
        android:id="@+id/completePath"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:layout_marginTop="0dp"
        android:layout_marginLeft="3dp"
        android:text="/mnt/sdcard"
        android:textColor="#ff2d8df1"
        android:textSize="16dip"
        android:textStyle="bold"
        android:typeface="sans"/>
    
    <View
    android:id="@+id/bar0"
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:layout_marginTop="0dp"
    android:background="#ffb2cb39"    
    android:layout_marginBottom="0dp"/>
    
    
    
    <View
    android:id="@+id/bar1"
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:layout_marginTop="0dp"
    android:background="#ffb2cb39"    
    android:layout_marginBottom="1dp"/>
    
    
    <RelativeLayout     
    android:id="@+id/temp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent" 
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"   
    android:orientation="vertical">
    
    <Button
        android:id="@+id/selectAll"
        android:layout_width="wrap_content"
        android:layout_height="25dp"
        android:layout_gravity="left"
        android:layout_marginBottom="1dp"
        android:layout_marginLeft="15dp"
        android:layout_alignParentLeft="true"
        android:background="@drawable/button_green"
        android:textSize="15dip"
        android:text="BUTTON1"
        android:paddingLeft="10px"  
        android:paddingRight="10px"
        android:textColor="#ffffff"
        android:textStyle="bold" />
    
    <Button
        android:id="@+id/selectBtn"
        android:layout_width="wrap_content"
        android:layout_height="25dp"
        android:layout_marginRight="15dp"
        android:layout_marginBottom="1dp"
        android:layout_alignParentRight="true"
        android:background="@drawable/button_green"
        android:textSize="15dip"
        android:text="BUTTON2"
        android:paddingLeft="10px"  
        android:paddingRight="10px"
        android:textColor="#ffffff"
        android:textStyle="bold" />
    </RelativeLayout>
    
    
    <View
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:layout_above="@+id/tableRow2"
    android:background="#444444"    
    android:layout_marginBottom="0dp"/>
    
     <TableRow
     android:id="@+id/tableRow2"
     android:layout_width="match_parent"
     android:layout_height="wrap_content">
     <com.google.android.gms.ads.AdView
         xmlns:ads="http://schemas.android.com/apk/res-auto"
         android:id="@+id/adView"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         ads:adSize="BANNER"
         ads:adUnitId="ca-app-pub-465697675827xxxx/xxxxxxxxxx"
         />    
    </TableRow>
    
    </LinearLayout>
    

    manifest

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.adtest_new"
        android:versionCode="1"
        android:versionName="1.0" >
    
    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="21" />
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    
        <activity android:name="com.google.android.gms.ads.AdActivity"
          android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
        <meta-data android:name="com.google.android.gms.version"
           android:value="@integer/google_play_services_version"/>
    
    </application>
    
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    
    </manifest>
    

    Kindly let me know how to resolve this?? Standalone admob SDK is working fine for both of the ad types.

    解决方案

    I see that you are loading "real" ads (ie not test ads), as you do not declare test devices. I am not 100% sure about what I am going to say, but this could perhaps be the cause of the problem. When displaying real ads, I think you will not always have available ads from Admob/Google Play Services, so sometimes you will not have ads displayed (I think the loading error code is 3, which is "no fill"). You could test while declaring test devices, which cause the use of test ads that are always available.

    In addition it is recommended to use test devices while developping, because if you click a lot on your own ads (by mistake or just to test) you may be banned from AdMob.

    To see how to add test devices, cf this

    Test ads

    You should use test ads during development to avoid generating false impressions. Additionally, you can always count on a test ad being available. Set up test ads by passing your hashed Device ID to AdRequest.Builder.addTestDevice: AdRequest request = new AdRequest.Builder() .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) // All emulators .addTestDevice("AC98C820A50B4AD8A2106EDE96FB87D4") // My Galaxy Nexus test phone .build();

    logcat will print the dev

    ice's MD5-hashed ID for convenience, for example: Use AdRequest.Builder.addTestDevice("AC98C820A50B4AD8A2106EDE96FB87D4") to get test ads on this device.

    (https://developers.google.com/mobile-ads-sdk/docs/admob/android/banner)

    Take care that some people say that test ads do not work properly if your phone is not in English (AdMob test ads shows only on English devices)

    Edit : Of course real users of your app will use the real ads, so they may also experience the "problem" of not always ads available, but this is normal. You can still regurlarly Schedule new loads (for the interstitial, the banner reloads itself regularly) to try to cope with that I guess.

    Edit 2 : By the way, this may be a little irrelevant to your problem, but people usually strongly advise to no display the ad from the AdListener.onAdLoaded method, but to invoke this manually at the moment your want it to be displayed (if isLoaded is true). But this is only an ergonomy consideration, no a bug-Wise one

    这篇关于Google Play服务:广告加载问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆