This guide demonstrates how to integrate Interstitial Ad Unit in a new cocos2d-x project.
1. Install the SDK
Follow the instructions and install the SDK.
2. Initialize the SDK
Add the following at the end of bool HelloWorld::init() in HelloWorldScene.cpp:
JniHelper::callStaticVoidMethod("org.cocos2dx.cpp.AppActivity", "initDIOSDK", "<APP-ID>");
Then add the following at the end of AppActivity.java:
public static void initDIOSDK(final String appId) {
new Handler(getContext().getMainLooper()).post(new Runnable(){public void run(){
Controller.getInstance().init(getContext(), appId, new SdkInitListener() {
@Override
public void onInit() {
Log.i("DIOSDK", "Controller initialized");
}
@Override
public void onInitError(String msg) {
Log.e("DIOSDK", msg);
}
});
}});
}
3. Show Ad
a. In case of AdMob Mediation Adapter integration follow the guide.
b. In case of MoPub Mediation Adapter integration follow the guide.
c. In case of direct SDK integration follow the steps below:
- In void HelloWorld::menuCloseCallback(Ref*) in HelloWorldScene.cpp, comment the line which closes the game.
- Then add the following:
JniHelper::callStaticVoidMethod("org.cocos2dx.cpp.AppActivity", "showDIOSDKAd", "<PLACEMENT-ID>");
Then add the following at the end of AppActivity.java:
public static void showDIOSDKAd(final String placementId) {
new Handler(getContext().getMainLooper()).post(new Runnable(){public void run(){
if (Controller.getInstance().isInitialized()) {
Placement placement;
try {
placement = Controller.getInstance().getPlacement(placementId);
} catch (DioSdkException e) {
Log.e("DIOSDK", e.getLocalizedMessage());
return;
}
AdRequest adRequest = placement.newAdRequest();
adRequest.setAdRequestListener(new AdRequestListener() {
@Override
public void onAdReceived(AdProvider adProvider) {
adProvider.setAdLoadListener(new AdLoadListener() {
@Override
public void onLoaded(Ad ad) {
ad.setEventListener(new AdEventListener() {
@Override
public void onShown(Ad ad) {
Log.i("DIOSDK", "onShown");
}
@Override
public void onFailedToShow(Ad ad) {
Log.e("DIOSDK", "onFailedToShow");
}
@Override
public void onClicked(Ad ad) {
Log.i("DIOSDK", "onClicked");
}
@Override
public void onClosed(Ad ad) {
Log.i("DIOSDK", "onClosed");
}
@Override
public void onAdCompleted(Ad ad) {
Log.i("DIOSDK", "onAdCompleted");
}
});
ad.showAd(getContext());
}
@Override
public void onFailedToLoad() {
Log.e("DIOSDK", "Ad for placement " + placementId + " failed to load");
}
});
try {
adProvider.loadAd();
} catch(DioSdkException e) {
Log.e("DIOSDK", e.getLocalizedMessage());
}
}
@Override
public void onNoAds() {
Log.e("DIOSDK", "No Ads placement " + placementId);
}
});
adRequest.requestAd();
}
}});
}
Then run and press the close button to show the ad.
Comments
0 comments
Please sign in to leave a comment.