Android App添加广告


分类: Android基础   300人阅读  评论(1)  收藏  举报

在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://www.youmi.net/

还有其它的一些广告平台如:多盟、力美、酷果

关于更多移动广告平台信息可以查看:http://baike.baidu.com/link?url=4MavjKAnjUsqLuhs0rdOkjzfWdzJBv_2pDEh36blDxDwNzp2R-0LoEP1mRLjb-KbId8w-FXnYg4EWc5QWAdFLa

下面是我使用百度推广SDK的例子,其实跟百度提供的例子是一样的,只是我自己练了一遍

首先把百度推广SDK包下载,解压后的目录


在《Baidu_MobAds_SDK_Manual.doc》文档中教你如何把广告添加到你的APP中

目录结构


效果图




AndroidManifest.xml文件中声明权限和Activity

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.dzt.baidu"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="8"  
  9.         android:targetSdkVersion="17" />  
  10.   
  11.     <uses-permission android:name="android.permission.INTERNET" />  
  12.     <uses-permission android:name="android.permission.READ_PHONE_STATE" />  
  13.     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
  14.     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />  
  15.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
  16.     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />  
  17.     <!-- 以下为可选权限 -->  
  18.     <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />  
  19.     <uses-permission android:name="android.permission.RECORD_AUDIO" />  
  20.     <uses-permission android:name="android.permission.VIBRATE" />  
  21.     <uses-permission android:name="android.permission.CAMERA" />  
  22.     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  
  23.     <uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" />  
  24.   
  25.     <application  
  26.         android:allowBackup="true"  
  27.         android:icon="@drawable/ic_launcher"  
  28.         android:label="@string/app_name"  
  29.         android:theme="@style/AppTheme" >  
  30.         <meta-data  
  31.             android:name="BaiduMobAd_APP_ID"  
  32.             android:value="debug" />  
  33.         <meta-data  
  34.             android:name="BaiduMobAd_APP_SEC"  
  35.             android:value="debug" />  
  36.   
  37.         <activity  
  38.             android:name="com.baidu.mobads.AppActivity"  
  39.             android:configChanges="keyboard|keyboardHidden|orientation" />  
  40.         <activity  
  41.             android:name="com.dzt.baidu.DeclaringActivity"  
  42.             android:label="@string/simple_declaring" />  
  43.         <activity  
  44.             android:name="com.dzt.baidu.CodingActivity"  
  45.             android:label="@string/simple_coding" />  
  46.         <activity  
  47.             android:name="com.dzt.baidu.InterstitialActivity"  
  48.             android:label="@string/simple_inters" />  
  49.         <activity  
  50.             android:name="com.dzt.baidu.IconsActyvity"  
  51.             android:label="@string/simple_icon" />  
  52.         <activity  
  53.             android:name="com.dzt.baidu.AdActivity"  
  54.             android:label="@string/app_name" >  
  55.             <intent-filter>  
  56.                 <action android:name="android.intent.action.MAIN" />  
  57.   
  58.                 <category android:name="android.intent.category.LAUNCHER" />  
  59.             </intent-filter>  
  60.         </activity>  
  61.     </application>  
  62.   
  63. </manifest>  

调试过程中,建议先设置APPSID和计费名的值为debug,以便进行广告调试。调试成功后,务必改为自己的APPSID和计费名,并上传应用至联盟端审核,审核通过后才能为您计费,如果还未审核通过而APPSID和计费名的值不为debug,则没有广告显示。

AdActivity.java

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.dzt.baidu;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9.   
  10. /** 
  11.  * 使用百度推广示例 dzt 
  12.  *  
  13.  * @author Administrator 2014.04.09 
  14.  *  
  15.  */  
  16. public class AdActivity extends Activity implements OnClickListener {  
  17.   
  18.     @Override  
  19.     protected void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.activity_main);  
  22.         initWidgets();  
  23.     }  
  24.   
  25.     private void initWidgets() {  
  26.         Button btn = (Button) findViewById(R.id.simple_declaring);  
  27.         btn.setOnClickListener(this);  
  28.         btn = (Button) findViewById(R.id.simple_coding);  
  29.         btn.setOnClickListener(this);  
  30.         btn = (Button) findViewById(R.id.simple_inters);  
  31.         btn.setOnClickListener(this);  
  32.         btn = (Button) findViewById(R.id.simple_icon);  
  33.         btn.setOnClickListener(this);  
  34.     }  
  35.   
  36.     @Override  
  37.     public void onClick(View v) {  
  38.         // TODO Auto-generated method stub  
  39.         switch (v.getId()) {  
  40.         case R.id.simple_declaring:  
  41.             StartActivity(DeclaringActivity.class);  
  42.             break;  
  43.         case R.id.simple_coding:  
  44.             StartActivity(CodingActivity.class);  
  45.             break;  
  46.         case R.id.simple_inters:  
  47.             StartActivity(InterstitialActivity.class);  
  48.             break;  
  49.         case R.id.simple_icon:  
  50.             StartActivity(IconsActyvity.class);  
  51.             break;  
  52.         default:  
  53.             break;  
  54.         }  
  55.     }  
  56.   
  57.     private void StartActivity(Class<?> cls) {  
  58.         Intent intent = new Intent(AdActivity.this, cls);  
  59.         startActivity(intent);  
  60.     }  
  61. }  
相应的xml文件

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".AdActivity" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/tv_text"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_centerHorizontal="true"  
  12.         android:layout_marginTop="10dp"  
  13.         android:text="@string/hello_world" />  
  14.   
  15.     <Button  
  16.         android:id="@+id/simple_declaring"  
  17.         android:layout_width="match_parent"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_below="@id/tv_text"  
  20.         android:layout_centerHorizontal="true"  
  21.         android:layout_marginTop="10dp"  
  22.         android:text="@string/simple_declaring" />  
  23.   
  24.     <Button  
  25.         android:id="@+id/simple_coding"  
  26.         android:layout_width="match_parent"  
  27.         android:layout_height="wrap_content"  
  28.         android:layout_below="@id/simple_declaring"  
  29.         android:layout_centerHorizontal="true"  
  30.         android:layout_marginTop="10dp"  
  31.         android:text="@string/simple_coding" />  
  32.   
  33.     <Button  
  34.         android:id="@+id/simple_inters"  
  35.         android:layout_width="match_parent"  
  36.         android:layout_height="wrap_content"  
  37.         android:layout_below="@id/simple_coding"  
  38.         android:layout_centerHorizontal="true"  
  39.         android:layout_marginTop="10dp"  
  40.         android:text="@string/simple_inters" />  
  41.   
  42.     <Button  
  43.         android:id="@+id/simple_icon"  
  44.         android:layout_width="match_parent"  
  45.         android:layout_height="wrap_content"  
  46.         android:layout_below="@id/simple_inters"  
  47.         android:layout_centerHorizontal="true"  
  48.         android:layout_marginTop="10dp"  
  49.         android:text="@string/simple_icon" />  
  50.   
  51. </RelativeLayout>  

横幅banner广告有两种添加方式

一、代码添加

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.dzt.baidu;  
  2.   
  3. import org.json.JSONObject;  
  4.   
  5. import com.baidu.mobads.AdSettings;  
  6. import com.baidu.mobads.AdSize;  
  7. import com.baidu.mobads.AdView;  
  8. import com.baidu.mobads.AdViewListener;  
  9.   
  10. import android.app.Activity;  
  11. import android.os.Bundle;  
  12. import android.util.Log;  
  13. import android.widget.RelativeLayout;  
  14.   
  15. public class CodingActivity extends Activity {  
  16.   
  17.     private static final String TAG = "dzt";  
  18.   
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         // TODO Auto-generated method stub  
  22.         super.onCreate(savedInstanceState);  
  23.         // setContentView(R.layout.coding);  
  24.         // 人群属性  
  25.         AdSettings.setKey(new String[] { "baidu""中国" });  
  26.         AdSettings.setCity("上海");  
  27.         AdSettings.setZip("123456");  
  28.         AdSettings.setJob("工程师");  
  29.         AdSettings.setEducation(AdSettings.Education.BACHELOR);  
  30.         AdSettings.setSalary(AdSettings.Salary.F10kT15k);  
  31.         AdSettings.setHob(new String[] { "羽毛球""足球""baseball" });  
  32.         RelativeLayout rlMain = new RelativeLayout(this);  
  33.         // 创建广告View  
  34.         AdView adView = new AdView(this, AdSize.Banner, "01");  
  35.         // 设置监听器  
  36.         adView.setListener(new AdViewListener() {  
  37.             public void onAdSwitch() {  
  38.                 Log.w(TAG, "[CodingActivity]->onAdSwitch");  
  39.             }  
  40.   
  41.             public void onAdShow(JSONObject info) {  
  42.                 Log.w(TAG, "[CodingActivity]->onAdShow " + info.toString());  
  43.             }  
  44.   
  45.             public void onAdReady(AdView adView) {  
  46.                 Log.w(TAG, "[CodingActivity]->onAdReady " + adView);  
  47.             }  
  48.   
  49.             public void onAdFailed(String reason) {  
  50.                 Log.w(TAG, "[CodingActivity]->onAdFailed " + reason);  
  51.             }  
  52.   
  53.             public void onAdClick(JSONObject info) {  
  54.                 Log.w(TAG, "[CodingActivity]->onAdClick " + info.toString());  
  55.             }  
  56.   
  57.             public void onVideoStart() {  
  58.                 Log.w(TAG, "[CodingActivity]->onVideoStart");  
  59.             }  
  60.   
  61.             public void onVideoFinish() {  
  62.                 Log.w(TAG, "[CodingActivity]->onVideoFinish");  
  63.             }  
  64.   
  65.             @Override  
  66.             public void onVideoClickAd() {  
  67.                 Log.w(TAG, "[CodingActivity]->onVideoClickAd");  
  68.             }  
  69.   
  70.             @Override  
  71.             public void onVideoClickClose() {  
  72.                 Log.w(TAG, "[CodingActivity]->onVideoClickClose");  
  73.             }  
  74.   
  75.             @Override  
  76.             public void onVideoClickReplay() {  
  77.                 Log.w(TAG, "[CodingActivity]->onVideoClickReplay");  
  78.             }  
  79.   
  80.             @Override  
  81.             public void onVideoError() {  
  82.                 Log.w(TAG, "[CodingActivity]->onVideoError");  
  83.             }  
  84.         });  
  85.         rlMain.addView(adView);  
  86.         setContentView(rlMain);  
  87.     }  
  88. }  
二、xml文件添加

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:baiduadsdk="http://schemas.android.com/apk/res/com.dzt.baidu"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:background="#fdfefa"  
  7.     android:orientation="vertical" >  
  8.   
  9.     <com.baidu.mobads.AdView  
  10.         android:id="@+id/adView"  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:layout_alignParentBottom="true"  
  14.         android:layout_alignParentLeft="true"  
  15.         baiduadsdk:adId=""  
  16.         baiduadsdk:adSize="0" />  
  17.     <!-- 注: baiduadsdk:adId可以指定为"",表示默认广告位 -->  
  18.   
  19. </RelativeLayout>  
xmlns:baiduadsdk="http://schemas.android.com/apk/res/com.dzt.baidu" 其中com.dzt.baidu为你app的包名,xmlns:baiduadsdk为自定义控件的属性域

相应的属性文件attrs.xml

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <declare-styleable name="com.baidu.mobads.AdView">  
  4.         <attr name="adSize" format="integer" />  
  5.         <attr name="adId" format="string" />  
  6.     </declare-styleable>   
  7. </resources>  

相应的Demo: http://download.csdn.net/detail/deng0zhaotai/7168479

  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值