Rewarded Ads: Publishers Get Started

Prerequisites

Initialize rewarded video ad

public class MainActivity extends ActionBarActivity {
    private RewardedVideoAd mAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Use an activity context to get the rewarded video instance.
        mAd = MobileAds.getRewardedVideoAdInstance(this);
        mAd.setRewardedVideoAdListener(this);
    }

    ...
}

A RewardedVideoAd object can be retrieved using MobileAds.getRewardedVideoAdInstance.

To be notified of rewarded video lifecycle events, you must implement RewardedVideoAdListener. The rewarded ad listener is set using the setRewardedVideoAdListener() method. This listener is automatically notified of events from all the networks you're mediating. For example, you are notified of a user being rewarded for watching a video through the onRewarded() method on RewardedVideoAdListener.

Request rewarded video

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Use an activity context to get the rewarded video instance.
    mAd = MobileAds.getRewardedVideoAdInstance(this);
    mAd.setRewardedVideoAdListener(this);

    loadRewardedVideoAd();
}

private void loadRewardedVideoAd() {
    mAd.loadAd("ca-app-pub-3940256099942544/5224354917", new AdRequest.Builder().build());
}

Adhering to the singleton design of RewardedVideoAd, requests to load an ad should be made to the shared instance.

It is highly recommended to call loadAd() as early as possible (for example, in the onCreate() method of your Activity) to allow videos to be preloaded.

Set up event notification

The RewardedVideoAdListener notifies you of rewarded video lifecycle events. The most important event in this delegate is onRewarded(), which is called when the user should be rewarded for watching a video. You are required to set the listener prior to loading an ad, and you also must implement onRewarded(). You may optionally implement other methods from this listener.

@Override
public void onRewarded(RewardItem reward) {
    Toast.makeText(this, "onRewarded! currency: " + reward.getType() + "  amount: " +
            reward.getAmount(), Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoAdLeftApplication() {
    Toast.makeText(this, "onRewardedVideoAdLeftApplication",
            Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoAdClosed() {
    Toast.makeText(this, "onRewardedVideoAdClosed", Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoAdFailedToLoad(int errorCode) {
    Toast.makeText(this, "onRewardedVideoAdFailedToLoad", Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoAdLoaded() {
    Toast.makeText(this, "onRewardedVideoAdLoaded", Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoAdOpened() {
    Toast.makeText(this, "onRewardedVideoAdOpened", Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoStarted() {
    Toast.makeText(this, "onRewardedVideoStarted", Toast.LENGTH_SHORT).show();
}

Display rewarded video

if (mAd.isLoaded()) {
    mAd.show();
}

It is best practice to ensure a rewarded video ad has completed loading before attempting to display it. The isLoaded() method indicates if the rewarded video ad request has been successfully fulfilled.

Forward lifecycle events

To forward the parent Activity's lifecycle events to the RewardedVideoAd object, call the resume(), pause(), and destroy() methods in the parent Activity's onResume(), onPause(), and onDestroy() methods respectively.

An implementation of Activity lifecycle event forwarding looks like this:

@Override
public void onResume() {
    mAd.resume(this);
    super.onResume();
}

@Override
public void onPause() {
    mAd.pause(this);
    super.onPause();
}

@Override
public void onDestroy() {
    mAd.destroy(this);
    super.onDestroy();
}

Download the example

You can also download the rewarded video example from GitHub to see rewarded video ads in action.

Download example from GitHub

Integration with Unity

For instructions on integrating rewarded video into a Unity project, see Rewarded Video.

Send feedback about...

AdMob by Google
AdMob by Google