Skip to content

一、简介

主要介绍在 Android 项目中如何集成 UNAD SDK 的开屏广告:

二、代码集成

1、在自己的 Application 中初始化 SDK

TEST_APPID 为测试 appid,上线请替换正式的 APPID

java
UNAD.initialize(
    new UNADConfig.Builder()
        // true-屏蔽个性化推荐广告(关闭),false-不屏蔽个性化推荐广告(打开),默认false
        .setPersonalRecommend(false)
        .setDebug(false).build(), "TEST_APPID", this,
            new UNAD.InitCallback() {
                @Override
                public void onSuccess() {
                    Log.e("unadsdk", "UI:onSuccess");
                }

                @Override
                public void onError(UnadError error) {
                    Log.e("unadsdk", "UI:onError");
                }});

2、创建 Activity 设置为 MAIN 入口

java
public class SplashActivity extends Activity implements UNADSplashAdLoader.UNADSplashADListener
xml
<activity
    android:name=".SplashActivity"
    android:label="@string/app_name"
    android:exported="true"
    android:launchMode="singleTask"
    android:screenOrientation="portrait"
    android:windowSoftInputMode="stateHidden|stateAlwaysHidden"
    android:theme="@style/Theme.Testsdk.NoActionBar">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

3、创建布局文件

xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--  避免第一次授权时界面空白 这里设置自己的启动背景图片和logo-->
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/white">
            <!--  启动时默认的logo-->
            <ImageView
                android:id="@+id/app_logo"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:src="@drawable/ad_logo" />
            <!--  启动时默认的背景-->
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_above="@id/app_logo"
                android:scaleType="fitXY"
                android:src="@drawable/ad_splash_holder" />
        </RelativeLayout>
	  <!-- 开屏传入进去的布局 这个布局文件不能隐藏不然不会计算收入-->
        <LinearLayout
            android:id="@+id/splashLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"></LinearLayout>
    </FrameLayout>


</LinearLayout>

4、初始化对象并且打开广告并监听

测试广告位 ID TEST_UNIT_ID 应用上线时请替换为正式的广告位 ID

java
/*
 *  activity activity
 *  splashAdId 广告位ID
 *  layout  要添加到布局文件
 *  fetchDelay 拉取广告的超时时长:取值范围[3000, 5000],设为0默认的超时时长。
 *  isFullScreen 是否全屏 全屏不显示LOGO
 *  logoid 启动logo图片id
 *  logobackgroundid APP启动背景ID
 *  unadSplashADListener 监听器
 */
UNADSplashAdLoader unadSplashAdLoader = new UNADSplashAdLoader(activity, posId, adContainer, fetchDelay, true, R.drawable.ad_logo, R.drawable.ad_splash_holder, this);

//打开广告
unadSplashAdLoader.loadAndShow();


public interface UNADSplashADListener {
    //关闭
    void onADDismissed();
		//错误
    void onADError(UnadError var1);
		//展示
    void onADPresent();
		//点击
    void onADClicked();
		//倒计时有些没有
    void onADTick(long var1);
		//收到广告加载成功
    void onADReceive(long var1);
}

5、加载广告

java
unadSplashAdLoader.loadAD();

6、打开广告

java
在广告监听中等广告加载成功后再打开
 public void onADReceive(long expireTimestamp) {
		if(!isShow){
            // 隐藏自己的开机界面
            splashbg.setVisibility(View.GONE);
            isShow=true;
            //显示广告
            unadSplashAdLoader.showAD();
        }
 }

7、销毁广告

java
protected void onDestroy() {
    super.onDestroy();
    if(null!=unadSplashAdLoader){
        unadSplashAdLoader.destroy();
    }
}

8、自定义下载信息展示

自定义下载提示

java
//自定义下载 代码参考DEMO
unadSplashAdLoader.setDownloadConfirmListener(DownloadConfirmHelper.DOWNLOAD_CONFIRM_LISTENER );

9、个性化广告的开关

初始化参数可以设置可性化是否开和关 setPersonalRecommend

java
new UNADConfig.Builder()
        // true-屏蔽个性化推荐广告(关闭),false-不屏蔽个性化推荐广告(打开),默认false
        .setPersonalRecommend(false)
        .setDebug(true).build()

10、注意事项

1、在调试时如果出现 5004 或者 102006 等错误码时可能是当前设备请求广告过于频繁,请换台设备或稍后重试,或者多次点击几下;

2、测试广告位 ID TEST_UNIT_ID 应用上线时请替换为正式的广告位 ID

3、参考 DEMO 开屏代码(注意 demo 中开屏 activity 生命周期内方法)