Incorporating Google AdMob Ads into your app is a three step process:
- Add the SDK JAR to your Eclipse project.
- Declare
com.google.ads.AdActivityinAndroidManifest.xml. - Set up required network permissions in the manifest.
Adding the SDK JAR
The decompressed SDK consists of a JAR, a javadoc folder and a README.1. Right click on your app project in Eclipse and choose Properties.
2. Select Java Build Path and the Libraries tab. Then click Add External JARs... to add the Google AdMob Ads JAR.
com.google.ads.AdActivity
The AdMob Ads SDK requires thatcom.google.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"> <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.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation/> </application> </manifest>
Permissions
Making ad requests requires the networking permissionsINTERNET and ACCESS_NETWORK_STATE, so these must also be declared in the manifest:<?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"> <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.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>You should now be able to rebuild your project without any errors.
Adding a com.google.ads.AdView
Android apps are composed ofView objects, Java instances the user sees as text areas, buttons and other controls. AdView is simply another View subclass displaying small HTML5 ads that respond to user touch.Like any
View, an AdView may be created either purely in code or largely in XML.The five lines of code it takes to add a banner:
- Import
com.google.ads.* - Declare an
AdViewinstance - Create it, specifying a unit ID—your AdMob publisher ID
- Add the view to the UI
- Load it with an ad
Activity.import com.google.ads.*; public class BannerExample extends Activity { private AdView adView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Create the adView adView = new AdView(this, AdSize.BANNER, MY_AD_UNIT_ID); // Lookup your LinearLayout assuming it’s been given // the attribute android:id="@+id/mainLayout" LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout); // Add the adView to it layout.addView(adView); // Initiate a generic request to load it with an ad adView.loadAd(new AdRequest()); } @Override public void onDestroy() { adView.destroy(); super.onDestroy(); } }
No comments:
Post a Comment