什么是开屏广告
开屏广告是一种在利用启动时且在利用主界面显示之前须要被展现的广告。个别是5s展现工夫,广告展现工夫完结后主动进入利用,用户能够点击跳过按钮间接进入主界面。
开屏广告示例
开屏广告的劣势
地位劣势:用户在进入App前就会看到开屏广告,相比于利用内广告提前,并且只有应用App的用户就要强制观看。
展现面积大:广告全屏显示,视觉冲击力很强,便于优质内容曝光,吸引用户眼球,加强用户点击率与品牌曝光度。
当用户刚关上利用时,用户覆盖面广,用户注意力集中。因而开屏广告实用于广告主进行大规模的品牌宣传和产品推广。
华为广告服务可能帮忙开发者接入包含开屏广告在内的6种广告位。接下来的文章会具体解说开屏广告的开发步骤。示例代码已在相干社区进行开源,欢送开发者关注、下载并提供宝贵意见:
Github官网地址:https://github.com/hms-core/h...
Gitee官网地址:https://gitee.com/hms-core/hm...
前提条件
HUAWEI Ads SDK依赖HMS Core(APK)4.0.0.300及以上版本。如果设施上未装置HMS Core(APK)4.0.0.300及以上版本,则无奈应用HUAWEI Ads SDK的相干接口。
在开发利用前须要在华为开发者联盟网站上注册成为开发者并实现实名认证,具体方法可参见帐号注册认证。
参见创立我的项目和在我的项目中增加利用实现利用的创立。
开发前筹备
广告服务的集成需如下4个关键步骤,能够参考华为开发者联盟文档
- 导入HUAWEI Ads SDK
- 配置网络权限
- 配置混同脚本
- 初始化SDK
1.1 增加SplashView。
在XML布局文件中增加SplashView。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context=".SplashActivity"> <!-- 开屏广告Logo区域 --> <RelativeLayout android:id="@+id/logo_area" android:layout_width="match_parent" android:layout_height="100dp" android:layout_alignParentBottom="true" android:background="@android:color/white" android:visibility="visible"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="40dp" android:orientation="vertical"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginBottom="6dp" android:gravity="center" android:orientation="horizontal"> <ImageView android:layout_width="28dp" android:layout_height="28dp" android:background="@mipmap/ic_launcher" /> <View android:layout_width="0.5dp" android:layout_height="18dp" android:layout_marginLeft="12dp" android:layout_marginRight="12dp" android:alpha="0.1" android:background="@android:color/black" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:alpha="1" android:text="@string/owner" android:textColor="@android:color/black" android:textSize="16sp" /> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:alpha="0.5" android:text="@string/copyright_info" android:textColor="@android:color/black" android:textSize="8sp" /> </LinearLayout> </RelativeLayout> <!-- 开屏广告视图 --> <com.huawei.hms.ads.splash.SplashView android:id="@+id/splash_ad_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/logo" /> </RelativeLayout>
以下示例代码展现了如何获取SplashView
SplashView splashView = findViewById(R.id.splash_ad_view);
1.2 批改利用默认启动页面。
开屏广告是在利用主界面显示之前被展现,所以需批改利用默认启动页面。
批改AndroidManifest.xml, 将默认启动的activity批改为SplashActivity,这样即可在利用主界面加载前展现开屏广告。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.huawei.hms.ads.sdk"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:exported="false" android:screenOrientation="portrait"> </activity> <activity android:name=".SplashActivity" android:exported="true" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> ... </application> </manifest>
创立SplashActivity.java类,用于实现开屏广告获取和展现。
... import android.os.Build; import androidx.appcompat.app.AppCompatActivity; public class SplashActivity extends AppCompatActivity { // "testq6zq98hecj"为测试专用的广告位ID, App正式公布时须要改为正式的广告位ID private static final String AD_ID = "testq6zq98hecj"; private static final int AD_TIMEOUT = 5000; private static final int MSG_AD_TIMEOUT = 1001; /** * 暂停标记位。 * 在开屏广告页面展现时: * 按返回键退出利用时需设置为true,以确保利用主界面不被拉起; * 切换至其余界面时需设置为false,以确保从其余页面回到开屏广告页面时依然能够失常跳转至利用主界面; */ private boolean hasPaused = false; // 收到广告展现超时音讯时的回调解决 private Handler timeoutHandler = new Handler(new Handler.Callback() { @Override public boolean handleMessage(@NonNull Message msg) { if (SplashActivity.this.hasWindowFocus()) { jump(); } return false; } }); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash); // 获取并展现开屏广告 loadAd(); } /** * 广告展现结束时,从广告界面跳转至App主界面 */ private void jump() { if (!hasPaused) { hasPaused = true; startActivity(new Intent(SplashActivity.this, MainActivity.class)); finish(); } } /** * 按返回键退出利用时需设置为true,以确保利用主界面不被拉起 */ @Override protected void onStop() { // 移除音讯队列中期待的超时音讯 timeoutHandler.removeMessages(MSG_AD_TIMEOUT); hasPaused = true; super.onStop(); } /** * 从其余页面回到开屏页面时调用,进入利用主界面 */ @Override protected void onRestart() { super.onRestart(); hasPaused = false; jump(); } @Override protected void onDestroy() { super.onDestroy();
1.3 获取广告。
SplashView创立好之后,通过SplashView类的load()办法来获取广告。
private void loadAd() { int orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; AdParam adParam = new AdParam.Builder().build(); SplashView.SplashAdLoadListener splashAdLoadListener = new SplashView.SplashAdLoadListener() { @Override public void onAdLoaded() { // 广告获取胜利时调用 ... } @Override public void onAdFailedToLoad(int errorCode) { // 广告获取失败时调用, 跳转至App主界面 jump(); } @Override public void onAdDismissed() { // 广告展现结束时调用, 跳转至App主界面 jump(); } }; // 获取SplashView SplashView splashView = findViewById(R.id.splash_ad_view); // 设置默认Slogan splashView.setSloganResId(R.drawable.default_slogan); // 设置视频类开屏广告的音频焦点类型 splashView.setAudioFocusType(AudioFocusType.NOT_GAIN_AUDIO_FOCUS_WHEN_MUTE); // 获取广告,其中AD_ID为广告位ID splashView.load(AD_ID, orientation, adParam, splashAdLoadListener); // 发送延时音讯,保障广告显示超时后,APP首页能够失常显示 timeoutHandler.removeMessages(MSG_AD_TIMEOUT); timeoutHandler.sendEmptyMessageDelayed(MSG_AD_TIMEOUT, AD_TIMEOUT);
1.4 监听广告事件。
通过实现SplashAdDisplayListener类中的办法来监听广告展现类事件。理解具体办法,请参见API文档中的SplashAdDisplayListener类。
SplashAdDisplayListener adDisplayListener = new SplashAdDisplayListener() { @Override public void onAdShowed() { // 广告显示时调用 ... } @Override public void onAdClick() { // 广告被点击时调用 ... } }; splashView.setAdDisplayListener(adDisplayListener);
更多利用内广告模式操作指南:
1、利用内增加Banner广告位
2、利用内增加激励广告
3、利用内增加原生广告
4、利用内增加开屏广告
5、利用内增加插屏广告
6、利用内增加贴片广告
拜访华为广告服务官网,理解更多相干内容
获取华为广告服务开发领导文档
拜访华为开发者联盟官网,理解更多相干内容
获取开发领导文档
华为挪动服务开源仓库地址:GitHub、Gitee
原文链接:https://developer.huawei.com/...
原作者:胡椒