基本上有5個步驟:
1.首先需先申請一個admob的帳號,申請完後會得到一組號碼,類似a150e6a6xxxxxxx。
2.下載Google AdMob Ads SDK(下載連結), 解壓縮後會得到一個GoogleAdMobAdsSdk-x.x.x.jar,將這個檔案放到project的libs資料夾下。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.media);
//初始橫幅廣告
//a150e6a6xxxxxxx則是你申請的那一組ID
AdView adView = new AdView(this, AdSize.SMART_BANNER, "a150e6a6xxxxxxx");
LinearLayout layout = (LinearLayout) findViewById(R.id.ads);
layout.addView(adView);
adView.loadAd(new AdRequest());
}
4.加入一個linearLayout來放置橫幅廣告
res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/ads"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</LinearLayout>
</LinearLayout>
5.AndroidManifest.xml需加入二個permission及一個ads的activity
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.vvvvvv"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
<activity android:name="com.example.vvvvvv.MainActivity"
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|screenLayout|uiMode|screenSize|smallestScreenSize"/>
</application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
結果:
避免廣告之嫌,所以馬塞克處理,呵
可否將做好的已加入廣告的檔案,供人下載。謝謝
回覆刪除If you are looking for a solid contextual ad company, I recommend that you take a look at ExoClick.
回覆刪除