链接:
sdk导入:
把 AmazonMobileAds 中的 amazon-ads-xxx.jar 导入到工程中
创建界面配置
新建linearlayout配置 命名:amazon_ad_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:Amazon="http://schemas.android.com/apk/lib/com.amazon.device.ads"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top|center_horizontal"
android:orientation="vertical">
<com.amazon.device.ads.AdLayout
android:id="@+id/adView"
android:layout_width="320dp"
android:layout_height="50dp"
Amazon:adSize="320x50"/>
</LinearLayout>
内部代码:
创建内部变量
protected AdLayout adView;
加载配置
setContentView(R.layout.amazon_ad_layout);
manifest.xml配置 :
<activity
android:name="com.amazon.device.ads.AdActivity"
android:configChanges="keyboardHidden|orientation|screenSize">
</activity>
纯代码配置:
不使用 setContentView 配置方法
首先是加载自己广告的adKey, 去开发中心创建key存回来
try {
AdRegistration.setAppKey(APP_KEY);
} catch (final IllegalArgumentException e) {
Log.e("GC_amazon_adview", "IllegalArgumentException thrown: " + e.toString());
return;
}
创建广告需要的框架
adViewContainer = new LinearLayout(this);
adViewContainer.setOrientation(LinearLayout.VERTICAL);
adViewContainer.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL);
addContentView(adViewContainer, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
这里需要对当前显示的主类做个单例来调取, 因为不在一个线程, 所以直接访问是出错的
adViewContainer.post(new Runnable() {
@Override
public void run() {
adView = new AdLayout({你的主类}.actInstance, AdSize.SIZE_300x50);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
adViewContainer.addView(adView,layoutParams);
adView.loadAd(new AdTargetingOptions());
}
});
销毁
onDestry中一定要写销毁
adView.destroy();
adView = null;
adViewContainer.post(new Runnable() {
@Override
public void run() {
adViewContainer.removeAllViews();
}
});
debug开启测试:
AdRegistration.enableLogging(true);
AdRegistration.enableTesting(true);
底部位置问题:
LinearLayout.LayoutParams.MATCH_PARENT要做双向填充, 纵向也要, 官方教程广告是在上部所以没有这么做
addContentView(adViewContainer, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));