Android Studio工程中添加移动广告平台广告条代码教程
来源:互联网 发布:办公室网络 编辑:程序博客网 时间:2024/05/21 09:42
准备工作:
开发过程:
1,创建或打开AS 工程,本示例是新建工程;
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.+'
<?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。
1.首先你是Android开发者,或者至少会点Java编程基础;
2.熟悉使用Android Studio(以下简称:AS),即官方Android应用开发工具。
3.注册了应用,并且有个banner_ad_unit_id吧
4,待加入横幅广告条的app;
开发过程:
1,创建或打开AS 工程,本示例是新建工程;
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.+'
<?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。
0 0
- Android Studio工程中添加移动广告平台广告条代码教程
- Android Studio工程中添加Admob广告条代码详解
- Android Studio工程中添加Admob广告条代码详解
- Android开发,移动广告平台
- Android Studio应用添加KeyMob广告管理库详细教程
- cordova应用加移动广告平台视频广告教程
- 使用移动广告平台横幅广告自动刷新教程
- 使用admob移动广告SDK在Android应用中集成插页式广告教程
- 移动广告平台Android SDK接入指南
- cocos2dx android添加广点通广告平台的---插屏广告
- 移动广告平台列举
- 移动平台广告类型
- 移动广告平台盈利模式
- android 中添加 admob 广告
- Android中添加Admob广告
- Android中添加Admob广告
- 在循环广告条中添加链接
- Android平台给PhoneGap添加Admob广告
- 设计模式(七)---原型模式
- SoftReference 和 WeakReference
- Oracled数据库的进一步理解
- 可变参数列表(...)
- Caffe 训练mnist数据
- Android Studio工程中添加移动广告平台广告条代码教程
- Android 自定义Dialog无法Dismiss
- ffmpeg调试需要使用的一些比较有用的命令
- flex设置DataGrid文本按内容自动换行
- javascript 正则表达式过滤sql关键字
- Cut Frame From Decoding Stream To Show As a RGB Form Picture
- strncpy函数的危险性
- 如何判断一段程序是由C编译程序还是C++编译程序编译的?
- RoboBinding探究二:ImageView及自定义控件实现