应用程序只显示测试广告而不显示真实广告,为什么



我有一个ad-id,它可以在所有其他项目中工作。如果我现在尝试在我的libgdx项目中显示它,那么它就不会显示。代码必须正确,才能显示测试广告。

这是我的代码:

My build.gradle Module: android:

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")
                        }
                    }
                }
            }
        }
    }
}

我的安卓启动器:

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>

为什么横幅广告没有显示?

我想念一个 AddListener,它执行回调onAdLoaded告诉您何时有添加可用。

我无法在您的代码中清楚地确定您在<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());

希望这有帮助。

如果您的测试广告在您的应用中运行良好,并且您没有收到实时广告。

请按照以下步骤操作:

  1. 等待一些时间。

  2. 等待一段时间/几小时后,如果仍然有问题,则需要交叉检查AdMob帐户中的广告单元ID和AppId。

  3. 如果您能够加载测试广告,但无法加载实时广告,这听起来像是您的 AdMob 帐号存在问题。

  4. 确保您正确设置了支付系统和/或验证了您的 PIN 码?如果不这样做,则不会从您的帐户投放实时广告。

  5. 现在,您需要在此组中发布您的问题以寻求帮助。

最新更新