什么是開屏廣告
開屏廣告是一種在應用啟動時且在應用主界面顯示之前需要被展示的廣告。一般是5s展示時間,廣告展示時間結束后自動進入應用,用戶可以點擊跳過按鈕直接進入主界面。
開屏廣告示例
開屏廣告的優勢
位置優勢:用戶在進入App前就會看到開屏廣告,相比於應用內廣告提前,並且只要使用App的用戶就要強制觀看。
展示面積大:廣告全屏顯示,視覺沖擊力很強,便於優質內容曝光,吸引用戶眼球,增強用戶點擊率與品牌曝光度。
當用戶剛打開應用時,用戶覆蓋面廣,用戶注意力集中。因此開屏廣告適用於廣告主進行大規模的品牌宣傳和產品推廣。
華為廣告服務能夠幫助開發者接入包括開屏廣告在內的6種廣告位。接下來的文章會詳細講解開屏廣告的開發步驟。示例代碼已在相關社區進行開源,歡迎開發者關注、下載並提供寶貴意見:
Github官方地址:https://github.com/hms-core/hms-ads-demo-java
Gitee官方地址:https://gitee.com/hms-core/hms-ads-demo-java
前提條件
HUAWEI Ads SDK依賴HMS Core(APK)4.0.0.300及以上版本。如果設備上未安裝HMS Core(APK)4.0.0.300及以上版本,則無法使用HUAWEI Ads SDK的相關接口。
在開發應用前需要在華為開發者聯盟網站上注冊成為開發者並完成實名認證,具體方法可參見帳號注冊認證。
開發前准備
廣告服務的集成需如下4個關鍵步驟,可以參考華為開發者聯盟文檔
1. 導入HUAWEI Ads SDK
2. 配置網絡權限
3. 配置混淆腳本
4. 初始化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、應用內添加貼片廣告
>>訪問華為廣告服務官網,了解更多相關內容
>>獲取華為廣告服務開發指導文檔
>>訪問華為開發者聯盟官網,了解更多相關內容
>>獲取開發指導文檔
原文鏈接:developer.huawei.com/consumer/cn…
原作者:胡椒