首先呢 請先按照官網的作法 

1. Setting Up  Google Play Services

To make the Google Play services APIs available to your app:

  1. Copy the library project at <android-sdk>/extras/google/google_play_services/libproject/google-play-services_lib/ to the location where you maintain your Android app projects.
  2. Import the library project into your Eclipse workspace. Click File > Import, select Android > Existing Android Code into Workspace, and browse to the copy of the library project to import it.
  3. In your app project, reference Google Play services library project. See Referencing a Library Project for Eclipse for more information on how to do this.

    Note: You should be referencing a copy of the library that you copied to your development workspace—you should not reference the library directly from the Android SDK directory.

  4. After you've added the Google Play services library as a dependency for your app project, open your app's manifest file and add the following tag as a child of the <application> element:
    <meta-dataandroid:name="com.google.android.gms.version"
           
    android:value="@integer/google_play_services_version"/>
       

Once you've set up your project to reference the library project, you can begin developing features with theGoogle Play services APIs.

 

2. 再來廣告的種類有很多  請參考 這邊

這裡要說明的是橫幅廣告

    // 建立 adView。
    adView
=newAdView(this);
    adView
.setAdUnitId(MY_AD_UNIT_ID);
    adView
.setAdSize(AdSize.BANNER);

   
// 假設 LinearLayout 已獲得 android:id="@+id/mainLayout" 屬性,
   
// 查詢 LinearLayout。
   
LinearLayout layout =(LinearLayout)findViewById(R.id.mainLayout);

   
// 在其中加入 adView。
    layout
.addView(adView);

   
// 啟動一般請求。
   
AdRequest adRequest =newAdRequest.Builder().build();

   
// 以廣告請求載入 adView。
    adView
.loadAd(adRequest);

主要程式碼大概長這樣  如果要加入監聽的話很簡單 

adView.setAdListener(newAdListener(){
 
@Override
 
publicvoid onAdOpened(){
   
// 前往重疊廣告前,先儲存應用程式狀態。
 
}
});

其他的類型還有如下, 依樣畫葫蘆就好了

publicabstractclassAdListener{
 
publicvoid onAdLoaded();
 
publicvoid onAdFailedToLoad(int errorCode);
 
publicvoid onAdOpened();
 
publicvoid onAdClosed();
 
publicvoid onAdLeftApplication();
}

 

 


, ,
創作者介紹

Mazs's Notes

cookiesp 發表在 痞客邦 PIXNET 留言(0) 人氣()