亚马逊 Amazon Ad 加入Eclipse

本文详细介绍了如何在Android应用中集成亚马逊广告SDK,包括SDK导入、界面布局配置、内部代码实现及广告展示流程。此外还提供了调试建议和注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链接:

layout

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));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值