我遵循了官方的Android文档来实现UMP。
这是我的代码:
public class MainActivity extends AppCompatActivity {
private ConsentInformation consentInformation;
private ConsentForm consentForm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
consent();
}
public void consent() {
ConsentRequestParameters params = new ConsentRequestParameters
.Builder()
.setTagForUnderAgeOfConsent(false)
.build();
consentInformation = UserMessagingPlatform.getConsentInformation(this);
consentInformation.requestConsentInfoUpdate(this,
params,
new ConsentInformation.OnConsentInfoUpdateSuccessListener() {
@Override
public void onConsentInfoUpdateSuccess() {
// The consent information state was updated.
// You are now ready to check if a form is available.
if (consentInformation.isConsentFormAvailable()) {
loadForm();
}
}
},
new ConsentInformation.OnConsentInfoUpdateFailureListener() {
@Override
public void onConsentInfoUpdateFailure(FormError formError) {
// Handle the error.
}
});
}
public void loadForm() {
// Loads a consent form. Must be called on the main thread.
UserMessagingPlatform.loadConsentForm(
this,
new UserMessagingPlatform.OnConsentFormLoadSuccessListener() {
@Override
public void onConsentFormLoadSuccess(ConsentForm consentForm) {
MainActivity.this.consentForm = consentForm;
if (consentInformation.getConsentStatus() == ConsentInformation.ConsentStatus.REQUIRED) {
consentForm.show(
MainActivity.this,
new ConsentForm.OnConsentFormDismissedListener() {
@Override
public void onConsentFormDismissed(FormError formError) {
if (consentInformation.getConsentStatus() == ConsentInformation.ConsentStatus.OBTAINED) {
// App can start requesting ads.
//what should be here?
}
// Handle dismissal by reloading form.
loadForm();
}
});
}
}
},
new UserMessagingPlatform.OnConsentFormLoadFailureListener() {
@Override
public void onConsentFormLoadFailure(FormError formError) {
loadForm();
}
}
);
}
好吧,如果我正确地实现了它,我的问题是,我应该在下面的if语句中放入什么:
if (consentInformation.getConsentStatus() == ConsentInformation.ConsentStatus.OBTAINED) {
// App can start requesting ads.
//what should be here?
}
也许正在初始化移动广告?:
MobileAds.initialize(this, initializationStatus -> {
});
我不知道什么是最好的选择。因为如果在用户接受或拒绝表单之前加载了广告,那么尝试在没有初始化移动广告的情况下加载广告将不是一个好的做法。我感谢所有可能的帮助:)