2017-06-19 176 views
0

我有一個ad-id在其他項目中工作。 如果我現在嘗試在我的libgdx項目中顯示它,那麼它不會顯示。 除了顯示測試廣告之外,代碼必須正確。應用只顯示測試廣告,但不顯示真實廣告,爲什麼?

這裏是我的代碼:

我的build.gradle模塊:機器人:

android { 
    buildToolsVersion "21.1.2" 
    compileSdkVersion 21 
    sourceSets { 
     main { 
      manifest.srcFile 'AndroidManifest.xml' 
      java.srcDirs = ['src'] 
      aidl.srcDirs = ['src'] 
      renderscript.srcDirs = ['src'] 
      res.srcDirs = ['res'] 
      assets.srcDirs = ['assets'] 
     } 

     instrumentTest.setRoot('tests') 
    } 
} 

// needed to add JNI shared libraries to APK when compiling on CLI 
tasks.withType(com.android.build.gradle.tasks.PackageApplication) { pkgTask -> 
    pkgTask.jniFolders = new HashSet<File>() 
    pkgTask.jniFolders.add(new File(projectDir, 'libs')) 
} 

// called every time gradle gets executed, takes the native dependencies of 
// the natives configuration, and extracts them to the proper libs/ folders 
// so they get packed with the APK. 
task copyAndroidNatives() { 
    file("libs/armeabi/").mkdirs(); 
    file("libs/armeabi-v7a/").mkdirs(); 
    file("libs/x86/").mkdirs(); 

    configurations.natives.files.each { jar -> 
     def outputDir = null 
     if(jar.name.endsWith("natives-armeabi-v7a.jar")) outputDir = file("libs/armeabi-v7a") 
     if(jar.name.endsWith("natives-armeabi.jar")) outputDir = file("libs/armeabi") 
     if(jar.name.endsWith("natives-x86.jar")) outputDir = file("libs/x86") 
     if(outputDir != null) { 
      copy { 
       from zipTree(jar) 
       into outputDir 
       include "*.so" 
      } 
     } 
    } 
} 

task run(type: Exec) { 
    def path 
    def localProperties = project.file("../local.properties") 
    if (localProperties.exists()) { 
     Properties properties = new Properties() 
     localProperties.withInputStream { instr -> 
      properties.load(instr) 
     } 
     def sdkDir = properties.getProperty('sdk.dir') 
     if (sdkDir) { 
      path = sdkDir 
     } else { 
      path = "$System.env.ANDROID_HOME" 
     } 
    } else { 
     path = "$System.env.ANDROID_HOME" 
    } 

    def adb = path + "/platform-tools/adb" 
    commandLine "$adb", 'shell', 'am', 'start', '-n', 'com.barodapride.flappy.android/com.barodapride.flappy.android.AndroidLauncher' 
} 

// sets up the Android Eclipse project, using the old Ant based build. 
eclipse { 
    // need to specify Java source sets explicitely, SpringSource Gradle Eclipse plugin 
    // ignores any nodes added in classpath.file.withXml 
    sourceSets { 
     main { 
      java.srcDirs "src", 'gen' 
     } 
    } 

    jdt { 
     sourceCompatibility = 1.6 
     targetCompatibility = 1.6 
    } 

    classpath { 
     plusConfigurations += [ project.configurations.compile ]   
     containers 'com.android.ide.eclipse.adt.ANDROID_FRAMEWORK', 'com.android.ide.eclipse.adt.LIBRARIES'  
    } 
    dependencies { 

     compile 'com.google.android.gms:play-services-ads:10.2.4' 
    } 
    project { 
     name = appName + "-android" 
     natures 'com.android.ide.eclipse.adt.AndroidNature' 
     buildCommands.clear(); 
     buildCommand "com.android.ide.eclipse.adt.ResourceManagerBuilder" 
     buildCommand "com.android.ide.eclipse.adt.PreCompilerBuilder" 
     buildCommand "org.eclipse.jdt.core.javabuilder" 
     buildCommand "com.android.ide.eclipse.adt.ApkBuilder" 
    } 
} 

// sets up the Android Idea project, using the old Ant based build. 
idea { 
    module { 
     sourceDirs += file("src"); 
     scopes = [ COMPILE: [plus:[project.configurations.compile]]]   

     iml { 
      withXml { 
       def node = it.asNode() 
       def builder = NodeBuilder.newInstance(); 
       builder.current = node; 
       builder.component(name: "FacetManager") { 
        facet(type: "android", name: "Android") { 
         configuration { 
          option(name: "UPDATE_PROPERTY_FILES", value:"true") 
         } 
        } 
       } 
      } 
     } 
    } 
} 

我AndroidLauncher:

package com.barodapride.flappy.android; 

import android.os.Bundle; 
import android.view.View; 
import android.widget.RelativeLayout; 

import com.badlogic.gdx.backends.android.AndroidApplication; 
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration; 
import com.barodapride.flappy.AdHandler; 
import com.barodapride.flappy.FlappyGame; 
import com.barodapride.flappy.IActivityRequestHandler; 
import com.google.android.gms.ads.AdRequest; 
import com.google.android.gms.ads.AdSize; 
import com.google.android.gms.ads.AdView; 
import com.google.android.gms.ads.InterstitialAd; 


public class AndroidLauncher extends AndroidApplication implements AdHandler{ 

    private static final String adUnitId ="ca-app-pub-XXXXXXXXX/XXXXXXXXXX"; 
    private static final String TAG = "AndroidLauncher"; 
    protected AdView adView; 


    @Override 
    protected void onCreate (Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     RelativeLayout layout = new RelativeLayout(this); 

     AndroidApplicationConfiguration config = new AndroidApplicationConfiguration(); 
     View gameView=initializeForView(new FlappyGame(this), config); 

     RelativeLayout.LayoutParams gameViewParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
     gameViewParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); 
     gameViewParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE); 

     gameView.setLayoutParams(gameViewParams); 
     layout.addView(gameView); 

     adView = new AdView(this); 
     adView.setAdSize(AdSize.BANNER); 
     adView.setAdUnitId(adUnitId); 

     AdRequest.Builder adRequestBuilder = new AdRequest.Builder(); 
     adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR); 
     adView.loadAd(adRequestBuilder.build()); 

     RelativeLayout.LayoutParams topParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
     topParams.addRule(RelativeLayout.ALIGN_PARENT_TOP,RelativeLayout.TRUE); 
     topParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE); 
     layout.addView(adView, topParams); 
     adView.setBackgroundColor(android.graphics.Color.TRANSPARENT); 

     setContentView(layout); 
    } 

    @Override 
    public void showAds(boolean show) { 

    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     adView.resume(); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     adView.pause(); 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     adView.destroy(); 
    } 
    } 

我的清單:

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

    <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="25" /> 
    <uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/GdxTheme" > 
     <meta-data android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version" /> 

     <activity android:name="com.google.android.gms.ads.AdActivity" 
      android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" 
      android:theme="@android:style/Theme.Translucent" /> 
     <activity 
      android:name="com.barodapride.flappy.android.AndroidLauncher" 
      android:label="@string/app_name" 
      android:screenOrientation="portrait" 
      android:configChanges="keyboard|keyboardHidden|orientation|screenSize"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

爲什麼是橫幅廣告沒有出現?

回答

0

我想念一個AddListener,它執行一個回調onAdLoaded,它告訴你什麼時候有一個add。

我無法在代碼中清楚地標識出<AdView>組件和代碼之間的鏈接(缺少xml佈局)。

這裏是我的libgdx遊戲之一的片段,其精細顯示的實際廣告:

在我的清單我用這個metadata<Application>

<application> 
<meta-data android:value="true" android:name="ADMOB_ALLOW_LOCATION_FOR_ADS" /> 

然後,你有在你的XML(至少,我希望如此)一個<AdView>組件。 該組件需要偵聽器。

adView.setAdListener(new AdListener() { 

@Override 
public void onAdLoaded() { 
    super.onAdLoaded(); 

    // Inform your screen, that the add is loaded 
    // Screen shall make the AdView visible -> runOnUiThread!!! 
    androidGame.runOnUiThread(50, new Runnable() { 
     @Override 
     public void run() { 
      // This call is from my game engine, just do a callback 
      // to your running screen that it shall make the view visible 
      if (Engine.get().getActiveScreen() != null) 
       Engine.get().getActiveScreen().onBannerAdLoaded(); 
     } 
    }); 
} 
}); 

adView.loadAd(new AdRequest.Builder().build()); 

希望這會有所幫助。

0

如果您的測試廣告與您的應用程序正常工作,並且您未獲得實時廣告。

遵循以下步驟:

  1. 等待一段時間。

  2. 如果仍有問題,請等待幾次/小時後,您需要從AdMob帳戶中檢查廣告單元ID和AppId。

  3. 如果您可以加載測試廣告但不是實時廣告,這聽起來像是您的AdMob帳戶的問題。

  4. 確保您正確設置了支付系統和/或驗證了PIN碼?如果他們沒有完成,則不會從您的帳戶投放實時廣告。

  5. 現在您需要在此group中發佈您的問題以尋求幫助。