Huawei Adsの実装方法－スプラッシュ広告編

# Huawei Adsの実装方法－スプラッシュ広告編
[スプラッシュ広告](https://developer.huawei.com/consumer/jp/doc/development/HMSCore-Guides-V5/publisher-service-splash-0000001050066919-V5)はアプリの起動時の表示される広告です。
<img width="300" alt="image.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/704548/97091614-f60a-cd55-6068-586635f05dcd.png">

### Ad slot ID
[Huawei Adsの実装方法－準備編](https://qiita.com/Rei_2020/items/9f9eb35f781051eba811)の[Ad slot ID](https://qiita.com/Rei_2020/items/9f9eb35f781051eba811#ad-slot-id)をご参照ください。
オフィシャルのテスト用Ad slot IDは
<b>testq6zq98hecj</b>
です。

### 実装の要点
1. スプラッシュ広告のActivityを作る
2. アプリ起動時にまずスプラッシュ広告のActivityを起動する
3. スプラッシュ広告の表示が完了後にメインActivityに遷移する

### スプラッシュ広告のActivityのレイアウト
必要なのはcom.huawei.hms.ads.splash.SplashViewだけです。

```xml:splash_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <com.huawei.hms.ads.splash.SplashView
            android:id="@+id/splash_ad_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            />

    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>
```

### Activity
広告終了時（onAdDismissed）、または広告ロード失敗時（onAdFailedToLoad）、次のActivityに遷移します。

```kotlin：SplashActivity.kt
class SplashActivity : AppCompatActivity() {

    private lateinit var binding: SplashActivityBinding
    private var jump = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = SplashActivityBinding.inflate(layoutInflater)
        setContentView(binding.root)
        HwAds.init(applicationContext)

        loadAd()
    }

    override fun onResume() {
        super.onResume()
        binding.splashAdView.resumeView()
    }

    override fun onPause() {
        super.onPause()
        binding.splashAdView.pauseView()
    }

    override fun onDestroy() {
        binding.splashAdView.destroyView()
        super.onDestroy()
    }

    private fun loadAd() {
        val adParam = AdParam.Builder().build()
        binding.splashAdView.setAdDisplayListener(object : SplashAdDisplayListener() {
            override fun onAdShowed() {

            }
            override fun onAdClick() {

            }
        })
        binding.splashAdView.setLogoResId(R.drawable.ic_launcher_foreground)
        binding.splashAdView.setMediaNameResId(R.string.app_name)
        binding.splashAdView.setAudioFocusType(AudioFocusType.NOT_GAIN_AUDIO_FOCUS_WHEN_MUTE)
        binding.splashAdView.load(
            getString(R.string.ad_id_splash),
            ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
            adParam,
            object : SplashAdLoadListener() {
                override fun onAdLoaded() {

                }

                override fun onAdFailedToLoad(errorCode: Int) {
                    jump()
                }

                override fun onAdDismissed() {
                    jump()
                }
            }
        )
    }

    private fun jump() {
        if (!jump) {
            jump = true
            val intent = Intent(this@SplashActivity, MainActivity::class.java).apply {
                // バックボタンを押しても、スプラッシュアクティビティに戻らないようにする
                flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
            }
            startActivity(intent)
            finish()
        }
    }
}
```

# Huawei Adsシリーズ
- [Huawei Adsの機能のまとめ](https://qiita.com/Rei_2020/items/87804f2057d4b143fc12)
- [Huawei Adsの実装方法－準備編](https://qiita.com/Rei_2020/items/9f9eb35f781051eba811)
- [Huawei Adsの実装方法－広告用識別子（OAID）編](https://qiita.com/Rei_2020/items/f5446a2bbbb4d5d3df67)
- [Huawei Adsの実装方法－インストールリファラー編](https://qiita.com/Rei_2020/items/977eabb3a514ba5c2476)
- [Huawei Adsの実装方法－バナー広告編](https://qiita.com/Rei_2020/items/308ebf626c1423a7cb1f)
- [Huawei Adsの実装方法－ネイティブ広告編](https://qiita.com/Rei_2020/items/f60976c23c1268261d5f)
- [Huawei Adsの実装方法－リワード広告編](https://qiita.com/Rei_2020/items/06744db6572c1603b0bd)
- [Huawei Adsの実装方法－インタースティシャル広告編](https://qiita.com/Rei_2020/items/3c9d9a19861a2d139737)
- [Huawei Adsの実装方法－スプラッシュ広告編](https://qiita.com/Rei_2020/items/5a138945d7b343602df1)

# GitHub
HMS Ads Kit Demo : [https://github.com/Rei2020GitHub/MyPublicProject/tree/master/AdsDemo](https://github.com/Rei2020GitHub/MyPublicProject/tree/master/AdsDemo)

# 参考
* HMS：[https://developer.huawei.com/consumer/jp/](https://developer.huawei.com/consumer/jp/)
* HMS Ads Kitの紹介：[https://developer.huawei.com/consumer/jp/hms/huawei-adskit/](https://developer.huawei.com/consumer/jp/hms/huawei-adskit/)
* HMS Ads Kitのドキュメント：[https://developer.huawei.com/consumer/jp/doc/development/HMSCore-Guides/publisher-service-introduction-0000001070671805](https://developer.huawei.com/consumer/jp/doc/development/HMSCore-Guides/publisher-service-introduction-0000001070671805)
* HUAWEI Ads Publisher Service：[https://developer.huawei.com/consumer/jp/monetize/](https://developer.huawei.com/consumer/jp/monetize/)
* HUAWEI Ads Publisher Serviceのドキュメント：[https://developer.huawei.com/consumer/jp/doc/distribution/monetize/advantage-0000001051201913](https://developer.huawei.com/consumer/jp/doc/distribution/monetize/advantage-0000001051201913)
* Huawei Developers：[https://forums.developer.huawei.com/forumPortal/en/home](https://forums.developer.huawei.com/forumPortal/en/home)
* Facebook Huawei Developersグループ：[https://www.facebook.com/Huaweidevs/](https://www.facebook.com/Huaweidevs/)