zoukankan      html  css  js  c++  java
  • 添加Google Admob到ANDROID应用中

    现在的ANDROID大部分还是靠在应用里面植入广告来盈利。Google Admob是一个不错的平台,但是到网络上一搜,发现按照现有的文章的步骤来添加的话,根本不成功。最后还是到Google Admob的官方文档才得到一个详细的步骤。故本文针对其步骤做了一个简要的翻译。

     

    本文使用的Google Admob SDK为4.1.0,原文网址为:http://code.google.com/intl/zh-CN/mobile/ads/docs/android/fundamentals.html

    使用Google Admob SDK包括以下三个步骤:

    1, 添加SDK到Eclipse工程里

    2, 添加com.google.ads.AdActivity

    3, 声明必须的网络权限

    4, 添加com.google.ads.AdView

    1,添加SDK

    解压之后的SDK包含一个jar文件,一个docs文件夹和一个README文档。

    1.1 右键单击Eclipse的工程并选择属性

    1.2 选择Java Build Path->Libraries,选择Add External JARs添加Google Admob SDK的jar文件

     

    2, 添加com.google.ads.AdActivity

    为了使你的应用在显示Admob广告的时候正确的维护Activity的栈,必须在AndroidManifest.xml文件添加com.google.ads.AdActivity

     

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    3.           package="com.company"  
    4.           android:versionCode="1" android:versionName="1.0">  
    5.   <application android:icon="@drawable/icon" android:label="@string/app_name"  
    6.                android:debuggable="true">  
    7.     <activity android:label="@string/app_name" android:name="BannerExample">  
    8.       <intent-filter>  
    9.         <action android:name="android.intent.action.MAIN"/>  
    10.         <category android:name="android.intent.category.LAUNCHER"/>  
    11.       </intent-filter>  
    12.     </activity>  
    13.     <activity android:name="com.google.ads.AdActivity"  
    14.               android:configChanges="keyboard|keyboardHidden|orientation"/>  
    15.   </application>  
    16. </manifest>  

     

    3,声明权限

    广告需要访问网络,必须添加权限。

     

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    3.           package="com.company"  
    4.           android:versionCode="1" android:versionName="1.0">  
    5.   <application android:icon="@drawable/icon" android:label="@string/app_name"  
    6.                android:debuggable="true">  
    7.     <activity android:label="@string/app_name" android:name="BannerExample">  
    8.       <intent-filter>  
    9.         <action android:name="android.intent.action.MAIN"/>  
    10.         <category android:name="android.intent.category.LAUNCHER"/>  
    11.       </intent-filter>  
    12.     </activity>  
    13.     <activity android:name="com.google.ads.AdActivity"  
    14.               android:configChanges="keyboard|keyboardHidden|orientation"/>  
    15.   </application>  
    16.   <uses-permission android:name="android.permission.INTERNET"/>  
    17.   <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>  
    18. </manifest>  

    4,添加AdView

    有如下两种方式来添加AdView。

    4.1 直接在代码中添加

    在代码里直接添加AdView需要如下步骤

    1,Import com.google.ads.*

    2,声明一个AdView实例

    3,创建AdView,指定你的Admob Publisher ID

    4,添加AdView到UI

    5,装载广告

     

    可参考如下示例:

     

    1. import com.google.ads.*;  
    2.   
    3. public class BannerExample extends Activity {  
    4.   @Override  
    5.   public void onCreate(Bundle savedInstanceState) {  
    6.     super.onCreate(savedInstanceState);  
    7.     setContentView(R.layout.main);  
    8.   
    9.     // Create the adView  
    10.     AdView adView = new AdView(this, AdSize.BANNER, MY_AD_PUBLISHER_ID);  
    11.     // Lookup your LinearLayout assuming it’s been given  
    12.     // the attribute android:id="@+id/mainLayout"  
    13.     LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);  
    14.     // Add the adView to it  
    15.     layout.addView(adView);  
    16.     // Initiate a generic request to load it with an ad  
    17.     adView.loadAd(new AdRequest());  
    18.   }  
    19. }  

    4.2 在XML文件中添加

    也可以在layout xml文件中添加AdView。


    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.               xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"  
    4.               android:orientation="vertical"  
    5.               android:layout_width="fill_parent"  
    6.               android:layout_height="fill_parent">  
    7.   <com.google.ads.AdView android:id="@+id/adView"  
    8.                          android:layout_width="wrap_content"  
    9.                          android:layout_height="wrap_content"  
    10.                          ads:adUnitId="MY_AD_PUBLISHER_ID"  
    11.                          ads:adSize="BANNER"  
    12.                          ads:loadAdOnCreate="true"/>  
    13. </LinearLayout>  



    测试结果

     

    注意:当Admob第一次接收到你的Publisher ID的广告请求时,可能需要最多2分钟来接受广告。当你的Publisher ID有24小时没有使用时,这初始的2分钟间隔将会重复出现。

  • 相关阅读:
    pat 乙级1084 外观数列
    将int 转换为string 函数 to_string()
    stl find_first_not_of()函数
    小写转变为大写函数toupper()
    基础实验2-2.3 组合数的和 (15分)
    基础实验2-2.2 求集合数据的均方差 (15分)
    习题1.9 有序数组的插入 (20分)
    用eclipse运行算法第四版的BinarySearch
    关于脱发
    HUD-2586(LCA板子)
  • 原文地址:https://www.cnblogs.com/jackrex/p/3001343.html
Copyright © 2011-2022 走看看