Android开发步步为营之55:google广告平台admob接入总结

App变现的主要渠道有广告,增值服务,在线交易。最近项目需要接入google的admob广告平台,这里写个总结,方便其他开发者参考。

第一步:通过android sdk manager下载google play services lib,如何下载不了,baidu一下,去下载其他人提供的

gpservice

下载好了之后,将该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 {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.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



FROM:  http://www.it165.net/pro/html/201504/39322.html

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值