接入穿山甲激励广告

1.初始化
在MainActivity的onCreate中初始化
protected void onCreate(){
    ..//
    adInit();
}
//代码部分
TTAdManager ttAdManager;
private TTAdNative mTTAdNative;
private TTRewardVideoAd mttRewardVideoAd;
private void adInit(){
    //强烈建议在应用对应的Application#onCreate()方法中调用,避免出现content为null的异常
    TTAdSdk.init(this,
            new TTAdConfig.Builder()
                    .appId(Constants.App_Ad_key)
                    .useTextureView(true) //默认使用SurfaceView播放视频广告,当有SurfaceView冲突的场景,可以使用TextureView
                    .appName(Constants.App_Name)
                    .titleBarTheme(TTAdConstant.TITLE_BAR_THEME_DARK)//落地页主题
                    .allowShowNotify(true) //是否允许sdk展示通知栏提示,若设置为false则会导致通知栏不显示下载进度
                    .debug(true) //测试阶段打开,可以通过日志排查问题,上线时去除该调用
                    .directDownloadNetworkType(TTAdConstant.NETWORK_STATE_WIFI) //允许直接下载的网络状态集合,没有设置的网络下点击下载apk会有二次确认弹窗,弹窗中会披露应用信息
                    .supportMultiProcess(false) //是否支持多进程,true支持
                    .build(),
            new TTAdSdk.InitCallback(){
                /**
                 * 初始化成功回调
                 * 注意:开发者需要在success回调之后再去请求广告
                 */
                @Override
                public void success(){
                    Log.e(TAG, "init suc");
                    ttAdManager = TTAdSdk.getAdManager();
                    mTTAdNative = ttAdManager.createAdNative(activity);
                }
                /**
                 * @param code 初始化失败回调错误码
                 * @param msg 初始化失败回调信息
                 */
                @Override
                public void fail(int code, String msg){
                    Log.e(TAG, "init fail"+code+"--"+msg);
                }
            }
    );
}
2.导入jar包
3.为了适配下载和安装相关功能
implementation 'androidx.legacy:legacy-support-v4:1.0.0'

4.防止初始化报错 code=4201 msg=no initializer
//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:28.0.0'
5.AndroidMainfest.xml配置
加权限



    android:protectionLevel="signature" />


application 需要配置网络部分,否则网络不可用
android:networkSecurityConfig="@xml/network_security_config"
application标签里,activity同级标签加入
    android:name="com.bytedance.sdk.openadsdk.TTFileProvider"
    android:authorities="${applicationId}.TTFileProvider"
    android:exported="false"
    android:grantUriPermissions="true">
             android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />

新建file_paths.xml文件
6.调起广告代码
private void loadAd(String codeId){
    AdSlot adSlot = new AdSlot.Builder()
            .setCodeId(codeId)
            //模板广告需要设置期望个性化模板广告的大小,单位dp,激励视频场景,只要设置的值大于0即可
            .setExpressViewAcceptedSize(500,500)
            .setSupportDeepLink(true)
            .setOrientation(TTAdConstant.VERTICAL)//必填参数,期望视频的播放方向:TTAdConstant.HORIZONTAL 或 TTAdConstant.VERTICAL
            .setAdLoadType(LOAD)//推荐使用,用于标注此次的广告请求用途为预加载(当做缓存)还是实时加载,方便后续为开发者优化相关策略
            .build();
    mTTAdNative.loadRewardVideoAd(adSlot, new TTAdNative.RewardVideoAdListener() {
        @Override
        public void onError(int code, String message) {
            Log.e(TAG, "Callback --> onError: " + code + ", " + String.valueOf(message));
        }
        //视频广告加载后,视频资源缓存到本地的回调,在此回调后,播放本地视频,流畅不阻塞。
        @Override
        public void onRewardVideoCached() {
            Log.e(TAG, "onRewardVideoCached");
        }
        @Override
        public void onRewardVideoCached(TTRewardVideoAd ad) {
            Log.e(TAG, "Callback --> onRewardVideoCached");
            if (mttRewardVideoAd != null) {
                //step6:在获取到广告后展示,强烈建议在onRewardVideoCached回调后,展示广告,提升播放体验
                //(不要在这里播放广告,等待视频加载完成,会出现一段空白期,体验不好,除非你弄了预加载)
                //展示广告,并传入广告展示的场景
                //mttRewardVideoAd.showRewardVideoAd(activity, TTAdConstant.RitScenes.CUSTOMIZE_SCENES, "scenes_test");
                //mttRewardVideoAd = null;
            } else {
                Log.e(TAG, "请先加载广告");
            }
        }
        //视频广告的素材加载完毕,比如视频url等,在此回调后,可以播放在线视频,网络不好可能出现加载缓冲,影响体验。
        @Override
        public void onRewardVideoAdLoad(TTRewardVideoAd ad) {
            Log.e(TAG, "Callback --> onRewardVideoAdLoad");
            mttRewardVideoAd = ad;
            mttRewardVideoAd.setRewardAdInteractionListener(new TTRewardVideoAd.RewardAdInteractionListener() {
                @Override
                public void onAdShow() {
                    Log.d(TAG, "Callback --> rewardVideoAd show");
                }
                @Override
                public void onAdVideoBarClick() {
                    Log.d(TAG, "Callback --> rewardVideoAd bar click");
                }
                @Override
                public void onAdClose() {
                    Log.d(TAG, "Callback --> rewardVideoAd close");
                    nativeAndroid.callExternalInterface("sendAdClose", "");
                }
                //视频播放完成回调
                @Override
                public void onVideoComplete() {
                    Log.d(TAG, "Callback --> rewardVideoAd complete");
                }
                @Override
                public void onVideoError() {
                    Log.e(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;
                    Log.e(TAG, "Callback --> " + logString);
                    nativeAndroid.callExternalInterface("sendAdCanGetR", rewardVerify?"1":"0");
                }
                @Override
                public void onRewardArrived(boolean var1, int var2, Bundle var3){
                    Log.e(TAG, "onRewardArrived --> " + var1);
                }
                @Override
                public void onSkippedVideo() {
                    Log.e(TAG, "Callback --> rewardVideoAd has onSkippedVideo");
                }
            });
            //在这里播放广告,它是获取到广告视频的url就播放,虽然网速不好会卡,但是现在网速这么快,不会卡的
            //展示广告,并传入广告展示的场景
            //mttRewardVideoAd.showRewardVideoAd(activity, TTAdConstant.RitScenes.CUSTOMIZE_SCENES, "scenes_test");
            //mttRewardVideoAd = null;
        }
    });
}
7.在onDestory中销毁对象
protected void onDestroy() {
    super.onDestroy();
    if (mttRewardVideoAd != null) {
        mttRewardVideoAd = null;
    }
}

你可能感兴趣的:(android,android)