Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void gatherConsent(
ConsentDebugSettings debugSettings =
new ConsentDebugSettings.Builder(activity)
// .setDebugGeography(ConsentDebugSettings.DebugGeography.DEBUG_GEOGRAPHY_EEA)
.addTestDeviceHashedId(MyApplication.TEST_DEVICE_HASHED_ID)
.addTestDeviceHashedId(SplashActivity.TEST_DEVICE_HASHED_ID)
.build();

ConsentRequestParameters params =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,12 @@
import java.util.Date;

/** Application class that initializes, loads and show ads when activities change states. */
// [START application_class]
public class MyApplication extends Application
implements ActivityLifecycleCallbacks, DefaultLifecycleObserver {

// Check your logcat output for the test device hashed ID e.g.
// "Use RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("ABCDEF012345"))
// to get test ads on this device" or
// "Use new ConsentDebugSettings.Builder().addTestDeviceHashedId("ABCDEF012345") to set this as
// a debug device".
public static final String TEST_DEVICE_HASHED_ID = "ABCDEF012345";

private AppOpenAdManager appOpenAdManager;
private Activity currentActivity;
private static final String TAG = "MyApplication";

@Override
public void onCreate() {
Expand All @@ -59,14 +52,20 @@ public void onCreate() {
appOpenAdManager = new AppOpenAdManager();
}

// [END application_class]

/** LifecycleObserver method that shows the app open ad when the app moves to foreground. */
// [START lifecycle_observer_events]
@Override
public void onStart(@NonNull LifecycleOwner owner) {
DefaultLifecycleObserver.super.onStart(owner);
appOpenAdManager.showAdIfAvailable(currentActivity);
}

// [END lifecycle_observer_events]

/** ActivityLifecycleCallback methods. */
// [START activity_lifecycle_callbacks]
@Override
public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {}

Expand Down Expand Up @@ -96,6 +95,8 @@ public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bun
@Override
public void onActivityDestroyed(@NonNull Activity activity) {}

// [END activity_lifecycle_callbacks]

/**
* Load an app open ad.
*
Expand Down Expand Up @@ -129,6 +130,7 @@ public interface OnShowAdCompleteListener {
}

/** Inner class that loads and shows app open ads. */
// [START manager_class]
private class AppOpenAdManager {

private static final String LOG_TAG = "AppOpenAdManager";
Expand All @@ -146,6 +148,8 @@ private class AppOpenAdManager {
/** Constructor. */
public AppOpenAdManager() {}

// [END manager_class]

/**
* Load an ad.
*
Expand All @@ -156,43 +160,37 @@ private void loadAd(Context context) {
if (isLoadingAd || isAdAvailable()) {
return;
}

isLoadingAd = true;
AdManagerAdRequest request = new AdManagerAdRequest.Builder().build();
// [START load_ad]
AppOpenAd.load(
context,
AD_UNIT_ID,
request,
new AdManagerAdRequest.Builder().build(),
new AppOpenAdLoadCallback() {
/**
* Called when an app open ad has loaded.
*
* @param ad the loaded app open ad.
*/
@Override
public void onAdLoaded(AppOpenAd ad) {
// Called when an app open ad has loaded.
Log.d(LOG_TAG, "App open ad loaded.");
Toast.makeText(context, "Ad was loaded.", Toast.LENGTH_SHORT).show();

appOpenAd = ad;
isLoadingAd = false;
loadTime = (new Date()).getTime();

Log.d(LOG_TAG, "onAdLoaded.");
Toast.makeText(context, "onAdLoaded", Toast.LENGTH_SHORT).show();
}

/**
* Called when an app open ad has failed to load.
*
* @param loadAdError the error.
*/
@Override
public void onAdFailedToLoad(LoadAdError loadAdError) {
// Called when an app open ad has failed to load.
Log.d(LOG_TAG, "App open ad failed to load with error: " + loadAdError.getMessage());
Toast.makeText(context, "Ad failed to load.", Toast.LENGTH_SHORT).show();

isLoadingAd = false;
Log.d(LOG_TAG, "onAdFailedToLoad: " + loadAdError.getMessage());
Toast.makeText(context, "onAdFailedToLoad", Toast.LENGTH_SHORT).show();
}
});
// [END load_ad]
}

// [START ad_expiration]
/** Check if ad was loaded more than n hours ago. */
private boolean wasLoadTimeLessThanNHoursAgo(long numHours) {
long dateDifference = (new Date()).getTime() - loadTime;
Expand All @@ -202,12 +200,12 @@ private boolean wasLoadTimeLessThanNHoursAgo(long numHours) {

/** Check if ad exists and can be shown. */
private boolean isAdAvailable() {
// Ad references in the app open beta will time out after four hours, but this time limit
// may change in future beta versions. For details, see:
// https://support.google.com/admob/answer/9341964?hl=en
// For time interval details, see: https://support.google.com/admob/answer/9341964
return appOpenAd != null && wasLoadTimeLessThanNHoursAgo(4);
}

// [END ad_expiration]

/**
* Show the ad if one isn't already showing.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
/** Splash Activity that inflates splash activity xml. */
public class SplashActivity extends AppCompatActivity {

// Check your logcat output for the test device hashed ID e.g.
// "Use RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("ABCDEF012345"))
// to get test ads on this device" or
// "Use new ConsentDebugSettings.Builder().addTestDeviceHashedId("ABCDEF012345") to set this as
// a debug device".
public static final String TEST_DEVICE_HASHED_ID = "ABCDEF012345";
private static final String LOG_TAG = "SplashActivity";
private final AtomicBoolean isMobileAdsInitializeCalled = new AtomicBoolean(false);
private final AtomicBoolean gatherConsentFinished = new AtomicBoolean(false);
Expand Down Expand Up @@ -129,7 +135,7 @@ private void initializeMobileAdsSdk() {
// Set your test devices.
MobileAds.setRequestConfiguration(
new RequestConfiguration.Builder()
.setTestDeviceIds(Arrays.asList(MyApplication.TEST_DEVICE_HASHED_ID))
.setTestDeviceIds(Arrays.asList(TEST_DEVICE_HASHED_ID))
.build());

new Thread(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void gatherConsent(
ConsentDebugSettings debugSettings =
new ConsentDebugSettings.Builder(activity)
// .setDebugGeography(ConsentDebugSettings.DebugGeography.DEBUG_GEOGRAPHY_EEA)
.addTestDeviceHashedId(MyApplication.TEST_DEVICE_HASHED_ID)
.addTestDeviceHashedId(SplashActivity.TEST_DEVICE_HASHED_ID)
.build();

ConsentRequestParameters params =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,12 @@
import java.util.Date;

/** Application class that initializes, loads and show ads when activities change states. */
// [START application_class]
public class MyApplication extends Application
implements ActivityLifecycleCallbacks, DefaultLifecycleObserver {

// Check your logcat output for the test device hashed ID e.g.
// "Use RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("ABCDEF012345"))
// to get test ads on this device" or
// "Use new ConsentDebugSettings.Builder().addTestDeviceHashedId("ABCDEF012345") to set this as
// a debug device".
public static final String TEST_DEVICE_HASHED_ID = "ABCDEF012345";

private AppOpenAdManager appOpenAdManager;
private Activity currentActivity;
private static final String TAG = "MyApplication";

@Override
public void onCreate() {
Expand All @@ -59,17 +52,23 @@ public void onCreate() {
appOpenAdManager = new AppOpenAdManager();
}

// [END application_class]

/**
* DefaultLifecycleObserver method that shows the app open ad when the app moves to foreground.
*/
// [START lifecycle_observer_events]
@Override
public void onStart(@NonNull LifecycleOwner owner) {
DefaultLifecycleObserver.super.onStart(owner);
// Show the ad (if available) when the app moves to foreground.
appOpenAdManager.showAdIfAvailable(currentActivity);
}

// [END lifecycle_observer_events]

/** ActivityLifecycleCallback methods. */
// [START activity_lifecycle_callbacks]
@Override
public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {}

Expand Down Expand Up @@ -99,6 +98,8 @@ public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bun
@Override
public void onActivityDestroyed(@NonNull Activity activity) {}

// [END activity_lifecycle_callbacks]

/**
* Load an app open ad.
*
Expand Down Expand Up @@ -132,6 +133,7 @@ public interface OnShowAdCompleteListener {
}

/** Inner class that loads and shows app open ads. */
// [START manager_class]
private class AppOpenAdManager {

private static final String LOG_TAG = "AppOpenAdManager";
Expand All @@ -149,6 +151,8 @@ private class AppOpenAdManager {
/** Constructor. */
public AppOpenAdManager() {}

// [END manager_class]

/**
* Load an ad.
*
Expand All @@ -159,43 +163,37 @@ private void loadAd(Context context) {
if (isLoadingAd || isAdAvailable()) {
return;
}

isLoadingAd = true;
AdRequest request = new AdRequest.Builder().build();
// [START load_ad]
AppOpenAd.load(
context,
AD_UNIT_ID,
request,
new AdRequest.Builder().build(),
new AppOpenAdLoadCallback() {
/**
* Called when an app open ad has loaded.
*
* @param ad the loaded app open ad.
*/
@Override
public void onAdLoaded(AppOpenAd ad) {
// Called when an app open ad has loaded.
Log.d(LOG_TAG, "App open ad loaded.");
Toast.makeText(context, "Ad was loaded.", Toast.LENGTH_SHORT).show();

appOpenAd = ad;
isLoadingAd = false;
loadTime = (new Date()).getTime();

Log.d(LOG_TAG, "onAdLoaded.");
Toast.makeText(context, "onAdLoaded", Toast.LENGTH_SHORT).show();
}

/**
* Called when an app open ad has failed to load.
*
* @param loadAdError the error.
*/
@Override
public void onAdFailedToLoad(LoadAdError loadAdError) {
// Called when an app open ad has failed to load.
Log.d(LOG_TAG, "App open ad failed to load with error: " + loadAdError.getMessage());
Toast.makeText(context, "Ad failed to load.", Toast.LENGTH_SHORT).show();

isLoadingAd = false;
Log.d(LOG_TAG, "onAdFailedToLoad: " + loadAdError.getMessage());
Toast.makeText(context, "onAdFailedToLoad", Toast.LENGTH_SHORT).show();
}
});
// [END load_ad]
}

// [START ad_expiration]
/** Check if ad was loaded more than n hours ago. */
private boolean wasLoadTimeLessThanNHoursAgo(long numHours) {
long dateDifference = (new Date()).getTime() - loadTime;
Expand All @@ -205,12 +203,12 @@ private boolean wasLoadTimeLessThanNHoursAgo(long numHours) {

/** Check if ad exists and can be shown. */
private boolean isAdAvailable() {
// Ad references in the app open beta will time out after four hours, but this time limit
// may change in future beta versions. For details, see:
// https://support.google.com/admob/answer/9341964?hl=en
// For time interval details, see: https://support.google.com/admob/answer/9341964
return appOpenAd != null && wasLoadTimeLessThanNHoursAgo(4);
}

// [END ad_expiration]

/**
* Show the ad if one isn't already showing.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
/** Splash Activity that inflates splash activity xml. */
public class SplashActivity extends AppCompatActivity {

// Check your logcat output for the test device hashed ID e.g.
// "Use RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("ABCDEF012345"))
// to get test ads on this device" or
// "Use new ConsentDebugSettings.Builder().addTestDeviceHashedId("ABCDEF012345") to set this as
// a debug device".
public static final String TEST_DEVICE_HASHED_ID = "ABCDEF012345";
private static final String LOG_TAG = "SplashActivity";
private final AtomicBoolean isMobileAdsInitializeCalled = new AtomicBoolean(false);
private final AtomicBoolean gatherConsentFinished = new AtomicBoolean(false);
Expand Down Expand Up @@ -129,7 +135,7 @@ private void initializeMobileAdsSdk() {
// Set your test devices.
MobileAds.setRequestConfiguration(
new RequestConfiguration.Builder()
.setTestDeviceIds(Arrays.asList(MyApplication.TEST_DEVICE_HASHED_ID))
.setTestDeviceIds(Arrays.asList(TEST_DEVICE_HASHED_ID))
.build());

new Thread(
Expand Down
1 change: 1 addition & 0 deletions java/advanced/APIDemo/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.7.0'
implementation 'com.google.android.gms:play-services-ads:24.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.lifecycle:lifecycle-process:2.9.2'
}
Loading