AppLovin Max聚合接入-激励广告

本文详细描述了在Android应用中集成AppLovin广告平台的过程,包括包的引入、AdRegistration配置、激励广告的初始化、加载及错误处理机制,如广告加载失败和显示失败时的重试策略。

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

文档

平台选择

错误码

涉及到的平台开发者账号、激励广告id申请、广告瀑布流等这里不做说明。

代码

引入包

implementation 'com.applovin:applovin-sdk:12.4.2'
//以下内容是根据勾选对应广告平台时需要导入的包,具体参考文档平台选择
implementation 'com.applovin.mediation:amazon-tam-adapter:9.9.3.2'
implementation 'com.amazon.android:aps-sdk:9.9.3'
implementation 'com.applovin.mediation:chartboost-adapter:9.6.1.0'
implementation 'com.google.android.gms:play-services-base:16.1.0'
implementation 'com.applovin.mediation:vungle-adapter:7.3.1.2'
implementation 'androidx.recyclerview:recyclerview-selection:1.1.0'
implementation 'com.applovin.mediation:mintegral-adapter:16.6.71.0'
implementation 'com.applovin.mediation:smaato-adapter:22.6.1.0'
implementation 'com.applovin.mediation:unityads-adapter:4.10.0.0'

gradle中

android {
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
}
buildscript {
    repositories {
        maven { url 'https://artifacts.applovin.com/android' }
    }
}

gradle.properties中

android.useAndroidX=true
android.enableJetifier=true

AndroidManifest.xml

<meta-data android:name="applovin.sdk.key"
           android:value="YOUR_SDK_KEY_HERE"/>

初始化

AdRegistration.getInstance( "AMAZON_APP_ID", this );
AdRegistration.setAdNetworkInfo( new DTBAdNetworkInfo( DTBAdNetwork.MAX ) );
AdRegistration.setMRAIDSupportedVersions( new String[] { "1.0", "2.0", "3.0" } );
AdRegistration.setMRAIDPolicy( MRAIDPolicy.CUSTOM );

激励广告初始化、加载(里面已经包含了广告加载成功失败和变现的处理)

private void loadAd()
    {
        if ( isFirstLoad )
        {
            isFirstLoad = false;

            if ( rewardedAd == null )
            {
                  //打开后可以测试广告是否接入成功  
//                AppLovinSdk.getInstance(activity).showMediationDebugger();
                rewardedAd = MaxRewardedAd.getInstance( Reward_Ad_Id, activity );

                rewardedAd.setListener( new MaxRewardedAdListener() {
                    @Override
                    public void onAdLoaded(@NonNull final MaxAd ad)
                    {
                        Log.d(TAG, "onAdLoaded ready to be shown");
                        System.out.println("onAdLoaded ready to be shown");
                        // Rewarded ad is ready to be shown. rewardedAd.isReady() will now return 'true'
//                _reconnectMilliseconds = RECONNECT_TIMER_START_MILLISECONDS;
                        String mediaName = ad.getDspName();
                        Map<String, String> infoMap = new HashMap<>();
                        infoMap.put("mediaName", mediaName);
                        putLoadInfoToScript(true, infoMap);

                        // Reset retry attempt
                        retryAttempt = 0;
                    }

                    @Override
                    public void onAdLoadFailed(@NonNull final String adUnitId, @NonNull final MaxError maxError)
                    {
                        Log.e(TAG, "onAdLoadFailed maxError=000"+maxError);
                        System.out.println("onAdLoadFailed maxError=000 "+maxError);

                        // Rewarded ad failed to load. We recommend retrying with exponentially higher delays up to a maximum delay (in this case 64 seconds).

                        retryAttempt++;
                        long delayMillis = TimeUnit.SECONDS.toMillis( (long) Math.pow( 2, Math.min( 6, retryAttempt ) ) );

                        new Handler().postDelayed(new Runnable()
                        {
                            @Override
                            public void run()
                            {
                                rewardedAd.loadAd();
                            }
                        }, delayMillis );
                    }

                    @Override
                    public void onAdDisplayFailed(@NonNull final MaxAd ad, @NonNull final MaxError maxError)
                    {
                        Log.e(TAG, "onAdDisplayFailedr "+maxError);
                        System.out.println("onAdDisplayFailedr "+maxError);
                        int errorCode = maxError.getCode();
                        Map<String, String> infoMap = new HashMap<>();
                        infoMap.put("status", "4");
                        infoMap.put("message", maxError.getMessage());
                        infoMap.put("domain", maxError.getMediatedNetworkErrorMessage());
                        infoMap.put("errorCode", Integer.toString(errorCode));
                        System.out.println("onAdDisplayFailedr infoMap="+infoMap);
                        putShowStatusToScript(infoMap);

                        // Rewarded ad failed to display. We recommend loading the next ad.
                        rewardedAd.loadAd();
                    }

                    @Override
                    public void onAdDisplayed(@NonNull final MaxAd ad) {}

                    @Override
                    public void onAdClicked(@NonNull final MaxAd ad) {}

                    @Override
                    public void onAdHidden(@NonNull final MaxAd ad)
                    {
                        Log.e(TAG, "onAdHidden ");
                        // Rewarded ad is hidden. Pre-load the the next ad
                        rewardedAd.loadAd();
                    }

                    @Override
                    public void onUserRewarded(@NonNull final MaxAd ad, @NonNull final MaxReward reward)
                    {
                        Log.e(TAG, "onUserRewarded ");
                        System.out.println("onUserRewarded ");
                        // Rewarded ad was displayed and user should receive the reward.
                        int rewardAmount = reward.getAmount();
                        String rewardType = reward.getLabel();
                        Map<String, String> infoMap = new HashMap<>();
                        infoMap.put("status", "3");
                        infoMap.put("rewardType", rewardType);
                        infoMap.put("rewardAmount", Integer.toString(rewardAmount));
                        infoMap.put("mediaName", ad.getNetworkName());
                        Log.e(TAG, "onUserRewarded infoMap="+infoMap);
                        //调用游戏也出去处理奖励
                        putShowStatusToScript(infoMap);
                    }

                } );

                //region MAX Ad Revenue Listener
                rewardedAd.setRevenueListener(new MaxAdRevenueListener() {
                    @Override
                    public void onAdRevenuePaid(@NonNull MaxAd maxAd) {

                        AdjustAdRevenue adjustAdRevenue = new AdjustAdRevenue( AdjustConfig.AD_REVENUE_APPLOVIN_MAX );
                        adjustAdRevenue.setRevenue( maxAd.getRevenue(), "USD" );
                        adjustAdRevenue.setAdRevenueNetwork( maxAd.getNetworkName() );
                        adjustAdRevenue.setAdRevenueUnit( maxAd.getAdUnitId() );
                        adjustAdRevenue.setAdRevenuePlacement( maxAd.getPlacement() );
                        System.out.println("setRevenueListener adjustAdRevenue="+adjustAdRevenue);

                        Adjust.trackAdRevenue( adjustAdRevenue );
                    }
                });
            }

            DTBAdRequest adLoader = new DTBAdRequest();

            // Switch video player width and height values(320, 480) depending on device orientation

            DisplayMetrics dm = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
            int width = dm.widthPixels;
            int height = dm.heightPixels;
            System.out.println("width = "+width+" height="+height);
            //设置显示区域大小
            adLoader.setSizes( new DTBAdSize.DTBVideo( width, height, "<AMAZON_VIDEO_REWARDED_SLOT_ID>" ) );
            adLoader.loadAd( new DTBAdCallback()
            {
                @Override
                public void onSuccess(@NonNull final DTBAdResponse dtbAdResponse)
                {
                    // 'rewardedAd' is your instance of MaxRewardedAd
                    rewardedAd.setLocalExtraParameter( "amazon_ad_response", dtbAdResponse );
                    rewardedAd.loadAd();
                }

                @Override
                public void onFailure(@NonNull final AdError adError)
                {
                    // 'rewardedAd' is your instance of MaxRewardedAd
                    rewardedAd.setLocalExtraParameter( "amazon_ad_error", adError );
                    rewardedAd.loadAd();
                }
            } );
        }
        else
        {
            rewardedAd.loadAd();
        }
    }

点击展示广告

public static void showAd()
{
    Log.d(TAG, "showAd");
    System.out.println("showAd");
    if(rewardedAd == null){
        if(_showAdCallback != null){
            Map<String, String> infoMap = new HashMap<>();
            infoMap.put("status", "0");
            infoMap.put("adIsOK", "0");
            Log.d(TAG, "showAd return");
            System.out.println("showAd return infoMap="+infoMap);
            putShowStatusToScript(infoMap);
        }
        return;
    }

    if ( rewardedAd.isReady() )
    {
        Log.d(TAG, "showAd isReady show");
        System.out.println("showAd isReady show");
        rewardedAd.showAd();
    } else {
        Log.d(TAG, "showAd not isReady!!!");
        System.out.println("showAd not isReady!!!");
        Map<String, String> infoMap = new HashMap<>();
        infoMap.put("status", "0");
        infoMap.put("adIsOK", "0");
        Log.d(TAG, "showAd return");
        System.out.println("showAd return infoMap="+infoMap);
        putShowStatusToScript(infoMap);
    }
}

函数putShowStatusToScript(infoMap)是处理广告函数。具体内容根据业务去处理

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值