Rewarded ads are only available from third-party networks that you've selected for mediation.
Prerequisites
- Running Android Studio 1.0 or higher
- Developing for Android API level 9 or higher
- Google Mobile Ads SDK 9.0 or higher
Download the example
Download the example app to see mediated rewarded ads in action. Currently, the only supported media type is video. You need to include any mediation adapters, ad-network-specific configurations, and a rewarded interstitial ad unit ID for the rewarded video mediation.
Add mediation to your project
Integrate Firebase and the Mobile Ads SDK into your project.
Add network adapters and SDKs
You're now ready to download and add to your project the adapters and SDKs of all the ad networks from which you'd like to serve rewarded video ads.
In Android Studio,
include the JAR files in your project's libs
folder.
Make sure that your build.gradle
file includes the following line
so that jar files are included in the build:
compile fileTree(dir: 'libs', include: ['*.jar'])
In Eclipse,
include the JAR files in your project's libs
folder.
Right-click on your project and select Properties.
Then add all the JAR files to your Java Build Path.
You may consider integrating adapters and SDKs from networks that you're not currently using but might use in the future. If you do this, you can start serving ads from these networks simply by making a configuration change in your AdMob account without requiring a code change to your app. Weigh this against the increase in app binary size that each additional SDK adds.
Include network configurations
Add entries to your AndroidManifest.xml
file
or any other configuration changes required
by each ad network you intend to use.
Identify any extra configuration required
in the documentation for the ad networks you're mediating.
For example,
each ad network typically requires
an additional activity declaration in the AndroidManifest.xml
file,
and may also require additional permissions to serve ads.
Your app should have a superset of the permissions required
by all the ad networks you're mediating.
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); RewardedVideoAd mAd = MobileAds.getRewardedVideoAdInstance(this); mAd.setRewardedVideoAdListener(this); loadRewardedVideoAd(); } private void loadRewardedVideoAd() { mAd.loadAd(AD_UNIT_ID, 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();
}
Additional parameters
Ad networks may support extra targeting parameters or inputs
that are not covered by the targeting parameters
provided by the Google Mobile Ads SDK.
You can pass extra parameters to specific mediation networks
by using the addNetworkExtrasBundle()
method on AdRequest.Builder
.
Pass the adapter class to which you want to send parameters,
and a bundle of values for the adapter to consume.
Here's an example of how to pass an option to mute audio to an ad network adapter:
Bundle bundle = new Bundle();
bundle.putBoolean("mute_audio", true);
AdRequest adRequest = new AdRequest.Builder()
.addNetworkExtrasBundle(MyAdNetworkAdapter.class, bundle)
.build();
The MyAdNetworkAdapter
class is used as an example
in the code snippet above.
You should substitute the adapter class
corresponding to the ad network for which your bundle is intended.
If an adapter supports extra parameters, that ad network should have documentation explaining how to pass extra parameters to their network.
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();
}
Integration with Unity
For instructions on integrating rewarded video mediation into a Unity project, see Rewarded Video Mediation.