Edited at

AppLovinSDKを android java(ネイティブ)で導入する方法

More than 1 year has passed since last update.


AppLovinSDKを android java(ネイティブ)で導入する方法

Unityやcocos2dxとは違い、androidのライフサイクルが絡んでくるので、

個人的にはネイティブで導入する方が難しかった。

複数の動画SDKを入れる場合は、メディエーションで入れるととコールバックがまとめられ楽できるかもw。汗

getActivityやgetApplicationContext等をシングルトンとして引き回したい場合の参考にも。w汗


インスタースティシャル版の導入

Android Studio等でツール・アプリを作る際は、、

初期化後、getApplicationContext()や、getAcitivy()の情報を引き回す必要があり、

それらに注意する。

WeakReferenceというものをキャッシュされたののを引き回す事ができるので、

予め、getApplicationContext()とgetActivity()をできるようにしておくと便利

例:


// MainActivityでセットした参照をキャッシュを使って、applovin再生
final WeakReference<MainActivity> weakRef = new WeakReference<MainActivity>(MainActivity.getInstance());


getApplicationContext()を引きまわす準備


シングルトンの準備


MyApplication.java


public class MyApplication extends Application {

//シングルトンの代わりとして利用(アプリが終了するまで存在する
private static MyApplication sInstance;

// 取得用
public static MyApplication getInstance() {
return sInstance;
}

@Override
public void onCreate() {
super.onCreate();

sInstance = this;



呼び出し側


MyApplication.getInstance();

これで、AppCompatAcitivy等、Acitivyも持っている場合はこれだけでOK。


getAcitivy()を引き回す

Fragment等で、Acitivyが呼び出したい画面でActivityが取得できない場合は、

FragmentAcitivyを継承しているところで、Singleton化してあげる必要があります。


MainAcitivy.java


public class MainActivity extends FragmentActivity {

private static MainActivity mainActivityInstance; //シングルトンの代わりとして利用(アプリが終了するまで存在する

// 取得用
public static MainActivity getInstance() {
return mainActivityInstance;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mainActivityInstance = this;



呼び出し側


MainActivity.getInstance()


AppLovin動画再生


HogeFrament.java

private AppLovinIncentivizedInterstitial incentivizedInterstitial;

//〜〜省略〜〜

final WeakReference<MainActivity> weakRef = new WeakReference<MainActivity>(MainActivity.getInstance()); // ★先程のMainActivityで用意したインスタンスを使用

//〜〜省略〜〜

// Applovin リワード型の読み込み準備
incentivizedInterstitial = AppLovinIncentivizedInterstitial.create(MyApplication.getInstance()); //先程用意した、MyApplicationのインスタンスを使って、applovinを初期化
incentivizedInterstitial.preload(new AppLovinAdLoadListener() {
@Override
public void adReceived(AppLovinAd appLovinAd) {
// showButton.setEnabled(true); //動画視聴ボタンを有効化等
}

@Override
public void failedToReceiveAd(int errorCode) {
log("Rewarded video failed to load with error code " + errorCode);
}
});

//〜〜省略〜〜

// Applivn リワード型再生可能かチェック
if (incentivizedInterstitial.isAdReadyToDisplay()) {

// Applovin コールバック
// Reward Listener
AppLovinAdRewardListener adRewardListener = new AppLovinAdRewardListener() {
@Override
public void userRewardVerified(AppLovinAd appLovinAd, Map map) {
// AppLovin servers validated the reward. Refresh user balance from your server. We will also pass the number of coins
// awarded and the name of the currency. However, ideally, you should verify this with your server before granting it.

// i.e. - "Coins", "Gold", whatever you set in the dashboard.
String currencyName = (String) map.get("currency");

// For example, "5" or "5.00" if you've specified an amount in the UI.
String amountGivenString = (String) map.get("amount");

log("Rewarded " + amountGivenString + " " + currencyName);
log("");
// By default we'll show a alert informing your user of the currency & amount earned.
// If you don't want this, you can turn it off in the Manage Apps UI.

Logger.d("Applovin_userRewardVerified");

// 動画視聴完了ポップアップダイアログ表示等...
DialogFragment hintDialogFragment = new HintDialogFragment();

Bundle args = new Bundle();
args.putInt("stamp_id", stampId); //引数
hintDialogFragment.setArguments(args);
hintDialogFragment.show(MainActivity.getInstance().getFragmentManager(), "dialog"); // ★先程のMainActivityで用意したインスタンスを使って、dialog等のview表示に!
}

@Override
public void userOverQuota(AppLovinAd appLovinAd, Map map) {
// Your user has already earned the max amount you allowed for the day at this point, so
// don't give them any more money. By default we'll show them a alert explaining this,
// though you can change that from the AppLovin dashboard.

Logger.d("Applovin_userOverQuota");
}

@Override
public void userRewardRejected(AppLovinAd appLovinAd, Map map) {
// Your user couldn't be granted a reward for this view. This could happen if you've blacklisted
// them, for example. Don't grant them any currency. By default we'll show them an alert explaining this,
// though you can change that from the AppLovin dashboard.

Logger.d("Applovin_userRewardRejected");
}

@Override
public void validationRequestFailed(AppLovinAd appLovinAd, int responseCode) {

Logger.d("Applovin_validationRequestFailed" + responseCode);
if (responseCode == AppLovinErrorCodes.INCENTIVIZED_USER_CLOSED_VIDEO) {
// Your user exited the video prematurely. It's up to you if you'd still like to grant
// a reward in this case. Most developers choose not to. Note that this case can occur
// after a reward was initially granted (since reward validation happens as soon as a
// video is launched).
} else if (responseCode == AppLovinErrorCodes.INCENTIVIZED_SERVER_TIMEOUT || responseCode == AppLovinErrorCodes.INCENTIVIZED_UNKNOWN_SERVER_ERROR) {
// Some server issue happened here. Don't grant a reward. By default we'll show the user
// a alert telling them to try again later, but you can change this in the
// AppLovin dashboard.
} else if (responseCode == AppLovinErrorCodes.INCENTIVIZED_NO_AD_PRELOADED) {
// Indicates that the developer called for a rewarded video before one was available.
// Note: This code is only possible when working with rewarded videos.
}
}

@Override
public void userDeclinedToViewAd(AppLovinAd appLovinAd) {
// This method will be invoked if the user selected "no" when asked if they want to view an ad.
// If you've disabled the pre-video prompt in the "Manage Apps" UI on our website, then this method won't be called.

Logger.d("Applovin_userDeclinedToViewAd");
}
};

// Video Playback Listener
AppLovinAdVideoPlaybackListener adVideoPlaybackListener = new AppLovinAdVideoPlaybackListener() {
@Override
public void videoPlaybackBegan(AppLovinAd appLovinAd) {
log("Video Started");
Logger.d("Applovin_videoPlaybackBegan");
}

@Override
public void videoPlaybackEnded(AppLovinAd appLovinAd, double v, boolean b) {
log("Video Ended");

Logger.d("Applovin_Video_endied");
}
};

// Ad Dispaly Listener
AppLovinAdDisplayListener adDisplayListener = new AppLovinAdDisplayListener() {
@Override
public void adDisplayed(AppLovinAd appLovinAd) {
log("Ad Displayed");
}

@Override
public void adHidden(AppLovinAd appLovinAd) {
log("Ad Dismissed");
}
};

// Ad Click Listener
AppLovinAdClickListener adClickListener = new AppLovinAdClickListener() {
@Override
public void adClicked(AppLovinAd appLovinAd) {
log("Ad Click");
}
};

// 動画表示
incentivizedInterstitial.show(weakRef.get(), adRewardListener, adVideoPlaybackListener, adDisplayListener, adClickListener);

getDialog().dismiss();

log("Interstitial Displayed");
} else {
// Ideally, the SDK preloads ads when you initialize it in your launch activity
// you can manually load an ad as demonstrated in InterstitialManualLoadingActivity
log("Interstitial not ready for display.\nPlease check SDK key or internet connection.");
}


初期化は起動時にやっておくと、バッファリング時間の省略になると思うので、起動時にやっておいた方がいいと思います。