広告SDK(Android)
# 1. 事前準備
- Create a Fascode Ads account (If not, Create an account)
- Register an app(Not yet? Go to Register an app)
- Use Android Studio 3.4 or higher
- Target Android 6.0 or higher (
minSdkVersion16 or higher) compileSdkVersion28 or higher
# 2. SDKの導入
Apps can import the Fascode Ads SDK with a Gradle dependency that points to Maven Central repository
- project-level build.gradle
allprojects {
repositories {
mavenCentral()
}
}
Next, open the app-level build.gradle file for your app, and add the SDK to "dependencies" section as below.
dependencies {
implementation "com.fascode.ads:fascode-ads:1.+"
...// Other dependencies
}
# 3. SDKの使用
# 3.1 Publisher ID取得
Go to Account to get the Publisher Id
# 3.2 SDK初期化
Before loading ads, have your app initialize the Fascode Ads SDK by calling FascodeAds.shared().init(...) which initializes the SDK and calls back a completion listener once initialization is complete (or after a 30-second timeout). This needs to be done only once, ideally at app launch.
import com.fascode.ads.FascodeAds
import com.fascode.ads.initialization.InitializationStatus
import com.fascode.ads.initialization.onInitializationComplete
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
FascodeAds.shared().init(this, "<Your Publisher ID>") {}
}
}
import com.fascode.ads.FascodeAds;
import com.fascode.ads.initialization.InitializationStatus;
import com.fascode.ads.initialization.onInitializationComplete;
class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
FascodeAds.shared().init(this, "<Your Publisher ID>", new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
}
}
# 4. 広告ユニット作成
For Android App, you can choose to show the following types of ads.
- Banner Ad
- Interstitial Ad
- Video Ad
- Custom Ad
- Reward Ad
Tips
- Reward Ad is actually using other types but supposed to be interact with your app, such as reward users for watching over an video ad.
# 4.2 Ad Targeting
By default all kinds of targetings are picked for your Ad Unit, but of course you can manually pick up the best ones to optimize your advertising.
# 4.3 Set the refresh interval(Banner Ad only)
For Banner Ad, you can change the refresh interval to decide how many seconds to refresh the ads.
# 4.4 Save Ad Unit
Click Save, an Ad Unit ID will be generated to be used by SDK in your app.
# 5. Show Ads
# 5.1 Banner Ad
# Add BannerAd to layout
# main_activity.xml
...
<com.fascode.ads.BannerAd
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/bannerAd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adUnitId="<Ad Unit ID>">
</com.fascode.ads.BannerAd>
...
You need to set the attribute below:
- adUnitId:
Ad Unit IDgenerated by 4. Create Ad Unit
You can alternatively create BannerAd programmatically:
mBannerAd = FascodeAds.shared().createBannerAd(this, "<Ad Unit ID>")
// TODO: Add mBannerAd to your Layout
mBannerAd = FascodeAds.shared().createBannerAd(this, "<Ad Unit ID>");
// TODO: Add mBannerAd to your Layout
# Load an ad
Once the BannerAd is in place, the next step is to load an ad. That's done with the loadAd() method in the BannerAd class. It takes an AdRequest parameter, which holds runtime information (such as targeting info) about a single ad request.
Here's an example that shows how to load an ad in the onCreate() method of an Activity:
class MainActivity : AppCompatActivity() {
lateinit var mBannerAd : BannerAd
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// In case BannerAd is added to Layout(R.layout.activity_main)
mBannerAd = findViewById(R.id.bannerAd)
val adRequest = FascodeAds.shared().createAdRequest();
mBannerAd?.loadAd(adRequest)
}
}
// MainActivity.java
public class MainActivity extends AppCompatActivity {
private BannerAd mBannerAd;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// In case BannerAd is added to Layout(R.layout.activity_main)
mAdView = findViewById(R.id.bannerAd);
AdRequest adRequest = FascodeAds.shared().createAdRequest();
mBannerAd.loadAd(adRequest);
}
}
Tips
Banner Ad is usually displayed at the bottom or top of the screen. Please ensure that the advertisement can be displayed normally and does not interfere with other views in the layout.
# 5.2 Interstitial Ad
Interstitial ads cannot be inserted into the layout, but can only be displayed programmatically, and make sure that the close button (the button in the upper right corner) is visible and clickable. Please call the following code when you need to display interstitial ads (for example, in Activity.onCreate()):
val interstitialAd = FascodeAds.shared().createInterstitialAd(this, "<Ad Unit ID>");
val adRequest = FascodeAds.shared().createAdRequest();
interstitialAd.loadAd(adRequest);
InterstitialAd interstitialAd = FascodeAds.shared().createInterstitialAd(this, "<Ad Unit ID>");
val adRequest = FascodeAds.shared().createAdRequest();
interstitialAd.loadAd(adRequest);
# 5.3 Video Ad
As same as interstitial ads, in-stream video ads can only be displayed programmatically. Please call the following code when you need to display an in-stream video ad (for example, at the end of a game):
val videoAd = FascodeAds.shared().createVideoAd(this, "<Ad Unit ID>");
val adRequest = FascodeAds.shared().createAdRequest();
videoAd.loadAd(adRequest);
VideoAd videoAd = FascodeAds.shared().createVideoId(this, "<Ad Unit ID>");
val adRequest = FascodeAds.shared().createAdRequest();
videoAd.loadAd(adRequest);
# 5.4 Custom Ad
If you choose Custom Ad type for your Ad Unit, you will have a fixed and completely owned ad displayed. To implement it in your app, use the same ways for 5.1 Banner Ad,5.2 Interstitial Ad ,5.3 Video Ad.
# 5.5 Reward Ad
To encourage users to watch over an Ad, you can reward them with some in-app goods or coins, with listening the 6. Ad Events, you can easily implement it.
# 6. Ad Event
An Ad has its life-cycle, from load to end, including clicking and back, etc, all those events can be listened by AdListener.
To use AdListener, call setAdListener() as below:
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
// Ad Loading completed
}
@Override
public void onAdFailedToLoad(int errorCode) {
// Ad loading failed
}
@Override
public void onAdOpened() {
// Ad showing started
}
@Override
public void onAdClicked() {
// Ad clicked
}
@Override
public void onAdClosed() {
// Ad closed
}
});
# 7. Test
Please use the test Ad Unit ID for you test
adUnitId: Fascode-Ads-Test-AdUnitId