android添加广告之--admob
2011-05-09 13:31
411 查看
Incorporating the SDK
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.
You should now be able to rebuild your project without any errors.
Adding a com.google.ads.AdView
Android apps are composed ofViewobjects, Java instances the user sees as text areas, buttons and other controls.
AdViewis simply another
Viewsubclass displaying small HTML5 ads that respond to user touch.
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
The easiest place to do all this is in your app’s
Activity.
import com.google.ads.*; public class BannerExample extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Create the adView 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()); } }
All that’s left is to properly configure your
AndroidManifest.xml.
com.google.ads.AdActivity
In order for your app to properly maintain the Android activity stack while displaying rich advertising it must declare acom.google.ads.AdActivityin its 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"/> </application> </manifest>
Permissions
Making ad requests requires the networking permissionsINTERNETand
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"/> </application> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> </manifest>
You can download an example project containing this code here.
The Result
When you now run your app you should see a banner at the top of the screen:转自:http://blog.sina.com.cn/s/blog_4d142b550100rw4b.html
相关文章推荐
- 基于Unity3D的Android游戏添加google广告的方法——使用AdMob
- android添加广告之--admob
- Android平台给PhoneGap添加Admob广告
- android 中添加 admob 广告
- Android平台给cordova添加Admob广告
- Unity3D Android游戏添加google广告的方法——使用AdMob
- google admob 广告插件添加[android]
- 基于Unity3D的Android游戏添加google广告的方法——使用AdMob
- 基于Unity3D的Android游戏添加google广告的方法——使用AdMob
- 用Eclipse添加android软件的GOOGLE admob广告2016年
- 使用Admob给Android APP添加广告
- Android中添加Admob广告
- Android中添加Admob广告
- Unity3D Android游戏添加谷歌广告——使用AdMob
- Google Admob广告Android 、简单应用
- cocos2d-x 3.0 final 如何在android里添加广告
- Android开发步步为营之55:google广告平台admob接入总结
- IOS 如何在cocos2d Game中添加 AdMob 广告条
- Cocos2d-x 3.X, Android Studio版添加多盟广告平台
- cocos2dx android添加广点通广告平台的---插屏广告