在Android应用中添加广告是开发者收入之一,但有广告的应用对于用户来说比较反感,很多事情都是相互矛盾。如何在我们的APP中添加广告呢?移动广告平台为我们提供了相应的SDK,只需要下载他们的SDK,按他们的开发文档添加就可以了。
下面是一些广告平台:
一、百度移动联盟:http://munion.baidu.com/
1.百度移动联盟推广SDK下载:http://munion.baidu.com/about.html#/sdk/mobSdk
2.百度移动联盟积分墙SDK下载:http://munion.baidu.com/about.html#/sdk/statSdk
还有其它的一些广告平台如:多盟、力美、酷果
关于更多移动广告平台信息可以查看:http://baike.baidu.com/link?url=4MavjKAnjUsqLuhs0rdOkjzfWdzJBv_2pDEh36blDxDwNzp2R-0LoEP1mRLjb-KbId8w-FXnYg4EWc5QWAdFLa
下面是我使用百度推广SDK的例子,其实跟百度提供的例子是一样的,只是我自己练了一遍
首先把百度推广SDK包下载,解压后的目录
在《Baidu_MobAds_SDK_Manual.doc》文档中教你如何把广告添加到你的APP中
目录结构
AndroidManifest.xml文件中声明权限和Activity
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.dzt.baidu" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <!-- 以下为可选权限 --> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <meta-data android:name="BaiduMobAd_APP_ID" android:value="debug" /> <meta-data android:name="BaiduMobAd_APP_SEC" android:value="debug" /> <activity android:name="com.baidu.mobads.AppActivity" android:configChanges="keyboard|keyboardHidden|orientation" /> <activity android:name="com.dzt.baidu.DeclaringActivity" android:label="@string/simple_declaring" /> <activity android:name="com.dzt.baidu.CodingActivity" android:label="@string/simple_coding" /> <activity android:name="com.dzt.baidu.InterstitialActivity" android:label="@string/simple_inters" /> <activity android:name="com.dzt.baidu.IconsActyvity" android:label="@string/simple_icon" /> <activity android:name="com.dzt.baidu.AdActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
调试过程中,建议先设置APPSID和计费名的值为debug,以便进行广告调试。调试成功后,务必改为自己的APPSID和计费名,并上传应用至联盟端审核,审核通过后才能为您计费,如果还未审核通过而APPSID和计费名的值不为debug,则没有广告显示。
AdActivity.java
package com.dzt.baidu; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; /** * 使用百度推广示例 dzt * * @author Administrator 2014.04.09 * */ public class AdActivity extends Activity implements OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initWidgets(); } private void initWidgets() { Button btn = (Button) findViewById(R.id.simple_declaring); btn.setOnClickListener(this); btn = (Button) findViewById(R.id.simple_coding); btn.setOnClickListener(this); btn = (Button) findViewById(R.id.simple_inters); btn.setOnClickListener(this); btn = (Button) findViewById(R.id.simple_icon); btn.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.simple_declaring: StartActivity(DeclaringActivity.class); break; case R.id.simple_coding: StartActivity(CodingActivity.class); break; case R.id.simple_inters: StartActivity(InterstitialActivity.class); break; case R.id.simple_icon: StartActivity(IconsActyvity.class); break; default: break; } } private void StartActivity(Class<?> cls) { Intent intent = new Intent(AdActivity.this, cls); startActivity(intent); } }相应的xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".AdActivity" > <TextView android:id="@+id/tv_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" android:text="@string/hello_world" /> <Button android:id="@+id/simple_declaring" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/tv_text" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" android:text="@string/simple_declaring" /> <Button android:id="@+id/simple_coding" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/simple_declaring" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" android:text="@string/simple_coding" /> <Button android:id="@+id/simple_inters" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/simple_coding" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" android:text="@string/simple_inters" /> <Button android:id="@+id/simple_icon" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/simple_inters" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" android:text="@string/simple_icon" /> </RelativeLayout>
横幅banner广告有两种添加方式
一、代码添加
package com.dzt.baidu; import org.json.JSONObject; import com.baidu.mobads.AdSettings; import com.baidu.mobads.AdSize; import com.baidu.mobads.AdView; import com.baidu.mobads.AdViewListener; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.RelativeLayout; public class CodingActivity extends Activity { private static final String TAG = "dzt"; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); // setContentView(R.layout.coding); // 人群属性 AdSettings.setKey(new String[] { "baidu", "中国" }); AdSettings.setCity("上海"); AdSettings.setZip("123456"); AdSettings.setJob("工程师"); AdSettings.setEducation(AdSettings.Education.BACHELOR); AdSettings.setSalary(AdSettings.Salary.F10kT15k); AdSettings.setHob(new String[] { "羽毛球", "足球", "baseball" }); RelativeLayout rlMain = new RelativeLayout(this); // 创建广告View AdView adView = new AdView(this, AdSize.Banner, "01"); // 设置监听器 adView.setListener(new AdViewListener() { public void onAdSwitch() { Log.w(TAG, "[CodingActivity]->onAdSwitch"); } public void onAdShow(JSONObject info) { Log.w(TAG, "[CodingActivity]->onAdShow " + info.toString()); } public void onAdReady(AdView adView) { Log.w(TAG, "[CodingActivity]->onAdReady " + adView); } public void onAdFailed(String reason) { Log.w(TAG, "[CodingActivity]->onAdFailed " + reason); } public void onAdClick(JSONObject info) { Log.w(TAG, "[CodingActivity]->onAdClick " + info.toString()); } public void onVideoStart() { Log.w(TAG, "[CodingActivity]->onVideoStart"); } public void onVideoFinish() { Log.w(TAG, "[CodingActivity]->onVideoFinish"); } @Override public void onVideoClickAd() { Log.w(TAG, "[CodingActivity]->onVideoClickAd"); } @Override public void onVideoClickClose() { Log.w(TAG, "[CodingActivity]->onVideoClickClose"); } @Override public void onVideoClickReplay() { Log.w(TAG, "[CodingActivity]->onVideoClickReplay"); } @Override public void onVideoError() { Log.w(TAG, "[CodingActivity]->onVideoError"); } }); rlMain.addView(adView); setContentView(rlMain); } }二、xml文件添加
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:baiduadsdk="http://schemas.android.com/apk/res/com.dzt.baidu" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fdfefa" android:orientation="vertical" > <com.baidu.mobads.AdView android:id="@+id/adView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" baiduadsdk:adId="" baiduadsdk:adSize="0" /> <!-- 注: baiduadsdk:adId可以指定为"",表示默认广告位 --> </RelativeLayout>xmlns:baiduadsdk="http://schemas.android.com/apk/res/com.dzt.baidu" 其中com.dzt.baidu为你app的包名,xmlns:baiduadsdk为自定义控件的属性域
相应的属性文件attrs.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="com.baidu.mobads.AdView"> <attr name="adSize" format="integer" /> <attr name="adId" format="string" /> </declare-styleable> </resources>
相应的Demo:http://download.csdn.net/detail/deng0zhaotai/7168479