Google Ads 用户同意SDK for Android 使用教程

随笔6天前发布 林晓鹏
16 0 0

Google Ads 用户同意SDK for Android 使用教程

googleads-consent-sdk-androidConsent SDK项目地址:https://gitcode.com/gh_mirrors/go/googleads-consent-sdk-android

项目介绍

Google Ads 用户同意SDK for Android 是一个帮助开发者遵守Google EU用户同意政策的工具。该SDK允许开发者在应用中收集用户同意信息,确保在展示广告时符合欧盟的法律规定。目前,该项目已不再维护,推荐使用User Messaging Platform (UMP) SDK。

项目快速启动

1. 添加依赖

在项目的 build.gradle 文件中添加以下依赖:




dependencies {


    implementation 'com.google.android.ump:user-messaging-platform:3.0.0'


}

2. 配置AndroidManifest.xml

AndroidManifest.xml 中添加应用ID:




<manifest>


    <application>


        <meta-data


            android:name="com.google.android.gms.ads.APPLICATION_ID"


            android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>


    </application>


</manifest>

3. 请求用户同意信息

在应用启动时请求用户同意信息:




import com.google.android.ump.ConsentInformation


import com.google.android.ump.UserMessagingPlatform


 


class MainActivity : AppCompatActivity() {


    private lateinit var consentInformation: ConsentInformation


 


    override fun onCreate(savedInstanceState: Bundle?) {


        super.onCreate(savedInstanceState)


        setContentView(R.layout.activity_main)


 


        consentInformation = UserMessagingPlatform.getConsentInformation(this)


 


        consentInformation.requestConsentInfoUpdate(


            this,


            ConsentRequestParameters.Builder().build(),


            {


                if (consentInformation.canRequestAds()) {


                    initializeMobileAdsSdk()


                }


            },


            { requestConsentError ->


                Log.w(TAG, String.format("%s: %s", requestConsentError.errorCode, requestConsentError.message))


            })


    }


 


    private fun initializeMobileAdsSdk() {


        if (isMobileAdsInitializeCalled.getAndSet(true)) {


            return


        }


        val backgroundScope = CoroutineScope(Dispatchers.IO)


        backgroundScope.launch {


            MobileAds.initialize(this@MainActivity) {}


            runOnUiThread {


                // TODO: Request an ad


                // loadInterstitialAd()


            }


        }


    }


}

应用案例和最佳实践

应用案例

假设你正在开发一个新闻阅读应用,需要展示广告以获取收入。使用Google Ads 用户同意SDK,你可以在应用启动时收集用户同意信息,确保广告展示符合法律规定。

最佳实践

及时更新SDK:由于该项目已不再维护,建议使用User Messaging Platform (UMP) SDK,并及时更新到最新版本。透明度:确保在应用中明确告知用户收集同意信息的目的,并提供隐私政策链接。测试:在发布应用前,确保在不同设备上测试用户同意流程,确保流程顺畅。

典型生态项目

User Messaging Platform (UMP) SDK

UMP SDK 是Google Ads 用户同意SDK的替代品,提供了更强大的功能和更好的维护支持。推荐所有开发者迁移到UMP SDK。

Google Mobile Ads SDK

Google Mobile Ads SDK 是展示广告的核心SDK,与用户同意SDK配合使用,确保广告展示符合法律规定。

通过以上步骤,你可以快速集成Google Ads 用户同意SDK到你的Android应用中,并确保遵守相关法律规定。

googleads-consent-sdk-androidConsent SDK项目地址:https://gitcode.com/gh_mirrors/go/googleads-consent-sdk-android

© 版权声明

相关文章

暂无评论

您必须登录才能参与评论!
立即登录
暂无评论...