android開發步步為營之55:google廣告平台admob接入總結,androidadmob
App變現的主要渠道有廣告,增值服務,線上交易。最近項目需要接入google的admob廣告平台,這裡寫個總結,方便其他開發人員參考。
第一步:通過android sdk manager下載google play services lib,如何下載不了,baidu一下,去下載其他人提供的
下載好了之後,將該lib項目引用到測試專案study,即可調用google play services相關廣告的api了
第二步、https://www.google.com/admob/ 註冊帳號,註冊完成之後,就可以建立新應用獲利了,我這裡測試已經有$0.13了
注意建立的廣告單元id,接下來我們要用到
第三步:AndroidManifest.xml添加許可權、meta-data、Adactivity
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.google.android.gms.example.bannerexample" > <!-- Include required permissions for Google Mobile Ads to run--> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <!--This meta-data tag is required to use Google Play Services.--> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <activity android:name="com.figo.study.AdmobActivity" android:theme="@android:style/Theme.NoTitleBar" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!--Include the AdActivity configChanges and theme. --> <activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:theme="@android:style/Theme.Translucent" /> </application></manifest>
第四步:設計測試頁面
<?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:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <com.google.android.gms.ads.AdView android:id="@+id/adView" android:layout_width="match_parent" android:layout_height="wrap_content" ads:adSize="SMART_BANNER" ads:adUnitId="@string/banner_ad_unit_id" > </com.google.android.gms.ads.AdView></LinearLayout>
這裡的banner_ad_unit_id即為admob中建立應用的廣告單元id,配置在了res/values/strings.xml檔案中
<string name="banner_ad_unit_id">ca-app-pub-2128163432248238/6211350109</string>
第五步:Activity中調用
* */package com.figo.study;import android.app.Activity;import android.os.Bundle;import com.google.android.gms.ads.AdRequest;import com.google.android.gms.ads.AdView;/** * @author figo * */public class AdmobActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_admob);try {// 橫幅廣告AdView mAdView = (AdView) findViewById(R.id.adView);AdRequest adRequest = new AdRequest.Builder().build();// mAdView.setAdSize(AdSize.FULL_BANNER);mAdView.loadAd(adRequest);// 插頁式廣告// final InterstitialAd interstitial = new InterstitialAd(// AdmobActivity.this);// interstitial.setAdUnitId(getString(R.string.banner_ad_unit_id));// if (interstitial.isLoaded()) {// interstitial.show();//// } else {// AdRequest adRequest = new AdRequest.Builder().build();// interstitial.loadAd(adRequest);// AdListener adListener = new AdListener() {//// @Override// public void onAdLoaded() {// // TODO Auto-generated method stub// super.onAdLoaded();// interstitial.show();// }// };// interstitial.setAdListener(adListener);// }} catch (Exception e) {System.out.print(e.getStackTrace());}}}
OK,大功告成,運行效果:
順便提供一下google admob官方接入文檔
AdMob網址:https://apps.admob.com
Admob開參考文檔:https://developers.google.com/mobile-ads-sdk/
快速入門:https://developers.google.com/mobile-ads-sdk/docs/admob/android/quick-start
SDK下載: https://developers.google.com/mobile-ads-sdk/download
Admob參考demo下載:https://github.com/googleads/googleads-mobile-android-examples