Global Settings

The MobileAds class provides global settings for the Google Mobile Ads SDK.

Video ad volume control

If your app has its own volume controls (such as custom music or sound effect volumes), disclosing app volume to the Google Mobile Ads SDK allows video ads to respect app volume settings. This ensures users receive video ads with the expected audio volume.

The device volume, controlled through volume buttons or OS-level volume slider, determines the volume for device audio output. However, apps can independently adjust volume levels relative to the device volume to tailor the audio experience. You can report the relative app volume to the Mobile Ads SDK through the static setAppVolume() method. Valid ad volume values range from 0.0 (silent) to 1.0 (current device volume). Here's an example of how to report the relative app volume to the SDK:

Java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    
    MobileAds.initialize(this, new OnInitializationCompleteListener() {
        @Override
        public void onInitializationComplete(InitializationStatus initializationStatus) {}
    });
    
    // Set app volume to be half of current device volume.
    MobileAds.setAppVolume(0.5f);
    ...
}

Kotlin

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    
    MobileAds.initialize(this) {}
    
    // Set app volume to be half of current device volume.
    MobileAds.setAppVolume(0.5f)
    ...
}

To inform the SDK that the app volume has been muted, use the setAppMuted() method:

Java

MobileAds.setAppMuted(true);

Kotlin

MobileAds.setAppMuted(true)

By default, the app volume is set to 1 (the current device volume), and the app is not muted.

If your app has special requirements, you can set the optional SharedPreferences gad_has_consent_for_cookies. The Mobile Ads SDK will enable limited ads (LTD) when the gad_has_consent_for_cookies preference is set to zero.

Java

Context activity = getActivity();
SharedPreferences sharedPreferences =
  PreferenceManager.getDefaultSharedPreferences(activity);
sharedPreferences.edit().putInt("gad_has_consent_for_cookies", 0).apply();

Kotlin

val sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context)
sharedPrefs.edit().putInt("gad_has_consent_for_cookies", 0).apply()

Same app key

Google Mobile Ads SDK version 21.4.0 introduces the same app key to help you deliver more relevant and personalized ads by using data collected from your apps. The same app key is enabled by default, but you can disable it using the following API.

Java

// Disables the same app key.
MobileAds.getInstance().enableSameAppKey(false);

Kotlin

// Disables the same app key.
MobileAds.getInstance().enableSameAppKey(false)