Android App添加广告

在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包下载,解压后的目录

Android App添加广告_第1张图片

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

目录结构<喎�"http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PGltZyBzcmM9"http://www.2cto.com/uploadfile/Collfiles/20140410/2014041009321272.png" alt="\">

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<!--?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" >
 
     
         <meta-data android:name= "BaiduMobAd_APP_ID" android:value= "debug" >
         <meta-data android:name= "BaiduMobAd_APP_SEC" android:value= "debug" >
 
         
         
         
         
         
         
             <intent-filter>
                 
 
                 <category android:name= "android.intent.category.LAUNCHER" >
             </category></action></intent-filter>
         </activity>
     </activity></activity></activity></activity></activity></meta-data></meta-data></application>
 
</uses-permission></uses-permission></uses-permission></uses-permission></uses-permission></uses-permission></uses-permission></uses-permission></uses-permission></uses-permission></uses-permission></uses-permission></uses-sdk></manifest>


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

AdActivity.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<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><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><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><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" >
 
</button></textview></relativelayout>


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

一、代码添加

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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文件添加

?
1
2
3
4
5
6
7
<!--?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可以指定为 "" ,表示默认广告位 -->
 
</com.baidu.mobads.adview></relativelayout>

xmlns:baiduadsdk="http://schemas.android.com/apk/res/com.dzt.baidu" 其中com.dzt.baidu为你app的包名,xmlns:baiduadsdk为自定义控件的属性域

相应的属性文件attrs.xml

?
1
2
3
4
5
6
7
<!--?xml version= "1.0" encoding= "utf-8" ?-->
<resources>
     <declare-styleable name= "com.baidu.mobads.AdView" >
         
         
     </attr></attr></declare-styleable>
</resources>


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

转载:点击打开链接

你可能感兴趣的:(Android App添加广告)