查看: 1666|回复: 0
打印 上一主题 下一主题

android中添加admob的方法总结

[复制链接]

5552

主题

2

听众

8万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
11

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2012-4-17 12:14:21 |只看该作者 |倒序浏览
首先,你需要去admob官网(http://zhcn.admob.com)注册一个账户;
然后按照官网的提示,添加站点和应用程序
添加好了,就可以获得发布者ID和admob的jar包

好了,获得的jar包,就可以根据http://code.google.com/intl/zh-CN/mobile/ads/docs/android/fundamentals.html介绍的方法为我们的应用添加广告

1. Right click on your app project in Eclipse and choose Properties




2. Select Java Build Path and the Libraries tab. Then click Add External JARs... to add the Google AdMob Ads JAR.




3.instantiate a com.google.ads.AdActivity in its AndroidManifest.xml and make ad requests requires the networking permissions INTERNET and ACCESS_NETWORK_STATE
Xml代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.hsj.test"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".UnityAdActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>
        </activity>
<activity android:name="com.google.ads.AdActivity"
              android:configChanges="keyboard|keyboardHidden|orientation"/>
  </application>
  <uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

</manifest>
接下来,有两种方式在你的应用中加入广告:

方式一  在你的Activity里面写入下面的代码
Java代码:
public class UnityAdActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create the adView
AdView adView = new AdView(this, AdSize.BANNER, "你的发布者ID");
LinearLayout layout = (LinearLayout) findViewById(R.id.adView);
// Add the adView to it
layout.addView(adView);
// Initiate a generic request to load it with an ad
adView.loadAd(new AdRequest());
}
}
这样就ok了,可以编译程序运行试一下。

方式二 不改动代码,只改动布局文件main.xml和attrs.xml
attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
              androidrientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">
  <com.google.ads.AdView android:id="@+id/adView"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         ads:adUnitId="a14e1fe17a70594"
                         ads:adSize="BANNER"
                         ads:loadAdOnCreate="***e"/>
</LinearLayout>
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
              androidrientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">
  <com.google.ads.AdView android:id="@+id/adView"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         ads:adUnitId="a14e1fe17a70594"
                         ads:adSize="BANNER"
                         ads:loadAdOnCreate="***e"/>
</LinearLayout>
这样就ok了,可以编译运行。
如果在main.xml文件中不想开始就运行广告的话,可以去掉ads:loadAdOnCreate=&quot;***e&quot;,然后在Activty中想要运行广告的地方加入代码

AdView adView = (AdView)this.findViewById(R.id.adView);
adView.loadAd(new AdRequest());
本文转自:http://tank2308635.iteye.com/blog/1125903
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-5-19 03:22 , Processed in 0.085411 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部