Consent SDK允许显示同意书,但目前只有英文版(SDK的1.0.3版)。 SDK页面说:
要更新Google提供的同意书的同意文本,请根据需要修改Consent SDK中包含的consentform.html文件。
但是,consentform.html是作为资产提供的,我没有看到本地化它的方法,特别是使用gradle。在这种情况下处理本地化的最佳方法是什么?为什么这不是首先要做的?欧洲不仅仅是英语。
答案 0 :(得分:14)
由于Google的“欧盟同意”对话框无法进行本地化,因此我创建了自己的同意对话框,您可以像往常一样使用strings.xml
进行翻译。它基于谷歌的做法。这将在没有调解的情况下使用:
您可以自由使用我的代码,但如果文本适合您,请咨询您的法律顾问。我无法就适合您的同意文本提供法律建议。
添加到您的gradle文件:
implementation 'com.google.android.ads.consent:consent-library:1.0.3'
添加成员变量:
public boolean mShowNonPersonalizedAdRequests = false;
private AlertDialog mEuDialog;
在onCreate()
致电checkConsentStatus()
:
@Override
protected void onCreate(Bundle savedInstanceState) {
// ...
checkConsentStatus();
// ...
}
添加使用Google的Consent SDK的checkConsentStatus()
方法:
// https://developers.google.com/admob/android/eu-consent
private void checkConsentStatus(){
ConsentInformation consentInformation = ConsentInformation.getInstance(this);
ConsentInformation.getInstance(this).addTestDevice("YOUR-DEVICE-ID"); // enter your device id, if you need it for testing
String[] publisherIds = {"pub-YOUR-ADMOB-PUB-ID"}; // enter your admob pub-id
consentInformation.requestConsentInfoUpdate(publisherIds, new ConsentInfoUpdateListener() {
@Override
public void onConsentInfoUpdated(ConsentStatus consentStatus) {
log("User's consent status successfully updated: " +consentStatus);
if (ConsentInformation.getInstance(MainActivity.this).isRequestLocationInEeaOrUnknown()){
log("User is from EU");
/////////////////////////////
// TESTING - reset the choice
//ConsentInformation.getInstance(MainActivity.this).setConsentStatus(ConsentStatus.UNKNOWN);
/////////////////////////////
// If the returned ConsentStatus is UNKNOWN, collect user's consent.
if (consentStatus == ConsentStatus.UNKNOWN) {
showMyConsentDialog(false);
}
// If the returned ConsentStatus is PERSONALIZED or NON_PERSONALIZED
// the user has already provided consent. Forward consent to the Google Mobile Ads SDK.
else if (consentStatus == ConsentStatus.NON_PERSONALIZED) {
mShowNonPersonalizedAdRequests = true;
// The default behavior of the Google Mobile Ads SDK is to serve personalized ads.
// If a user has consented to receive only non-personalized ads, you can configure
// an AdRequest object with the following code to specify that only non-personalized
// ads should be returned.
}
} else {
log("User is NOT from EU");
// we don't have to do anything
}
}
@Override
public void onFailedToUpdateConsentInfo(String errorDescription) {
log("User's consent status failed to update: " +errorDescription);
}
});
}
添加showMyConsentDialog()
方法:
public void showMyConsentDialog(boolean showCancel) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this, R.style.MyAlertDialogStyle);
LayoutInflater inflater = getLayoutInflater();
View eu_consent_dialog = inflater.inflate(R.layout.eu_consent, null);
alertDialog.setView(eu_consent_dialog)
.setCancelable(false);
if (showCancel) alertDialog.setPositiveButton(R.string.dialog_close, null);
mEuDialog = alertDialog.create();
mEuDialog.show();
Button btn_eu_consent_yes = eu_consent_dialog.findViewById(R.id.btn_eu_consent_yes);
Button btn_eu_consent_no = eu_consent_dialog.findViewById(R.id.btn_eu_consent_no);
Button btn_eu_consent_remove_ads = eu_consent_dialog.findViewById(R.id.btn_eu_consent_remove_ads);
btn_eu_consent_yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mEuDialog.cancel();
toast(getString(R.string.thank_you), MainActivity.this);
ConsentInformation.getInstance(MainActivity.this).setConsentStatus(ConsentStatus.PERSONALIZED);
mShowNonPersonalizedAdRequests = false;
}
});
btn_eu_consent_no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mEuDialog.cancel();
toast(getString(R.string.thank_you), MainActivity.this);
ConsentInformation.getInstance(MainActivity.this).setConsentStatus(ConsentStatus.NON_PERSONALIZED);
mShowNonPersonalizedAdRequests = true;
}
});
btn_eu_consent_remove_ads.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mEuDialog.cancel();
IAP_buyAdsFree(); // YOUR REMOVE ADS METHOD
}
});
TextView tv_eu_learn_more = eu_consent_dialog.findViewById(R.id.tv_eu_learn_more);
tv_eu_learn_more.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
euMoreInfoDialog();
}
});
}
这是同意布局,保存到eu_consent.xml
:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:id="@+id/ll_eu_consent"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/activity_horizontal_margin"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/eu_consent_text"
android:textSize="14sp"
android:paddingBottom="6dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/eu_consent_question"
android:textSize="14sp"
android:paddingBottom="6dp"
android:textStyle="bold"
/>
<Button
android:id="@+id/btn_eu_consent_yes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/eu_consent_yes"
android:textSize="13sp"
/>
<Button
android:id="@+id/btn_eu_consent_no"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/eu_consent_no"
android:textSize="13sp"
android:layout_marginTop="6dp"
android:layout_marginBottom="6dp"
/>
<Button
android:id="@+id/btn_eu_consent_remove_ads"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/action_remove_ads"
android:textSize="13sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/eu_consent_change_setting"
android:textSize="14sp"
android:paddingTop="6dp"
android:paddingBottom="6dp"
/>
<TextView
android:id="@+id/tv_eu_learn_more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/learn_more"
android:textSize="14sp"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:paddingTop="6dp"
android:paddingBottom="6dp"
android:textColor="@color/blue"
style="@style/SelectableItem"
/>
</LinearLayout>
</ScrollView>
添加euMoreInfoDialog()
:
private void euMoreInfoDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this, R.style.MyAlertDialogStyle);
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(40, 20, 40, 20);
TextView tv_my_privacy_policy = new TextView(this);
String link = "<a href="+PRIVACY_URL+">"+getResources().getString(R.string.app_name)+"</a>";
tv_my_privacy_policy.setText(Html.fromHtml(link));
tv_my_privacy_policy.setMovementMethod(LinkMovementMethod.getInstance());
ll.addView(tv_my_privacy_policy, params);
TextView tv_google_partners = new TextView(this);
tv_google_partners.setText(R.string.google_partners);
tv_google_partners.setPadding(40,40,40,20);
ll.addView(tv_google_partners);
List<AdProvider> adProviders = ConsentInformation.getInstance(this).getAdProviders();
for (AdProvider adProvider : adProviders) {
//log("adProvider: " +adProvider.getName()+ " " +adProvider.getPrivacyPolicyUrlString());
link = "<a href="+adProvider.getPrivacyPolicyUrlString()+">"+adProvider.getName()+"</a>";
TextView tv_adprovider = new TextView(this);
tv_adprovider.setText(Html.fromHtml(link));
tv_adprovider.setMovementMethod(LinkMovementMethod.getInstance());
ll.addView(tv_adprovider, params);
}
sv.addView(ll);
builder.setTitle(R.string.privacy_policy)
.setView(sv)
.setPositiveButton(R.string.dialog_close, null);
final AlertDialog createDialog = builder.create();
createDialog.show();
}
在AdMob网络界面中,选择您要使用的广告技术提供商。我建议你不要选择超过20个(左右),因为如果你选择了太多的提供者,我认为euMoreInfoDialog()
会变得很慢。
添加到onDestroy()
以防止屏幕旋转错误:
@Override
public void onDestroy(){
// ...
if (mEuDialog != null && mEuDialog.isShowing()) mEuDialog.cancel();
// ...
super.onDestroy();
}
当您发出广告请求时,请检查mShowNonPersonalizedAdRequests
的值,并在必要时将"npa"
添加到请求中:
Bundle extras = new Bundle();
if (mShowNonPersonalizedAdRequests)
extras.putString("npa", "1");
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("YOUR-DEVICE-ID-GOES-HERE") // insert your device id
.addNetworkExtrasBundle(AdMobAdapter.class, extras)
.build();
最后,将所有语言的字符串添加到strings.xml
:
<!-- EU GDPR Consent texts -->
<string name="eu_consent_text">Dear user!\n\nWe use Google Admob to show ads. Ads support our work, and enable further development of this app. In line with the new European Data Protection Regulation (GDPR), we need your consent to serve ads tailored for you.</string>
<string name="eu_consent_question">Can your data be used to show ads tailored for you?</string>
<string name="learn_more">Learn how your data is used</string>
<string name="google_partners">Google and its partners:</string>
<string name="eu_consent_yes">Yes, continue to show relevant ads</string>
<string name="eu_consent_no">No, show ads that are irrelevant</string>
<string name="eu_consent_change_setting">You can change this setting anytime in the \"About\" window.</string>
<string name="thank_you">Thank you!</string>
就是这样!
(注意:log()
和toast()
是我的方法,请用您自己的方法替换。PRIVACY_URL
是您隐私政策的String
网址。)
答案 1 :(得分:2)
在Android Studio中选择Project files视图,然后转到External Libraries&gt;然后查找同意库,然后在classes.dex上单击鼠标右键,选择“在资源管理器中显示”。接下来转到上层文件夹并搜索资产文件夹和consetform.html,该库有两个文件夹 - 也许,用于调试和发布版本?但我发现它有效。
答案 2 :(得分:0)
我带了ConsentFormClasses.jar并创建了自己的MyConsentFormClasses.jar。在此jar文件中,我使用了类'ConsentForm.class',并添加了一个新方法load(String locale)
。
与原始方法load()
的不同之处在于webiew的调用。
this.webView.loadUrl("file:///android_asset/consentform.html")
更改为
this.webView.loadUrl("file:///android_asset/consentform_"+locale+".html")
我将字符串变量locale
定义为字符串资源。
在Assests文件夹中,我放入了以下文件,例如同意书_zh.html,同意书_fr.html,同意书_pl.html。
构建新的ConsentForm.Class并创建MyConsentFormClasses.jar,将jar复制到我项目的libs
文件夹中,并配置AndroidStudio依赖项...
对我来说,必须更改源代码的缺点是可以承受的。我希望Google尽快在这里提供适当的解决方案
GGK