求Android应用中请求AdMob个性化广告用户授权的示例代码
没问题!这里有一个完整的Android代码示例,用来请求用户授权AdMob个性化广告,完全遵循Google的隐私规范和UMP SDK的最佳实践:
Android AdMob个性化广告授权请求实现
首先,确保你已经在项目中集成了UMP(User Messaging Platform)SDK,它是Google官方推荐用于处理广告授权的工具。在你的app/build.gradle(Module级别)中添加依赖:
dependencies { // UMP SDK 用于广告授权请求 implementation 'com.google.android.ump:user-messaging-platform:2.2.0' }
接下来,我们可以通过两种方式实现授权请求:使用UMP默认弹窗,或者自定义授权界面。下面是两种方案的代码:
方案1:使用UMP默认授权弹窗(推荐)
这种方式无需自定义布局,直接使用Google提供的标准化授权界面,符合地区隐私法规要求。
import android.os.Bundle import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import com.google.android.ump.ConsentForm import com.google.android.ump.ConsentInformation import com.google.android.ump.ConsentRequestParameters import com.google.android.ump.UserMessagingPlatform class MainActivity : AppCompatActivity() { private lateinit var consentInformation: ConsentInformation private var consentForm: ConsentForm? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // 初始化授权请求参数 val consentParams = ConsentRequestParameters.Builder() .setTagForUnderAgeOfConsent(false) // 若应用面向13岁以下未成年人,请设为true // 开发阶段可添加测试设备ID,测试不同授权场景 // .setTestDeviceIds(listOf("YOUR_TEST_DEVICE_ID")) .build() // 获取授权信息实例 consentInformation = UserMessagingPlatform.getConsentInformation(this) // 请求更新授权状态 consentInformation.requestConsentInfoUpdate( this, consentParams, { // 授权信息更新成功,判断是否需要展示授权表单 if (consentInformation.isConsentFormAvailable) { loadAndShowConsentForm() } else { // 无需展示表单,直接处理当前授权状态 handleConsentResult() } }, { error -> // 处理请求失败的情况,比如网络问题 Toast.makeText(this, "授权请求失败: ${error.message}", Toast.LENGTH_LONG).show() handleConsentResult() } ) } // 加载并展示UMP默认授权表单 private fun loadAndShowConsentForm() { UserMessagingPlatform.loadConsentForm( this, { form -> consentForm = form // 仅当用户尚未授权时展示表单 if (consentInformation.consentStatus == ConsentInformation.ConsentStatus.REQUIRED) { form.show(this) { formError -> // 表单提交后处理结果 handleConsentResult() formError?.let { Toast.makeText(this, "表单加载异常: ${it.message}", Toast.LENGTH_SHORT).show() } } } }, { loadError -> Toast.makeText(this, "加载授权表单失败: ${loadError.message}", Toast.LENGTH_LONG).show() handleConsentResult() } ) } // 根据授权状态处理后续逻辑 private fun handleConsentResult() { when (consentInformation.consentStatus) { ConsentInformation.ConsentStatus.PERSONALIZED -> { // 用户允许个性化广告,可正常加载AdMob个性化广告 Toast.makeText(this, "已授权个性化广告", Toast.LENGTH_SHORT).show() // 这里添加进入主页面的逻辑 } ConsentInformation.ConsentStatus.NON_PERSONALIZED -> { // 用户选择非个性化广告,加载非个性化广告 Toast.makeText(this, "已选择非个性化广告", Toast.LENGTH_SHORT).show() // 这里添加进入主页面的逻辑 } ConsentInformation.ConsentStatus.REQUIRED -> { // 用户尚未授权,重新加载表单 loadAndShowConsentForm() } else -> { // 未知状态,默认使用非个性化广告 Toast.makeText(this, "授权状态未知,将展示非个性化广告", Toast.LENGTH_SHORT).show() } } } override fun onDestroy() { super.onDestroy() // 销毁表单实例,避免内存泄漏 consentForm?.destroy() } }
方案2:自定义授权界面
如果你需要完全自定义授权提示的UI,可以使用以下方式:
1. 自定义布局文件(activity_consent_custom.xml)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="24dp" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="个性化广告授权" android:textSize="22sp" android:textStyle="bold"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="为了给您提供更贴合兴趣的广告内容,我们需要获取您的授权。您可以随时在应用设置中修改此选项。" android:textSize="16sp" android:layout_marginTop="16dp" android:gravity="center"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="32dp" android:gravity="center"> <Button android:id="@+id/btn_allow_personalized" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="允许个性化广告"/> <Button android:id="@+id/btn_deny_personalized" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="仅展示非个性化广告" android:layout_marginStart="16dp"/> </LinearLayout> </LinearLayout>
2. Activity逻辑实现
import android.os.Bundle import android.widget.Button import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import com.google.android.ump.ConsentInformation import com.google.android.ump.ConsentRequestParameters import com.google.android.ump.UserMessagingPlatform class CustomConsentActivity : AppCompatActivity() { private lateinit var consentInformation: ConsentInformation override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_consent_custom) // 初始化授权参数 val consentParams = ConsentRequestParameters.Builder() .setTagForUnderAgeOfConsent(false) .build() consentInformation = UserMessagingPlatform.getConsentInformation(this) // 先检查是否已有授权记录 consentInformation.requestConsentInfoUpdate( this, consentParams, { if (consentInformation.consentStatus != ConsentInformation.ConsentStatus.REQUIRED) { // 用户已授权,直接进入主页面 handleConsentResult() finish() } }, { error -> Toast.makeText(this, "授权状态检查失败: ${error.message}", Toast.LENGTH_LONG).show() } ) // 绑定按钮点击事件 findViewById<Button>(R.id.btn_allow_personalized).setOnClickListener { consentInformation.setConsentStatus(ConsentInformation.ConsentStatus.PERSONALIZED) Toast.makeText(this, "已授权个性化广告", Toast.LENGTH_SHORT).show() finish() // 跳转主页面逻辑 } findViewById<Button>(R.id.btn_deny_personalized).setOnClickListener { consentInformation.setConsentStatus(ConsentInformation.ConsentStatus.NON_PERSONALIZED) Toast.makeText(this, "已选择非个性化广告", Toast.LENGTH_SHORT).show() finish() // 跳转主页面逻辑 } } private fun handleConsentResult() { when (consentInformation.consentStatus) { ConsentInformation.ConsentStatus.PERSONALIZED -> { Toast.makeText(this, "当前为个性化广告模式", Toast.LENGTH_SHORT).show() } ConsentInformation.ConsentStatus.NON_PERSONALIZED -> { Toast.makeText(this, "当前为非个性化广告模式", Toast.LENGTH_SHORT).show() } } } }
关键注意事项
- 未成年人适配: 如果应用面向13岁以下用户,务必将
setTagForUnderAgeOfConsent设为true,UMP会自动遵循COPPA规范,跳过个性化广告授权请求。 - 状态持久化: UMP会自动保存用户的授权选择,下次启动应用时无需重复请求,直接读取状态即可。
- 测试模式: 开发阶段可通过
setTestDeviceIds添加测试设备ID,模拟不同地区的授权场景。
内容的提问来源于stack exchange,提问作者Digiform South Africa
相关产品推荐
相关产品推荐

