android開發步步為營之55:google廣告平台admob接入總結,androidadmob

來源:互聯網
上載者:User

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



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.