This class is used to request and display a rewarded ad.
public class MainActivity extends AppCompatActivity { private RewardedAd rewardedAd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FullScreenContentCallback fullScreenContentCallback = new FullScreenContentCallback() { @Override public void onAdShowedFullScreenContent() { // Code to be invoked when the ad showed full screen content. } @Override public void onAdDismissedFullScreenContent() { rewardedAd = null; // Code to be invoked when the ad dismissed full screen content. } }; RewardedAd.load( this, "adUnitId", new AdRequest.Builder().build(), new RewardedAdLoadCallback() { @@Override public void onAdLoaded(RewardedAd ad) { findViewById(R.id.display_button).setVisibility(View.VISIBLE); rewardedAd = ad; rewardedAd.setFullScreenContentCallback(fullScreenContentCallback); } }); } public void onDisplayButtonClicked(View view) { if (rewardedAd != null) { rewardedAd.show( this, new OnUserEarnedRewardListener() { @@Override public void onUserEarnedReward(@NonNull RewardItem rewardItem) { Toast.makeText( MainActivity.this, "onRewarded! currency: " + rewardItem.getType() + " amount: " + rewardItem.getAmount(), Toast.LENGTH_SHORT).show(); } }); } } }
Public Constructor Summary
RewardedAd(Context
context, String adUnitId)
This constructor is deprecated. Use
load(Context, String, AdRequest, RewardedAdLoadCallback) .
|
Public Method Summary
Inherited Method Summary
Public Constructors
public RewardedAd (Context context, String adUnitId)
This constructor is deprecated.
Use
load(Context, String, AdRequest, RewardedAdLoadCallback)
.
Construct an RewardedAd
.
Public Methods
public Bundle getAdMetadata ()
Gets all available ad metadata for this RewardedAd
.
Ad metadata is empty if no ad has loaded or the loaded ad doesn't have metadata. Ad
metadata may update after loading. Use
OnAdMetadataChangedListener.onAdMetadataChanged()
to listen for changes.
public String getAdUnitId ()
Returns the ad unit ID.
public FullScreenContentCallback getFullScreenContentCallback ()
Gets the FullScreenContentCallback
for this RewardedAd
.
public String getMediationAdapterClassName ()
This method is deprecated.
Use
getResponseInfo()
and
ResponseInfo.getMediationAdapterClassName()
.
Returns the mediation adapter class name. In the case of a mediated ad response,
this is the name of the class that was responsible for performing the ad request and
rendering the ad. For non-mediated responses, this value will be
"com.google.ads.mediation.admob.AdMobAdapter"
.
public OnAdMetadataChangedListener getOnAdMetadataChangedListener ()
Gets the
OnAdMetadataChangedListener
for this RewardedAd
.
public OnPaidEventListener getOnPaidEventListener ()
Gets the OnPaidEventListener
for this RewardedAd
.
public ResponseInfo getResponseInfo ()
Returns the ResponseInfo
object for the loaded ad. Returns null
until the ad successfully
loads.
public RewardItem getRewardItem ()
Gets reward item for the loaded rewarded ad. Returns null
if an ad has
not yet loaded. Use isLoaded()
to check if an ad is loaded.
public boolean isLoaded ()
This method is deprecated.
When using
load(Context, String, AdRequest, RewardedAdLoadCallback)
, the ad returned in
the callback is already loaded.
Returns true if a rewarded ad is available and is ready to be shown.
public static void load (Context context, String adUnitId, AdRequest adRequest, RewardedAdLoadCallback loadCallback)
Loads a RewardedAd
.
Parameters
context | The context. |
---|---|
adUnitId | The ad unit ID. |
adRequest | An ad request with targeting information. |
loadCallback | A callback to be invoked when a rewarded ad finishes loading. |
public static void load (Context context, String adUnitId, AdManagerAdRequest adManagerAdRequest, RewardedAdLoadCallback loadCallback)
Loads a RewardedAd
.
Parameters
context | The context. |
---|---|
adUnitId | The ad unit ID. |
adManagerAdRequest | A Google Ad Manager ad request with targeting information. |
loadCallback | A callback to be invoked when a rewarded ad finishes loading. |
public void loadAd (AdRequest adRequest, RewardedAdLoadCallback loadCallback)
This method is deprecated.
Use
load(Context, String, AdRequest, RewardedAdLoadCallback)
.
Loads a rewarded ad with the specified AdRequest
.
public void loadAd (PublisherAdRequest adRequest, RewardedAdLoadCallback loadCallback)
This method is deprecated.
Use
load(Context, String, AdManagerAdRequest, RewardedAdLoadCallback)
.
Loads a rewarded ad with the specified PublisherAdRequest
.
public void setFullScreenContentCallback (FullScreenContentCallback fullScreenContentCallback)
Registers a callback to be invoked when ads show and dismiss full screen content.
public void setImmersiveMode (boolean immersiveModeEnabled)
Sets a flag that controls if this rewarded ad object will be displayed in immersive
mode. Call this method before
show(Activity, OnUserEarnedRewardListener)
. During
show(Activity, OnUserEarnedRewardListener)
, if this flag is on and immersive
mode is supported, SYSTEM_UI_FLAG_IMMERSIVE_STICKY & SYSTEM_UI_FLAG_HIDE_NAVIGATION
will be turned on for the rewarded ad.
public void setOnAdMetadataChangedListener (OnAdMetadataChangedListener listener)
Sets the
OnAdMetadataChangedListener
for this RewardedAd
.
public void setOnPaidEventListener (OnPaidEventListener listener)
Registers a callback to be invoked when this ad is estimated to have earned money.
public void setServerSideVerificationOptions (ServerSideVerificationOptions options)
Set
ServerSideVerificationOptions
for this RewardedAd
.
public void show (Activity activity, RewardedAdCallback callback)
This method is deprecated.
Use
show(Activity, OnUserEarnedRewardListener)
.
Shows rewarded ad if available. Apps should check isLoaded()
before calling this method.
Parameters
activity | The activity from which the rewarded ad should be shown. |
---|---|
callback | The callback for rewarded ad events. |
public void show (Activity activity, RewardedAdCallback callback, boolean immersiveModeEnabled)
This method is deprecated.
Use
setImmersiveMode(boolean)
and
show(Activity, OnUserEarnedRewardListener)
.
Shows rewarded ad if available. Apps should check isLoaded()
before calling this method.
Parameters
activity | The activity from which the rewarded ad should be shown. |
---|---|
callback | The callback for rewarded ad events. |
immersiveModeEnabled | The flag that controls if a rewarded ad is shown during immersive mode. If
true , SYSTEM_UI_FLAG_IMMERSIVE_STICKY and
SYSTEM_UI_FLAG_HIDE_NAVIGATION will be set when the ad is displayed. |
public void show (Activity activity, OnUserEarnedRewardListener onUserEarnedRewardListener)
Shows the rewarded ad.
Parameters
activity | The activity from which the rewarded ad should be shown. |
---|---|
onUserEarnedRewardListener | The listener to be notified when user earns a reward. |