Android Studio工程中添加移動廣告平臺廣告條代碼教程

準備工做:
android

1.首先你是Android開發者,或者至少會點Java編程基礎;編程


2.熟悉使用Android Studio(如下簡稱:AS),即官方Android應用開發工具。app


3.註冊了應用,而且有個banner_ad_unit_id吧ide


4,待加入橫幅廣告條的app;工具



開發過程:
1,建立或打開AS 工程,本示例是新建工程;
開發工具


2,打開Android SDK Manager安裝Google Repository;

3,關聯咱們的廣告SDK,這裏有兩種方式:
A  點擊Project Structure 直接選取,以下圖:
com.Google.android.gms:play-services:7.8.0;
B 打開build-gradle 加入一行代碼,以下圖:
compile 'com.Google.android.gms:play-services:6.+'
gradle


4,程序會開始自動編譯,而後打開AndroidManifest.xml 文件,添加以下代碼:
<?xml version="1.0" encoding="utf-8"?>  
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mapboo.adviewdemo" >  
   
<span style="color:#ff0000;"> <uses-permission android:name="android.permission.INTERNET" />  
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/></span>  
   
<application  
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >  

<span style="color:#ff0000;">
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" /></span>  
   
<activity android:name=".MainActivity"
android:label="@string/app_name" >  
<intent-filter>  

<action android:name="android.intent.action.MAIN" />  
   
<category android:name="android.intent.category.LAUNCHER" />  

       </intent-filter>  

       </activity>  
   
<span style="color:#ff0000;">
<activity  
android:name="com.Google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
</span>  
   
</application>  
   
</manifest>


5,打開activity_main.xml 文件,輸入以下代碼:
<font style="background-color:rgb(255, 255, 255)"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:gravity="center_vertical">  
   
    <TextView  
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/demoName"
        android:gravity="center"
        android:textSize="30dp"
        android:layout_marginTop="150dp"
        />  
   
    <com.google.android.gms.ads.AdView  
        android:id="@+id/adView_bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="bottom"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id" />  
   
</RelativeLayout>  </font>

<font style="background-color:rgb(255, 255, 255)"><resources>  
<string name="app_name">AdViewDemo</string>  
<string name="demoName">谷歌Admob廣告條示例</string>  
<string name="banner_ad_unit_id">ca-app-pub-8514769117499822/6386262194</string>   

</resources> </font>

6,打開MainActivity文件,輸入以下代碼:
<font style="background-color:rgb(255, 255, 255)">package com.mapboo.adviewdemo;  
   
import android.support.v7.app.ActionBarActivity;  
import android.os.Bundle;  
import android.util.Log;  
import android.view.Menu;  
import android.view.MenuItem;  
import com.google.android.gms.ads.AdListener;  
import com.google.android.gms.ads.AdRequest;  
import com.google.android.gms.ads.AdView;  
   
public class MainActivity extends ActionBarActivity {  
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
   
        /**
         * 建立廣告條對象
         */
AdView mAdView = (AdView) findViewById(R.id.adView_bottom);  

AdRequest adRequest = new AdRequest.Builder().build();
 
   
        /**
         * 建立一個監聽,好比判斷用戶是否是已經點擊了廣告條,以此來給用戶增長積分等操做;
         */
        AdListener listener = new AdListener() {  
            @Override
            public void onAdClosed() {  
                super.onAdClosed();  
   
                Log.i("TAG", "onAdClosed");  
            }  
   
            @Override
            public void onAdFailedToLoad(int errorCode) {  
                super.onAdFailedToLoad(errorCode);  
   
                Log.i("TAG", "onAdFailedToLoad");  
            }  
   
            @Override
            public void onAdLeftApplication() {  
                super.onAdLeftApplication();  
   
                Log.i("TAG", "onAdLeftApplication");  
            }  
   
            @Override
            public void onAdLoaded() {  
                super.onAdLoaded();  
   
                Log.i("TAG", "onAdLoaded");  
            }  
   
            @Override
            public void onAdOpened() {  
                super.onAdOpened();  
   
                Log.i("TAG", "onAdOpened");  
            }  
        };  
   
     /**
      * 設置監聽
   */
  mAdView.setAdListener(listener);  
   
   /**
     * 開始載入廣告條
       */
   mAdView.loadAd(adRequest);  
    }  
   
}  


編譯運行:
運行代碼,就會出現廣告條,只要替換本身的banner_ad_unit_id就能開始賺錢了;


KeyMob移動廣告平臺是一個平臺或者中介,鏈接着應用開發者和廣告主。在平臺上,開發者提供應用,廣告主提供廣告,而KeyMob移動廣告平臺就會提供相應手機系統的SDK。



ui