1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

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

Last updated at Posted at 2021-02-25

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

スプラッシュ広告はアプリの起動時の表示される広告です。
image.png

Ad slot ID

Huawei Adsの実装方法-準備編Ad slot IDをご参照ください。
オフィシャルのテスト用Ad slot IDは
testq6zq98hecj
です。

実装の要点

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

スプラッシュ広告のActivityのレイアウト

必要なのはcom.huawei.hms.ads.splash.SplashViewだけです。

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シリーズ

GitHub

HMS Ads Kit Demo : https://github.com/Rei2020GitHub/MyPublicProject/tree/master/AdsDemo

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?