How to add Admob advertising in my Android applica

2019-06-21 18:05发布

I have read this article http://code.google.com/mobile/ads/docs/android/fundamentals.html, and now I have the code

package com.nda.admob;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

import android.app.Activity;
import android.os.Bundle;
import android.util.AttributeSet;
import android.widget.LinearLayout;

public class AdMobTestingActivity extends Activity {
    /** Called when the activity is first created. */
    AdView adView;

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

        adView = new AdView(this, AdSize.BANNER,  "a14eb6c98335a35");
        LinearLayout l=(LinearLayout)findViewById(R.id.linear);
        l.addView(adView);

        AdRequest request = new AdRequest();
        adView.loadAd(request);
    }
}

And code for AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.nda.admob"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".AdMobTestingActivity"
                  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.ads.AdActivity" 
              android:configChanges="keyboard|keyboardHidden|orientation"/>       
    </application>
        <uses-sdk android:minSdkVersion="3" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
</manifest>

But I have the message "You must have AdActivity declared in AndroidManifest.xml with configChanges" always. Where have I made mistake?

2条回答
叛逆
2楼-- · 2019-06-21 18:43

If you are using the latest admob sdk, you need define your AdActivity like this:

    <activity android:name="com.google.ads.AdActivity"
          android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

Note that some configChanges option requires a later Android SDK version (API Level 11), As it is stated in the link your provided, you need:

Requirements

The Google AdMob Ads SDK for Android requires Android 1.5 or later. Make sure you have the latest copy of the Android SDK and that you're compiling against at least Android v3.2 (set target in default.properties to android-13).

Hope that help.

查看更多
不美不萌又怎样
3楼-- · 2019-06-21 18:49

looks like you are missing the listener. ply try by adding this just after you init adView

adView.setAdListener(this);
查看更多
登录 后发表回答