码迷,mamicode.com
首页 > 其他好文 > 详细

Add admob advertisement for Google Play Service

时间:2014-06-03 01:49:10      阅读:384      评论:0      收藏:0      [点我收藏+]

标签:android   admob   

1. Add and reference the Google Play services library project

 

  a. Right click on your app project in Eclipse and select Properties.

   bubuko.com,布布扣

  b. Select Android and then click Add.... Find the   google-play-services_lib project and select OK to add the  Google Play services library.

   bubuko.com,布布扣

  c. The project now references the Google Play services library.

   bubuko.com,布布扣

2. Add a meta-data tag

Google 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>

3. Declare 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>

4. Set up network permissions

Making ad requests requires these permissions to be declared in the manifest:

INTERNET
Required. Used to access the Internet to make ad  requests.
ACCESS_NETWORK_STATE
Optional. Used to check if an internet connection is  available prior to making an ad request.
<?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>

 

5. Defining a com.google.android.gms.ads.AdView

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.

6. Look up and Load

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

标签:android   admob   

原文地址:http://blog.csdn.net/wehappy168/article/details/27610915

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!