前往主控台

RewardedAd

public final class RewardedAd extends Object

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);
         rewardedAd = new RewardedAd(this, "adUnitId");
         ServerSideVerificationOptions serverSideVerificationOptions =
             new ServerSideVerificationOptions.Builder().setUserId("userId").build();
         rewardedAd.setServerSideVerificationOptions(serverSideVerificationOptions);
         AdRequest adRequest = new AdRequest.Builder().build();
         rewardedAd.loadAd(adRequest, new RewardedAdLoadCallback() {
             @Override
             public void onRewardedAdLoaded() {
              findViewById(R.id.display_button).setVisibility(View.VISIBLE);
             }
         });
     }

     public void onDisplayButtonClicked(View view) {
         if (rewardedAd.isLoaded()) {
             rewardedAd.show(this, new RewardedAdCallback() {
                 @Override
                 public void onUserEarnedReward(@NonNull RewardItem reward) {
                     Toast.makeText(
                         this,
                         "onRewarded! currency: "
                             + rewardItem.getType() + "    amount: "
                             + rewardItem.getAmount(), Toast.LENGTH_SHORT).show();
                 }
             });
         }
     }
 }

Public Constructor Summary

RewardedAd(Context context, String adUnitId)
Construct an RewardedAd.

Public Method Summary

Bundle
getAdMetadata()
Gets all available ad metadata for this RewardedAd.
String
getMediationAdapterClassName()
Returns the mediation adapter class name.
RewardItem
getRewardItem()
Gets reward item for the loaded rewarded ad.
boolean
isLoaded()
Returns true if a rewarded ad is available and is ready to be shown.
void
loadAd(PublisherAdRequest adRequest, RewardedAdLoadCallback loadCallback)
Loads a rewarded ad with the specified PublisherAdRequest.
void
loadAd(AdRequest adRequest, RewardedAdLoadCallback loadCallback)
Loads a rewarded ad with the specified AdRequest.
void
void
void
show(Activity activity, RewardedAdCallback callback)
Shows rewarded ad if available.
void
show(Activity activity, RewardedAdCallback callback, boolean immersiveModeEnabled)
Shows rewarded ad if available.

Inherited Method Summary

Public Constructors

public RewardedAd (Context context, String adUnitId)

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 onAdMetadataChanged() to listen for changes.

public String 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 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 ()

Returns true if a rewarded ad is available and is ready to be shown.

public void loadAd (PublisherAdRequest adRequest, RewardedAdLoadCallback loadCallback)

Loads a rewarded ad with the specified PublisherAdRequest.

public void loadAd (AdRequest adRequest, RewardedAdLoadCallback loadCallback)

Loads a rewarded ad with the specified AdRequest.

public void setOnAdMetadataChangedListener (OnAdMetadataChangedListener listener)

public void setServerSideVerificationOptions (ServerSideVerificationOptions options)

public void show (Activity activity, RewardedAdCallback callback)

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)

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.