2014-09-03 88 views
0

描述: - 根據Google的最新指南,我將我的應用與「Google Play服務」SDK集成在一起。早些時候它與獨立的admob SDK集成在一起。Google Play服務:廣告加載問題

問題: - 當顯示其中一個廣告時,橫幅廣告和完整廣告會正常顯示。但是,當兩者都必須展示時,它們會產生不可預測的結果。

不可預知的結果? 是的,有時只有橫幅正在加載,有時只顯示完整的廣告。他們都被證明是非常罕見的。

修正: - 1.我試圖給與Banner和interstitial之間的一些延遲,並得到好一點的結果。但是大多數時候結果仍然不可預測。

  1. 我用非同步任務完整版的廣告。在Background()中延遲2秒,並在PostExecute()中加載廣告。它提供了更好的結果。但仍有10次嘗試,只有7次顯示廣告。其餘的3次橫幅或全部或沒有任何加載。

以下是主要活動

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> 

清單的代碼

<?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> 

請讓我知道如何解決這個問題? 獨立admob SDK適用於兩種廣告類型。

回答

0

我看到你加載「真實」的廣告(即沒有測試廣告),只要你不申報的測試設備。我不是100%確定我要說什麼,但這可能是問題的原因。當顯示內容的廣告,我想你不會總是有來自AdMob /谷歌提供的廣告播放服務,所以有時你會不會有廣告顯示出來(我想加載錯誤代碼是3,這是「無填充顏色」)。您可以在聲明測試設備時進行測試,這會導致使用始終可用的測試廣告。

此外,建議使用測試設備,同時深化發展,因爲如果你點擊了很多自己的廣告(由於失誤或只是爲了測試),你可能來自AdMob被禁止。

要了解如何添加測試設備,比照本

測試廣告

發展過程中應使用測試廣告,以避免產生錯誤的印象 。此外,您始終可以依靠可用的測試廣告 。通過將散列的設備ID傳遞給 AdRequest.Builder來設置測試廣告。addTestDevice:在AdRequest請求=新 AdRequest.Builder() .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)//所有模擬器 .addTestDevice( 「AC98C820A50B4AD8A2106EDE96FB87D4」)//我的Galaxy Nexus手機測試 .build();

logcat會dev的

冰的MD5哈希ID爲方便起見,例如:使用 AdRequest.Builder.addTestDevice( 「AC98C820A50B4AD8A2106EDE96FB87D4」)在此設備上 取得測試廣告。

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

要小心,有些人說,如果你的手機是不是英語,測試廣告和不正常工作(AdMob test ads shows only on English devices

編輯:當然,你的應用程序的真實用戶使用真實的廣告,所以他們也可能遇到並不總是可用廣告的「問題」,但這是正常的。您仍然可以按時重新安排新的負載(對於插頁式廣告,橫幅會定期重新加載)以嘗試應對我的猜測。

編輯2:順便說一下,這可能與您的問題有點不相關,但人們通常強烈建議不要顯示AdListener.onAdLoaded方法中的廣告,而是在您希望的時候手動調用此方法顯示(如果isLoaded爲true)。但這只是一個人體工程學的考慮,沒有錯誤 - 明智之一