接入穿山甲激励广告

时间:2025-01-18 19:32:30
<uses-permission android:name="${applicationId}..TT_PANGOLIN" />
application 需要配置网络部分,否则网络不可用
android:networkSecurityConfig="@xml/network_security_config"
application标签里,activity同级标签加入
<provider
    android:name=""
    android:authorities="${applicationId}.TTFileProvider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name=".FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
</provider>
新建file_paths.xml文件
6.调起广告代码
private void loadAd(String codeId){
    AdSlot adSlot = new ()
            .setCodeId(codeId)
            //模板广告需要设置期望个性化模板广告的大小,单位dp,激励视频场景,只要设置的值大于0即可
            .setExpressViewAcceptedSize(500,500)
            .setSupportDeepLink(true)
            .setOrientation()//必填参数,期望视频的播放方向: 或
            .setAdLoadType(LOAD)//推荐使用,用于标注此次的广告请求用途为预加载(当做缓存)还是实时加载,方便后续为开发者优化相关策略
            .build();
    (adSlot, new () {
        @Override
        public void onError(int code, String message) {
            (TAG, "Callback --> onError: " + code + ", " + (message));
        }
        //视频广告加载后,视频资源缓存到本地的回调,在此回调后,播放本地视频,流畅不阻塞。
        @Override
        public void onRewardVideoCached() {
            (TAG, "onRewardVideoCached");
        }
        @Override
        public void onRewardVideoCached(TTRewardVideoAd ad) {
            (TAG, "Callback --> onRewardVideoCached");
            if (mttRewardVideoAd != null) {
                //step6:在获取到广告后展示,强烈建议在onRewardVideoCached回调后,展示广告,提升播放体验
                //(不要在这里播放广告,等待视频加载完成,会出现一段空白期,体验不好,除非你弄了预加载)
                //展示广告,并传入广告展示的场景
                //(activity, .CUSTOMIZE_SCENES, "scenes_test");
                //mttRewardVideoAd = null;
            } else {
                (TAG, "请先加载广告");
            }
        }
        //视频广告的素材加载完毕,比如视频url等,在此回调后,可以播放在线视频,网络不好可能出现加载缓冲,影响体验。
        @Override
        public void onRewardVideoAdLoad(TTRewardVideoAd ad) {
            (TAG, "Callback --> onRewardVideoAdLoad");
            mttRewardVideoAd = ad;
            (new () {
                @Override
                public void onAdShow() {
                    (TAG, "Callback --> rewardVideoAd show");
                }
                @Override
                public void onAdVideoBarClick() {
                    (TAG, "Callback --> rewardVideoAd bar click");
                }
                @Override
                public void onAdClose() {
                    (TAG, "Callback --> rewardVideoAd close");
                    ("sendAdClose", "");
                }
                //视频播放完成回调
                @Override
                public void onVideoComplete() {
                    (TAG, "Callback --> rewardVideoAd complete");
                }
                @Override
                public void onVideoError() {
                    (TAG, "Callback --> rewardVideoAd error");
                }
                //视频播放完成后,奖励验证回调,rewardVerify:是否有效,rewardAmount:奖励梳理,rewardName:奖励名称
                @Override
                public void onRewardVerify(boolean rewardVerify, int rewardAmount, String rewardName, int errorCode, String errorMsg) {
                    String logString = "verify:" + rewardVerify + " amount:" + rewardAmount +
                            " name:" + rewardName + " errorCode:" + errorCode + " errorMsg:" + errorMsg;
                    (TAG, "Callback --> " + logString);
                    ("sendAdCanGetR", rewardVerify?"1":"0");
                }
                @Override
                public void onRewardArrived(boolean var1, int var2, Bundle var3){
                    (TAG, "onRewardArrived --> " + var1);
                }
                @Override
                public void onSkippedVideo() {
                    (TAG, "Callback --> rewardVideoAd has onSkippedVideo");
                }
            });
            //在这里播放广告,它是获取到广告视频的url就播放,虽然网速不好会卡,但是现在网速这么快,不会卡的
            //展示广告,并传入广告展示的场景
            //(activity, .CUSTOMIZE_SCENES, "scenes_test");
            //mttRewardVideoAd = null;
        }
    });
}
7.在onDestory中销毁对象
protected void onDestroy() {
    ();
    if (mttRewardVideoAd != null) {
        mttRewardVideoAd = null;
    }
}