`
tank2308635
  • 浏览: 188848 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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 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里面写入下面的代码 

 

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"?>
<resources>
    <declare-styleable name="com.google.ads.AdView">
        <attr name="adSize">
            <enum name="BANNER" value="1" />
            <enum name="IAB_MRECT" value="2" />
            <enum name="IAB_BANNER" value="3" />
            <enum name="IAB_LEADERBOARD" value="4" />
        </attr>
        <attr name="adUnitId" format="string" />
    </declare-styleable>
</resources>

 

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"
              android:orientation="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="你的发布者id"
                         ads:adSize="BANNER"
                         ads:loadAdOnCreate="true"/>
</LinearLayout>

 

这样就ok了,可以编译运行。


如果在main.xml文件中不想开始就运行广告的话,可以去掉ads:loadAdOnCreate=&quot;true&quot;,然后在Activty中想要运行广告的地方加入代码

AdView adView = (AdView)this.findViewById(R.id.adView);
adView.loadAd(new AdRequest());

 

  • 大小: 1.6 KB
  • 大小: 3.1 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics