a. Right click on your app project in Eclipse and select Properties.
b. Select
Android and then click Add.... Find the google-play-services_lib
project and select
OK to add the Google Play services library.
c. The project now references the Google Play services library.
meta-data
tagGoogle Play services requires you to add the following meta-data
tag within the
element in your app‘s
AndroidManifest.xml
:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.company" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true"> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/> <activity android:label="@string/app_name" android:name="BannerExample"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest>
com.google.android.gms.ads.AdActivity
The Mobile Ads SDK requires that com.google.android.gms.ads.AdActivity
be declared in your app‘s
AndroidManifest.xml
:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.company" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true"> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/> <activity android:label="@string/app_name" android:name="BannerExample"> <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"/> </application> </manifest>
Making ad requests requires these permissions to be declared in the manifest:
INTERNET
ACCESS_NETWORK_STATE
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.company" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true"> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/> <activity android:label="@string/app_name" android:name="BannerExample"> <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"/> </application> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> </manifest>
The easiest way to incorporate an ad is to simply define your
AdView
as you would any other part of your res/layout/main.xml
:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.gms.ads.AdView android:id="@+id/adView" android:layout_width="wrap_content" android:layout_height="wrap_content" ads:adUnitId="MY_AD_UNIT_ID" ads:adSize="BANNER"/> </LinearLayout>
As usual you must replace MY_AD_UNIT_ID
with your AdMob ad unit ID. Note the inclusion of the
ads
namespace referenced in specifying adUnitId
and
adSize
.
To fetch ads, look up the AdView
as a resource via
findViewById
, create an AdRequest
, and invoke loadAd
:
import com.google.android.gms.ads.*; public class BannerExample extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Look up the AdView as a resource and load a request. AdView adView = (AdView)this.findViewById(R.id.adView); AdRequest adRequest = new AdRequest.Builder().build(); adView.loadAd(adRequest); } }
Add admob advertisement for Google Play Service,布布扣,bubuko.com
Add admob advertisement for Google Play Service
原文地址:http://blog.csdn.net/wehappy168/article/details/27610915