Use the Samsung Ads SDK

The Samsung Ads SDK provides the following capabilities:

  • Request an ad.
  • Listen for ad events.

Request an ad

  1. Initialize the SDK.

    Call the initialize() API by passing the context along with the client key and the access key (which you requested before integrating the SDK).

SMobileAdService.initialize(this, new AdKeyContainer.Builder()
    .setAccessKeyId(AdConst.ACCESS_KEY)
	.setClientKey(AdConst.CLIENT_KEY).build());
  1. Create the AdLoader.

    Create the InterstitialAdDispatcher by passing context, isRewardType, and the placement ID (which you requested before integrating the SDK).

For interstitial video ads:

interstitialAdDispatcher = new InterstitialAdDispatcher(this, false, AdConst.INTERSTITIAL_VIDEO_AD_PLCMNT_ID);

For rewarded video ads:

interstitialAdDispatcher = new InterstitialAdDispatcher(this, true, AdConst. REWARDED_VIDEO_AD_PLCMNT_ID);

3. Add listeners.

Refer to the Listen for ad events section.

4. Load an ad.

  • To prefetch then show an ad:
// to prefetch ad
try {
    interstitialAdDispatcher.prefetchAd(new InterstitialAd.InterstitialAdListener() {
	    @Override
		public void onAdLoaded(InterstitialAd ad) {
		    SampleAppLog.d(TAG, "prefetchAdRequest success");
		}
		
		@Override
		public void onAdFailedToLoad(int errorCode) {
		    SampleAppLog.d(TAG, "prefetchAdRequest Failed - " + errorCode);
		}
	});
} catch (AdException e) {
    SampleAppLog.e(<em>TAG</em>, "prefetchAd returned error " + e.getMessage());
}

// to load and show ad
try {
    interstitialAdDispatcher.loadAndShowAd(adListener, interstitialAdLifecycleListener);
} catch (AdException e) {
    SampleAppLog.e(TAG, "loadAndShowAd returned error " + e.getMessage());
}
  • To load and show an ad:
// to load and show ad
try {
    interstitialAdDispatcher.loadAndShowAd(adListener, interstitialAdLifecycleListener);
} catch (AdException e) {
    SampleAppLog.e(TAG, "loadAndShowAd returned error " + e.getMessage());
}

Listen for ad events

To further customize the behavior of your ad, you can receive events during the ad’s lifecycle: loading, opening, closing, error and so on. You can listen for these events through the following listeners.

  • AdListener

    Set the InterstitialAdListener to receive the video ad loading status. This is used as the first parameter of prefetchAd() or loadAndShowAd():

InterstitialAd.InterstitialAdListener adListener = new InterstitialAd.InterstitialAdListener() {
    @Override
	public void onAdLoaded(InterstitialAd ad) {
		SampleAppLog.d(TAG, "onAdLoaded");
	}
	
    @Override
	public void onAdFailedToLoad(int errorCode) {
		SampleAppLog.e(TAG, "onAdFailedToLoad " + errorCode);
	}
};

The details of the AdListener are as follows:

Name

Detail

1 onAdLoaded When an Ad has successfully finished loading.
2 onAdFailedToLoad When an Ad has failed to load.

(Include the error parameter of type ‘ErrorCode’ which describes the error that has occurred.)

  • AdLifeCycleListener

    Set the InterstitialAdLifecycleListener to get the status of an ad. This is used as the second parameter of loadAndShowAd().

private InterstitialAdLifecycleListener interstitialAdLifecycleListener = >new InterstitialAdLifecycleListener() {
    @Override
	public void onAdClosed() {
	    AdAppLog.d(TAG, "onAdClosed");
		// Add your codes
	}
	
	@Override<
	public void onAdStarted() {
	    AdAppLog.d(TAG, "onAdStarted");
	    // Add your codes
	}
	
	@Override
	public void onAdCompleted() {
	    AdAppLog.d(TAG, "onAdCompleted");
		// Add your codes
	}
	
	@Override
	public void onAdSkipTimeElapsed() {
	    AdAppLog.d(TAG, "onAdSkipTimeElapsed");
		// Add your codes
	}
	
	@Override
	public void onAdPlaybackError() {
	    AdAppLog.d(TAG, "onAdPlaybackError");
		// Add your codes
	}
};

The details of the video listener are as follows (get the status of an ad using InterstitialAdLifecycleListener when the ad finishes):

Name

Detail

1 OnAdStarted When starting the playback of a video ad.

For example, do not provide a reward because the ad is closed before the playback is started, that is, if this listener is not called before the ad is closed.

2 OnAdCompleted When the playback of a video ad is completed. Called when the user should be rewarded.

For example, provide a reward because the video ad is closed after the playback is completed if this listener is called before the ad is closed.

3 OnAdSkipTimeElapsed When the playback is done until skip time (5 secs) of the interstitial video ad

Warning: This event does not occur in the rewarded video ad.

For example, a game can start when this listener is called before the interstitial video ad is closed because playback of the video ad has reached or exceeded the skip time.

4 OnAdPlaybackError When a playback error occurs.

For example, you can determine whether to provide reward or not when a playback error occurs.

5 OnAdClosed When the ad is closed.