Android-在android應用中嵌入廣告的方案

AdMob是一個比較成熟的移動平臺 廣告 商,其爲android 和 iphone提供了非常方便的集成JAR包,使得開發 者可以在自己的應用 中很方便的嵌入其提供的廣告 ,進而按照廣告 展 示和點擊次數付廣告 費。這裏用個例子 給大家演示下如何在自己的應用中集成AdMob的廣告 功能 。

0、準備工作
http://www.admob.com/ 注 冊一個帳號,然後添加一個“Add Mobile Site”,輸入相關信息後,提交完成,進入AD代碼 獲取 界面 ,其提供了PHP,Rails等等類型的代碼,同時 提供了Android平臺使用的JAR,我們就來演示這個。

1、下載 JAR包
其爲android 平臺提供了JAR包,然後將JAR添 加到你的項目 組,按照如下步驟

  • Go to the Properties of your project (right-click on your project from the Package Explorer tab and select Properties)
  • Select “Java Build Path” from left panel
  • Select “Libraries” tab from the main window
  • Click on “Add JARs…”
  • Select the JAR copied to the libs directory
  • Click “OK” to add the SDK to your android project

2、編輯AndroidManifest.xml
Your AdMob publisher ID was given to you when creating your publisher account on www.admob.com before downloading this code. It is a 15-character code like a14a48e3387c5ce . Just before the closing </app lication> tag add a line to set your publisher ID:

1
2
3
4
5
6
7
8
9
10
<!-- The application's publisher ID assigned by AdMob -->
                <meta-data android :value="YOUR_ID_HERE" android :name="ADMOB_PUBLISHER_ID" />
        </application>
 
Set any permissions not already included just before the closing </manifest> tag:
                <!-- AdMob SDK permissions -->
                <uses-permission android :name="android .permission.INTERNET" />
                <uses-permission android :name="android .permission.READ_PHONE_STATE" />
                <uses-permission android :name="android .permission.ACCESS_COARSE_LOCATION" />
        </manifest>

Only the INTERNET permission is required. Setting READ_PHONE_STATE is highly recommended because it identifies the user letting a greater variety of more relevant ads be chosen. Finally setting ACCESS_COARSE_LOCATION (and/or ACCESS_FINE_LOCATION) will let geo-targeted ads be shown.

3、添加attrs.xml
The attrs.xml file specifies custom AdView attributes in XML layout files. If your application does not already have an/res/values/attrs.xml file then create one and copy-and-paste the following into it. If you do have that file then just add the declare-styleable element:

1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="utf-8"?>
        <resources>
                <declare-styleable name="com.admob.android .ads.AdView">
                        <attr name="testing" format="boolean" />
                        <attr name="backgroundColor" format="color" />
                        <attr name="textColor" format="color" />
                        <attr name="keywords" format="string" />
                        <attr name="refreshInterval" format="integer" />
                        <attr name="isGoneWithoutAd" format="boolean" />
                </declare-styleable>
        </resources>

4、Placing an AdView in a Layout
AdView widgets can be put into any XML layout now. The first step is to reference attrs.xml in your layout element by adding an xmlns line that includes your package name specified in AndroidManifest.xml:

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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android ="http://schemas.android .com/apk/res/android "
xmlns:admobsdk ="http://schemas.android .com/apk/res/com.eoe android .demo.Adadmob"
android :orientation="vertical"
android :layout_width="fill_parent"
android :layout_height="fill_parent">
 
<TextView
android :layout_width="fill_parent"
android :layout_height="wrap_content"
android :textSize="24px"
android :paddingBottom="10px"
android :layout_gravity="center"
android :text="eoeAndroid  - 最棒的Android開發社區 " />
<TextView
android :layout_width="fill_parent"
android :layout_height="wrap_content"
android :textSize="18px"
android :paddingBottom="10px"
android :layout_gravity="center"
android :text="eoeAndroid.com 立足於Android開發者 ,營造一個樂於分享、共同進步的開發者社區." />
<com.admob.android .ads.AdView android :id="@+id/ad"
android :layout_width="fill_parent"
android :layout_height="wrap_content"
admobsdk:backgroundColor="#000000"
admobsdk:textColor="#FFFFFF"
admobsdk:keywords="Android application"
admobsdk:refreshInterval="60"
/>
</LinearLayout>

原文鏈接:http://yangguangfu.javaeye.com/blog/675502

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章