android - AdMob 广告未在 Android 应用中展示

标签 android eclipse android-activity admob ads

我尝试使用 Google Play Services API 将 AdMob 广告插入我的应用程序 不管怎样,他们没有出现。

我希望有人能帮助我

我有 2 个 Activity (MainActivity.java 和 AdMobAdd.java)

这是 AdMobAdd.java(如果有人需要 MainActivity,请告诉我)

(我使用了这里的示例 http://kyanogen.com/admob-interstitial-ads/ )

package com.myapp.helpme:D;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import com.rajatbsosz.timbermancheat.R;

public class AdMobAdd extends Activity {

/**
 * Your ad unit id, you must replace it with your actual ad unit id
 * Which you can generate from Admob website
 * 
 */
private static final String AD_UNIT_ID = "ca-app-pub-99******************";
private static final String TAG = "AdMobAdd";
private InterstitialAd iAd;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    iAd = new InterstitialAd(this);
    iAd.setAdUnitId(AD_UNIT_ID);

    iAd.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            Log.d(TAG, "onAdLoaded");
            Toast.makeText(AdMobAdd.this, "Ad loaded successfully", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onAdFailedToLoad(int errorCode) {
            String errorMessage = String.format("Failed to load add : "+ getErrorReason(errorCode));
            Log.d(TAG, errorMessage);
            Toast.makeText(AdMobAdd.this, errorMessage, Toast.LENGTH_SHORT).show();
        }
    });

    loadInterstitial();
}

public void loadInterstitial() {
    AdRequest adRequest = new AdRequest.Builder()
    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
    .addTestDevice("You can add you device id here, run code once and get id from logs")
    .build();

    iAd.loadAd(adRequest);
}

public void showInterstitial() {
    if (iAd.isLoaded()) {
        iAd.show();
    } else {
        Log.d(TAG, "Interstitial ad is not loaded yet");
    }
}

/** 
 * Gets a string error reason from an error code
 * 
 * @param errorCode
 * @return
 */
private String getErrorReason(int errorCode) {

    String errorReason = "unknown error";

    switch(errorCode) {
        case AdRequest.ERROR_CODE_INTERNAL_ERROR:
            errorReason = "internal error";
            break;
        case AdRequest.ERROR_CODE_INVALID_REQUEST:
            errorReason = "invalid request";
            break;
        case AdRequest.ERROR_CODE_NETWORK_ERROR:
            errorReason = "network Error";
            break;
        case AdRequest.ERROR_CODE_NO_FILL:
            errorReason = "no fill";
            break;
    }
    return errorReason;
}

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

list :

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

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

 <!-- Used to request banner and interstitial ads. -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Used to avoid sending an ad request if there is no connectivity. -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<!-- Required permissions for video ads. -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <meta-data android:name="com.google.android.gms.version"
           android:value="@integer/google_play_services_version"/>
    <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|s    mallestScreenSize"/>

</application>

</manifest>

布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/d3"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.example.MainActivity" >

<ScrollView
    android:id="@+id/scroll"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollbars="vertical"
    android:fillViewport="true" >

<RelativeLayout
    android:id="@+id/container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >


    about 10 TextViews and Buttons...
    ...

    </RelativeLayout>

</ScrollView>

</RelativeLayout>

最佳答案

有两种类型的横幅,这是我的例子

public class MainActivity extends SherlockActivity  {

    private Button btnList;
    private AdView adView;
    LinearLayout layout;

    private InterstitialAd interstitial;

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

        setContentView(R.layout.activity_main);

        layout = (LinearLayout) findViewById(R.id.banner);

        addBanner();
        // Create the interstitial or big banner
        interstitial = new InterstitialAd(this);
        interstitial.setAdUnitId(Utils.idBANNERBIG);

        // Create ad request.
        AdRequest adRequest = new AdRequest.Builder().build();

        // Begin loading your interstitial.
        interstitial.loadAd(adRequest);

        interstitial.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                Log.d("GAME", "onAdLoaded");
                displayInterstitial();
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
                Log.d("GAME", errorCode + "");

            }
        });

    }

    // Invoke displayInterstitial() when you are ready to display an
    // interstitial.
    private void displayInterstitial() {
        if (interstitial.isLoaded()) {
            interstitial.show();
        }
    }

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

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

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

    private void addBanner() { //small banner
        adView = new AdView(this);
        adView.setAdUnitId(Utils.idBANNER);
        adView.setAdSize(AdSize.SMART_BANNER);

        layout.addView(adView);

        // Iniciar una solicitud gen�rica.
        AdRequest adRequest = new AdRequest.Builder().addTestDevice(
                AdRequest.DEVICE_ID_EMULATOR).build();

        // Cargar adView con la solicitud de anuncio.
        adView.loadAd(adRequest);
    }
}

关于android - AdMob 广告未在 Android 应用中展示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25168628/

相关文章:

java - 忽略textspeech中的某些单词(http地址)

android - Eclipse w/ADT 崩溃,编辑器中出现 "import android."语句

android startActivityForResult 正在终止父 Activity 中的线程

android - TextView 将右侧放置的控件推出屏幕

Android:进程已经死亡 - 资源不足?

android - : java. lang.NoSuchMethodError : okhttp3. internal.Platform.log 导致 Retrofit2 登录出错

java - 在 Eclipse 中,如何将 Open Type 快捷方式限制为仅显示当前项目中的源文件

c - C项目中的Eclipse错误

android - ACTION_SENDTO 不起作用

android kotlin - 开始 Activity 并从 Intent 中获取数据