How to integrate Mobvista Ads SDK(Android)

How to integrate Mobvista Ads SDK(Android)
V2.1.0
Introduction
This document will guide you through the integration process of the Mobvista SDK, which will
help you monetize your traffic from Android applications.
NOTES:

The code samples in this document can be copy/pasted into your source code

Please notice that steps 1-2 are mandatory

If you have any questions, contact us via [email protected]
Step1. Configure your “libs” file and “res” file
IMPORTANT: This is a mandatory step
Copy the “libs” file to your application’s “libs” file
Copy the “res/drawable” file to your application’s “res/drawable” file
Copy the “res/drawable-hdpi” file to your application’s “res/drawable-hdpi” file
Copy the “res/layout” file to your application’s “res/layout” file
Example:
Step2. Update your Manifest file
IMPORTANT: This is a mandatory step
Under the main manifest tag, add the following permissions:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
Note: More about permissions usage, please refer to Permissions Explained.
<!—For download, necessary -->
<service
android:name="com.framework.download.DownloadService"
android:enabled="true" />
Under the application tag, add your API KEY and APP ID:
<meta-data android:name="mobvista.ads.sdk.apikey" android:value="your api key"/>
<meta-data android:name="mobvista.ads.sdk.appid" android:value="your app id"/>
Note: You can find API KEY and APP ID in the developers’ portal.
Example:
Interactive Ads (Highly recommended)
You can show the interactive ad by simply adding this code in main thread:
AnimationLayout animationView = new AnimationLayout(this);
addContentView(animationView, new android.widget.RelativeLayout.LayoutParams(
android.widget.RelativeLayout.LayoutParams.MATCH_PARENT,
android.widget.RelativeLayout.LayoutParams.MATCH_PARENT));
Optional: You can customize when to show the ads, please refer to advanced usage
Floating icon Ads (Recommended)
1) In your activity, new an instance in your main thread using this constructor:
private FloatAdView adView
= new FloatAdView(this);
Optional: If you want to monitor the status of ad, use this constructor; adListener can be
found in advanced usage.
private FloatAdView adView
= new FloatAdView(this, adListener);
//The two parameters of this constructor is the activity and adListener.
Optional: If you want to customize the floating icon, use this constructor
private FloatAdView adView
= new FloatAdView(this, resid, adListener);
//The three parameters of this constructor is the activity, image resource id and
AdListener. resid is the resource id of customized icon, icon size 72*72 is suggested.
2) Override onDestroy(), onResume(), onPause(), onConfigurationChanged(), and add the call to
adView.onDestroy(), adView.onResume(), adView.onPause(), adView.onConfigurationChanged():
@Override
protected void onDestroy() {
mFloatView.onDestory();
super.onDestroy();
}
@Override
protected void onResume() {
if (mFloatView != null)
mFloatView.onResume();
super.onResume();
}
@Override
protected void onPause() {
if (mFloatView != null)
mFloatView.onPause();
super.onPause();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mFloatView.onConfigurationChanged(newConfig);
};
Example:
Showing Expandable Interstitial Ads
1) In your activity, add this in the main thread:
sdkManager = new SDKManager(this);
Example:
2) Show Expandable Interstitial Ad:
sdkManager.showOverlayAd();
Optional: If you want to monitor the status of ad, use this constructor; adListener can be
found in advanced usage.
sdkManager.showOverlayAd(adListener);
3) Override the onDestroy(), onConfigurationChanged() methods:
@Override
public void onDestroy(){
sdkManager.onDestroy();
super.onDestroy();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mSdkManager.onConfigurationChanged(newConfig);
};
Showing 3D App-Wall Ads (Highly Recommended)
1) In your activity, add this in the main thread:
sdkManager = new SDKManager(this);
Example:
2) Show 3D App-Wall Ad:
sdkManager.showAppWallAd();
Optional: If you want to monitor the status of ad, use this constructor; adListener can
be found in advanced usage.
sdkManager.showAppWallAd(adListener);
3) Override the onDestroy(), onConfigurationChanged() methods:
@Override
public void onDestroy(){
sdkManager.onDestroy();
super.onDestroy();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mSdkManager.onConfigurationChanged(newConfig);
Showing Banners
To show the Banner Ads, add the following view inside your .xml file and .java file:
1) //add this in your .xml file:
<com.mobvista.sdk.ad.view.BannerAdImageLayout
android:id="@+id/banner_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
refreshtime="30"<!-- Refresh time should be between 20 to 40 seconds,
default value is 30 seconds>
adtype="banner"/>
2) //add this in main thread in your .java file:
sdkManager.showBannerAd(bannerView);
Optional: If you want to monitor the status of ad, use this constructor; adListener can be
found in advanced usage.
sdkManager.showBannerAd(bannerView, adListener);
Advanced Usage
1. Permissions Explained
To show a variety of ad units in the apps, the following permissions are needed.
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
INTERNET /ACCESS_NETWORK_STATE
Allows an application to create network sockets.
ACCESS_WIFI_STATE
Allows an application to view the information about the state of Wi-Fi.
READ_PHONE_STATE (optional but recommended)
Allows an application to monitor the app behavior to optimize the ads show.
WRITE_EXTERNAL_STORAGE
Allows an application to download ad creatives.
SYSTEM_ALERT_WINDOW
Allows using floating icon ad.
If you have any questions regarding the permissions, feel free to contact us at [email protected].
2. Customize the interactive ads
If you want to show interactive ads in specific time, for example, after making an achievement in the game, a gift
is given to them.
You can make it disappear at first, using this function:
animationView.setVisibility(View.GONE);//let interactive ads disappear
And call it when you want it to show:
animationView.setVisibility(View.VISIBLE);//show interactive ads
3. AdListener
//AdListener is an optional function, all the ad types can be listened if needed.
AdListener adListener = new AdListener()
{
public void onAdError(Msg msg) {
//failed to request ad
}
public void onAdShow() {
//success to show ad
}
public void onNoAd() {
//no ads
}
public void onAdClick() {
//ad is clicked
}
public void onAdClose() {
// ad is closed
}
};
4. Unity 3D and cocos2dx engine
Regarding the banner ads, if you use unity3d engine or cocos2dx engine, but failed to use .xml
file, you can use an alternative way as below:
//main thread, onCreate() function
android.view.ViewGroup.LayoutParams params = new android.widget.LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
BannerAdImageLayout bannerView = sdkManager.getBannerView(this, params, refreshtime);
ViewGroup vg = (ViewGroup) rootView.findViewById(R.id.content);
vg.addView(bannerView);