纳金网

标题: android中添加admob的方法总结 [打印本页]

作者: 会飞的鱼    时间: 2012-4-17 12:14
标题: android中添加admob的方法总结
首先,你需要去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




欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5