Skip to content

Commit 839fe6f

Browse files
malandr2copybara-github
authored andcommitted
Added context for how to use the AppOpenAdManager
PiperOrigin-RevId: 788575189
1 parent 891cf9d commit 839fe6f

File tree

12 files changed

+383
-102
lines changed

12 files changed

+383
-102
lines changed

java/admanager/AppOpenExample/app/src/main/java/com/google/android/gms/example/appopendemo/GoogleMobileAdsConsentManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void gatherConsent(
5959
ConsentDebugSettings debugSettings =
6060
new ConsentDebugSettings.Builder(activity)
6161
// .setDebugGeography(ConsentDebugSettings.DebugGeography.DEBUG_GEOGRAPHY_EEA)
62-
.addTestDeviceHashedId(MyApplication.TEST_DEVICE_HASHED_ID)
62+
.addTestDeviceHashedId(SplashActivity.TEST_DEVICE_HASHED_ID)
6363
.build();
6464

6565
ConsentRequestParameters params =

java/admanager/AppOpenExample/app/src/main/java/com/google/android/gms/example/appopendemo/MyApplication.java

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,12 @@
3636
import java.util.Date;
3737

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

42-
// Check your logcat output for the test device hashed ID e.g.
43-
// "Use RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("ABCDEF012345"))
44-
// to get test ads on this device" or
45-
// "Use new ConsentDebugSettings.Builder().addTestDeviceHashedId("ABCDEF012345") to set this as
46-
// a debug device".
47-
public static final String TEST_DEVICE_HASHED_ID = "ABCDEF012345";
48-
4943
private AppOpenAdManager appOpenAdManager;
5044
private Activity currentActivity;
51-
private static final String TAG = "MyApplication";
5245

5346
@Override
5447
public void onCreate() {
@@ -59,14 +52,20 @@ public void onCreate() {
5952
appOpenAdManager = new AppOpenAdManager();
6053
}
6154

55+
// [END application_class]
56+
6257
/** LifecycleObserver method that shows the app open ad when the app moves to foreground. */
58+
// [START lifecycle_observer_events]
6359
@Override
6460
public void onStart(@NonNull LifecycleOwner owner) {
6561
DefaultLifecycleObserver.super.onStart(owner);
6662
appOpenAdManager.showAdIfAvailable(currentActivity);
6763
}
6864

65+
// [END lifecycle_observer_events]
66+
6967
/** ActivityLifecycleCallback methods. */
68+
// [START activity_lifecycle_callbacks]
7069
@Override
7170
public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {}
7271

@@ -96,6 +95,8 @@ public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bun
9695
@Override
9796
public void onActivityDestroyed(@NonNull Activity activity) {}
9897

98+
// [END activity_lifecycle_callbacks]
99+
99100
/**
100101
* Load an app open ad.
101102
*
@@ -129,6 +130,7 @@ public interface OnShowAdCompleteListener {
129130
}
130131

131132
/** Inner class that loads and shows app open ads. */
133+
// [START manager_class]
132134
private class AppOpenAdManager {
133135

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

151+
// [END manager_class]
152+
149153
/**
150154
* Load an ad.
151155
*
@@ -156,43 +160,37 @@ private void loadAd(Context context) {
156160
if (isLoadingAd || isAdAvailable()) {
157161
return;
158162
}
159-
160163
isLoadingAd = true;
161-
AdManagerAdRequest request = new AdManagerAdRequest.Builder().build();
164+
// [START load_ad]
162165
AppOpenAd.load(
163166
context,
164167
AD_UNIT_ID,
165-
request,
168+
new AdManagerAdRequest.Builder().build(),
166169
new AppOpenAdLoadCallback() {
167-
/**
168-
* Called when an app open ad has loaded.
169-
*
170-
* @param ad the loaded app open ad.
171-
*/
172170
@Override
173171
public void onAdLoaded(AppOpenAd ad) {
172+
// Called when an app open ad has loaded.
173+
Log.d(LOG_TAG, "App open ad loaded.");
174+
Toast.makeText(context, "Ad was loaded.", Toast.LENGTH_SHORT).show();
175+
174176
appOpenAd = ad;
175177
isLoadingAd = false;
176178
loadTime = (new Date()).getTime();
177-
178-
Log.d(LOG_TAG, "onAdLoaded.");
179-
Toast.makeText(context, "onAdLoaded", Toast.LENGTH_SHORT).show();
180179
}
181180

182-
/**
183-
* Called when an app open ad has failed to load.
184-
*
185-
* @param loadAdError the error.
186-
*/
187181
@Override
188182
public void onAdFailedToLoad(LoadAdError loadAdError) {
183+
// Called when an app open ad has failed to load.
184+
Log.d(LOG_TAG, "App open ad failed to load with error: " + loadAdError.getMessage());
185+
Toast.makeText(context, "Ad failed to load.", Toast.LENGTH_SHORT).show();
186+
189187
isLoadingAd = false;
190-
Log.d(LOG_TAG, "onAdFailedToLoad: " + loadAdError.getMessage());
191-
Toast.makeText(context, "onAdFailedToLoad", Toast.LENGTH_SHORT).show();
192188
}
193189
});
190+
// [END load_ad]
194191
}
195192

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

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

207+
// [END ad_expiration]
208+
211209
/**
212210
* Show the ad if one isn't already showing.
213211
*

java/admanager/AppOpenExample/app/src/main/java/com/google/android/gms/example/appopendemo/SplashActivity.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
/** Splash Activity that inflates splash activity xml. */
3434
public class SplashActivity extends AppCompatActivity {
3535

36+
// Check your logcat output for the test device hashed ID e.g.
37+
// "Use RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("ABCDEF012345"))
38+
// to get test ads on this device" or
39+
// "Use new ConsentDebugSettings.Builder().addTestDeviceHashedId("ABCDEF012345") to set this as
40+
// a debug device".
41+
public static final String TEST_DEVICE_HASHED_ID = "ABCDEF012345";
3642
private static final String LOG_TAG = "SplashActivity";
3743
private final AtomicBoolean isMobileAdsInitializeCalled = new AtomicBoolean(false);
3844
private final AtomicBoolean gatherConsentFinished = new AtomicBoolean(false);
@@ -129,7 +135,7 @@ private void initializeMobileAdsSdk() {
129135
// Set your test devices.
130136
MobileAds.setRequestConfiguration(
131137
new RequestConfiguration.Builder()
132-
.setTestDeviceIds(Arrays.asList(MyApplication.TEST_DEVICE_HASHED_ID))
138+
.setTestDeviceIds(Arrays.asList(TEST_DEVICE_HASHED_ID))
133139
.build());
134140

135141
new Thread(

java/admob/AppOpenExample/app/src/main/java/com/google/android/gms/example/appopenexample/GoogleMobileAdsConsentManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void gatherConsent(
5959
ConsentDebugSettings debugSettings =
6060
new ConsentDebugSettings.Builder(activity)
6161
// .setDebugGeography(ConsentDebugSettings.DebugGeography.DEBUG_GEOGRAPHY_EEA)
62-
.addTestDeviceHashedId(MyApplication.TEST_DEVICE_HASHED_ID)
62+
.addTestDeviceHashedId(SplashActivity.TEST_DEVICE_HASHED_ID)
6363
.build();
6464

6565
ConsentRequestParameters params =

java/admob/AppOpenExample/app/src/main/java/com/google/android/gms/example/appopenexample/MyApplication.java

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,12 @@
3636
import java.util.Date;
3737

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

42-
// Check your logcat output for the test device hashed ID e.g.
43-
// "Use RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("ABCDEF012345"))
44-
// to get test ads on this device" or
45-
// "Use new ConsentDebugSettings.Builder().addTestDeviceHashedId("ABCDEF012345") to set this as
46-
// a debug device".
47-
public static final String TEST_DEVICE_HASHED_ID = "ABCDEF012345";
48-
4943
private AppOpenAdManager appOpenAdManager;
5044
private Activity currentActivity;
51-
private static final String TAG = "MyApplication";
5245

5346
@Override
5447
public void onCreate() {
@@ -59,17 +52,23 @@ public void onCreate() {
5952
appOpenAdManager = new AppOpenAdManager();
6053
}
6154

55+
// [END application_class]
56+
6257
/**
6358
* DefaultLifecycleObserver method that shows the app open ad when the app moves to foreground.
6459
*/
60+
// [START lifecycle_observer_events]
6561
@Override
6662
public void onStart(@NonNull LifecycleOwner owner) {
6763
DefaultLifecycleObserver.super.onStart(owner);
6864
// Show the ad (if available) when the app moves to foreground.
6965
appOpenAdManager.showAdIfAvailable(currentActivity);
7066
}
7167

68+
// [END lifecycle_observer_events]
69+
7270
/** ActivityLifecycleCallback methods. */
71+
// [START activity_lifecycle_callbacks]
7372
@Override
7473
public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {}
7574

@@ -99,6 +98,8 @@ public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bun
9998
@Override
10099
public void onActivityDestroyed(@NonNull Activity activity) {}
101100

101+
// [END activity_lifecycle_callbacks]
102+
102103
/**
103104
* Load an app open ad.
104105
*
@@ -132,6 +133,7 @@ public interface OnShowAdCompleteListener {
132133
}
133134

134135
/** Inner class that loads and shows app open ads. */
136+
// [START manager_class]
135137
private class AppOpenAdManager {
136138

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

154+
// [END manager_class]
155+
152156
/**
153157
* Load an ad.
154158
*
@@ -159,43 +163,37 @@ private void loadAd(Context context) {
159163
if (isLoadingAd || isAdAvailable()) {
160164
return;
161165
}
162-
163166
isLoadingAd = true;
164-
AdRequest request = new AdRequest.Builder().build();
167+
// [START load_ad]
165168
AppOpenAd.load(
166169
context,
167170
AD_UNIT_ID,
168-
request,
171+
new AdRequest.Builder().build(),
169172
new AppOpenAdLoadCallback() {
170-
/**
171-
* Called when an app open ad has loaded.
172-
*
173-
* @param ad the loaded app open ad.
174-
*/
175173
@Override
176174
public void onAdLoaded(AppOpenAd ad) {
175+
// Called when an app open ad has loaded.
176+
Log.d(LOG_TAG, "App open ad loaded.");
177+
Toast.makeText(context, "Ad was loaded.", Toast.LENGTH_SHORT).show();
178+
177179
appOpenAd = ad;
178180
isLoadingAd = false;
179181
loadTime = (new Date()).getTime();
180-
181-
Log.d(LOG_TAG, "onAdLoaded.");
182-
Toast.makeText(context, "onAdLoaded", Toast.LENGTH_SHORT).show();
183182
}
184183

185-
/**
186-
* Called when an app open ad has failed to load.
187-
*
188-
* @param loadAdError the error.
189-
*/
190184
@Override
191185
public void onAdFailedToLoad(LoadAdError loadAdError) {
186+
// Called when an app open ad has failed to load.
187+
Log.d(LOG_TAG, "App open ad failed to load with error: " + loadAdError.getMessage());
188+
Toast.makeText(context, "Ad failed to load.", Toast.LENGTH_SHORT).show();
189+
192190
isLoadingAd = false;
193-
Log.d(LOG_TAG, "onAdFailedToLoad: " + loadAdError.getMessage());
194-
Toast.makeText(context, "onAdFailedToLoad", Toast.LENGTH_SHORT).show();
195191
}
196192
});
193+
// [END load_ad]
197194
}
198195

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

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

210+
// [END ad_expiration]
211+
214212
/**
215213
* Show the ad if one isn't already showing.
216214
*

java/admob/AppOpenExample/app/src/main/java/com/google/android/gms/example/appopenexample/SplashActivity.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
/** Splash Activity that inflates splash activity xml. */
3434
public class SplashActivity extends AppCompatActivity {
3535

36+
// Check your logcat output for the test device hashed ID e.g.
37+
// "Use RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("ABCDEF012345"))
38+
// to get test ads on this device" or
39+
// "Use new ConsentDebugSettings.Builder().addTestDeviceHashedId("ABCDEF012345") to set this as
40+
// a debug device".
41+
public static final String TEST_DEVICE_HASHED_ID = "ABCDEF012345";
3642
private static final String LOG_TAG = "SplashActivity";
3743
private final AtomicBoolean isMobileAdsInitializeCalled = new AtomicBoolean(false);
3844
private final AtomicBoolean gatherConsentFinished = new AtomicBoolean(false);
@@ -129,7 +135,7 @@ private void initializeMobileAdsSdk() {
129135
// Set your test devices.
130136
MobileAds.setRequestConfiguration(
131137
new RequestConfiguration.Builder()
132-
.setTestDeviceIds(Arrays.asList(MyApplication.TEST_DEVICE_HASHED_ID))
138+
.setTestDeviceIds(Arrays.asList(TEST_DEVICE_HASHED_ID))
133139
.build());
134140

135141
new Thread(

java/advanced/APIDemo/app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ dependencies {
2828
implementation 'androidx.appcompat:appcompat:1.7.0'
2929
implementation 'com.google.android.gms:play-services-ads:24.5.0'
3030
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
31+
implementation 'androidx.lifecycle:lifecycle-process:2.9.2'
3132
}

0 commit comments

Comments
 (0)