广告展示逻辑优化
This commit is contained in:
parent
aeda180cc8
commit
f77d2152be
@ -17,8 +17,8 @@ android {
|
|||||||
applicationId = "com.nova.keyboard"
|
applicationId = "com.nova.keyboard"
|
||||||
minSdk = 23
|
minSdk = 23
|
||||||
targetSdk = 34
|
targetSdk = 34
|
||||||
versionCode = 3
|
versionCode = 4
|
||||||
versionName = "1.0.2"
|
versionName = "1.0.3"
|
||||||
setProperty("archivesBaseName", "Nova keyboard_V" + versionName + "(${versionCode})_$timestamp")
|
setProperty("archivesBaseName", "Nova keyboard_V" + versionName + "(${versionCode})_$timestamp")
|
||||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||||
}
|
}
|
||||||
|
|||||||
@ -25,10 +25,8 @@ class KeyBoardApp : Application() {
|
|||||||
const val testMode = false
|
const val testMode = false
|
||||||
|
|
||||||
var SDKOK = false
|
var SDKOK = false
|
||||||
|
const val AD_INIT_ACTION ="ACTION"
|
||||||
|
|
||||||
var AD1Ready = false
|
|
||||||
var AD2Ready = false
|
|
||||||
var AD3Ready = false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
@ -109,9 +107,9 @@ class KeyBoardApp : Application() {
|
|||||||
Log.d(UnityAdManager.TAG,"----------------------application init")
|
Log.d(UnityAdManager.TAG,"----------------------application init")
|
||||||
UnityAds.initialize(this, unityGameID, testMode, object : IUnityAdsInitializationListener {
|
UnityAds.initialize(this, unityGameID, testMode, object : IUnityAdsInitializationListener {
|
||||||
override fun onInitializationComplete() {
|
override fun onInitializationComplete() {
|
||||||
|
LocalBroadcastManager.getInstance(this@KeyBoardApp).sendBroadcast(Intent(AD_INIT_ACTION))
|
||||||
SDKOK = true
|
SDKOK = true
|
||||||
UnityAdManager.loadAllAd()
|
UnityAdManager.loadAllAdNew()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -13,6 +13,8 @@ import com.unity3d.ads.IUnityAdsShowListener;
|
|||||||
import com.unity3d.ads.UnityAds;
|
import com.unity3d.ads.UnityAds;
|
||||||
import com.unity3d.ads.UnityAdsShowOptions;
|
import com.unity3d.ads.UnityAdsShowOptions;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
public class UnityAdManager {
|
public class UnityAdManager {
|
||||||
@ -22,98 +24,99 @@ public class UnityAdManager {
|
|||||||
public static String unitAd2 = "Nova_keyboard_ad_unity_inst_set";
|
public static String unitAd2 = "Nova_keyboard_ad_unity_inst_set";
|
||||||
public static String unitAd3 = "Nova_keyboard_ad_unity_inst_theme";
|
public static String unitAd3 = "Nova_keyboard_ad_unity_inst_theme";
|
||||||
|
|
||||||
public static String ACTION = "----";
|
|
||||||
|
public static String TAG = "--------";
|
||||||
|
|
||||||
|
|
||||||
public static String TAG = "-----------------tt";
|
|
||||||
|
|
||||||
public static String KEY_UNIT ="unitAD";
|
public static Boolean AD1Ready = false;
|
||||||
|
public static Boolean AD2Ready = false;
|
||||||
public static String KEY_OK ="unit_ready";
|
public static Boolean AD3Ready = false;
|
||||||
|
|
||||||
|
|
||||||
public static void loadAllAd(){
|
|
||||||
loadAd(unitAd1, new onLoadCallBack() {
|
|
||||||
@Override
|
|
||||||
public void onIsLoad(boolean loaded) {
|
|
||||||
Log.d(TAG, "-------unitAd1" + loaded);
|
|
||||||
|
|
||||||
Intent intent = new Intent(ACTION);
|
|
||||||
intent.putExtra(KEY_UNIT,unitAd1);
|
|
||||||
intent.putExtra(KEY_OK,loaded);
|
|
||||||
LocalBroadcastManager.getInstance(KeyBoardApp.appInstance).sendBroadcast(intent);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static void loadAllAdNew() {
|
||||||
|
if (!AD1Ready)
|
||||||
|
loadAd(unitAd1);
|
||||||
|
if (!AD2Ready)
|
||||||
|
loadAd(unitAd2);
|
||||||
|
if (!AD3Ready)
|
||||||
|
loadAd(unitAd3);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getReadyUnitAd() {
|
||||||
|
ArrayList<String> unitList = new ArrayList<>();
|
||||||
|
unitList.add(unitAd1);
|
||||||
|
unitList.add(unitAd2);
|
||||||
|
unitList.add(unitAd3);
|
||||||
|
Collections.shuffle(unitList);
|
||||||
|
|
||||||
|
for (String unit : unitList) {
|
||||||
|
if (Objects.equals(unit, unitAd1)) {
|
||||||
|
if (AD1Ready) {
|
||||||
|
return unit;
|
||||||
|
}
|
||||||
|
} else if (Objects.equals(unit, unitAd2)) {
|
||||||
|
if (AD2Ready) {
|
||||||
|
return unit;
|
||||||
|
}
|
||||||
|
} else if (Objects.equals(unit, unitAd3)) {
|
||||||
|
if (AD3Ready) {
|
||||||
|
return unit;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
loadAd(unitAd2, new onLoadCallBack() {
|
return null;
|
||||||
@Override
|
|
||||||
public void onIsLoad(boolean loaded) {
|
|
||||||
|
|
||||||
Intent intent = new Intent(ACTION);
|
|
||||||
intent.putExtra(KEY_UNIT,unitAd2);
|
|
||||||
intent.putExtra(KEY_OK,loaded);
|
|
||||||
LocalBroadcastManager.getInstance(KeyBoardApp.appInstance).sendBroadcast(intent);
|
|
||||||
|
|
||||||
Log.d("------------------tt", "-------unitAd2" + loaded);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
loadAd(unitAd3, new onLoadCallBack() {
|
|
||||||
@Override
|
|
||||||
public void onIsLoad(boolean loaded) {
|
|
||||||
|
|
||||||
Intent intent = new Intent(ACTION);
|
|
||||||
intent.putExtra(KEY_UNIT,unitAd3);
|
|
||||||
intent.putExtra(KEY_OK,loaded);
|
|
||||||
LocalBroadcastManager.getInstance(KeyBoardApp.appInstance).sendBroadcast(intent);
|
|
||||||
|
|
||||||
Log.d(TAG, "-------unitAd3" + loaded);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void loadAd(String adUnitId) {
|
public static void loadAd(String adUnitId) {
|
||||||
UnityAds.load(adUnitId, new IUnityAdsLoadListener() {
|
UnityAds.load(adUnitId, new IUnityAdsLoadListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onUnityAdsAdLoaded(String placementId) {
|
public void onUnityAdsAdLoaded(String placementId) {
|
||||||
Log.d(TAG, "------loaded-unit"+placementId);
|
Log.d(TAG, "------loaded-unit" + placementId);
|
||||||
|
if (Objects.equals(placementId, unitAd1)) {
|
||||||
|
AD1Ready = true;
|
||||||
|
} else if (Objects.equals(placementId, unitAd2)) {
|
||||||
|
AD2Ready = true;
|
||||||
|
} else if (Objects.equals(placementId, unitAd3)) {
|
||||||
|
AD3Ready = true;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onUnityAdsFailedToLoad(String placementId, UnityAds.UnityAdsLoadError error, String message) {
|
public void onUnityAdsFailedToLoad(String placementId, UnityAds.UnityAdsLoadError error, String message) {
|
||||||
Log.d(TAG, "------load fail-unit"+placementId+"-----" + message);
|
Log.d(TAG, "------load fail-unit" + placementId + "-----" + message);
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
|
||||||
private static void loadAd(String adUnitId, onLoadCallBack callBack) {
|
|
||||||
UnityAds.load(adUnitId, new IUnityAdsLoadListener() {
|
|
||||||
@Override
|
|
||||||
public void onUnityAdsAdLoaded(String placementId) {
|
|
||||||
Log.d(TAG, "------loaded-unit"+placementId);
|
|
||||||
callBack.onIsLoad(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onUnityAdsFailedToLoad(String placementId, UnityAds.UnityAdsLoadError error, String message) {
|
|
||||||
Log.d(TAG, "------load fail-unit"+placementId+"-----" + message);
|
|
||||||
callBack.onIsLoad(false);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void showUnityAd(Activity activity,String adUnitId, onShowCallBack onShowCallBack) {
|
|
||||||
|
|
||||||
|
public static void showUnityAd(Activity activity, String adUnitId, onShowCallBack onShowCallBack) {
|
||||||
UnityAds.show(activity, adUnitId, new UnityAdsShowOptions(), new IUnityAdsShowListener() {
|
UnityAds.show(activity, adUnitId, new UnityAdsShowOptions(), new IUnityAdsShowListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onUnityAdsShowFailure(String placementId, UnityAds.UnityAdsShowError error, String message) {
|
public void onUnityAdsShowFailure(String placementId, UnityAds.UnityAdsShowError error, String message) {
|
||||||
onShowCallBack.onShowFail();
|
onShowCallBack.onShowFail();
|
||||||
Log.d("------------------tt", "--------onUnityAdsShowFailure---message=" + message);
|
Log.d(TAG, "--------onUnityAdsShowFailure---message=" + message);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onUnityAdsShowStart(String placementId) {
|
public void onUnityAdsShowStart(String placementId) {
|
||||||
|
Log.d(TAG, "--------onUnityAdsShowStart---=" + placementId);
|
||||||
|
if (Objects.equals(placementId, unitAd1)) {
|
||||||
|
AD1Ready = false;
|
||||||
|
} else if (Objects.equals(placementId, unitAd2)) {
|
||||||
|
AD2Ready = false;
|
||||||
|
} else if (Objects.equals(placementId, unitAd3)) {
|
||||||
|
AD3Ready = false;
|
||||||
|
}
|
||||||
onShowCallBack.onShowSuccess();
|
onShowCallBack.onShowSuccess();
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -126,7 +129,8 @@ public class UnityAdManager {
|
|||||||
@Override
|
@Override
|
||||||
public void onUnityAdsShowComplete(String placementId, UnityAds.UnityAdsShowCompletionState state) {
|
public void onUnityAdsShowComplete(String placementId, UnityAds.UnityAdsShowCompletionState state) {
|
||||||
onShowCallBack.onShowClose();
|
onShowCallBack.onShowClose();
|
||||||
Log.d("------------------tt", "--------onUnityAdsShowComplete---");
|
Log.d(TAG, "--------onUnityAdsShowComplete---");
|
||||||
|
loadAd(placementId);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -62,7 +62,9 @@ class ApplyActivity : AppCompatActivity() {
|
|||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
setContentView(R.layout.activity_apply)
|
setContentView(R.layout.activity_apply)
|
||||||
showEnterAd()
|
showInterAd{
|
||||||
|
|
||||||
|
}
|
||||||
findViewId()
|
findViewId()
|
||||||
getExtraData()
|
getExtraData()
|
||||||
displayData()
|
displayData()
|
||||||
@ -71,22 +73,7 @@ class ApplyActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private fun showEnterAd() {
|
|
||||||
UnityAdManager.showUnityAd(this, UnityAdManager.unitAd2, object : onShowCallBack {
|
|
||||||
override fun onShowFail() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onShowClose() {
|
|
||||||
UnityAdManager.loadAd(UnityAdManager.unitAd2)
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onShowSuccess() {
|
|
||||||
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getExtraData() {
|
private fun getExtraData() {
|
||||||
displayUrl = intent.getStringExtra(DISPLAY_URL_KEY).toString()
|
displayUrl = intent.getStringExtra(DISPLAY_URL_KEY).toString()
|
||||||
@ -215,7 +202,9 @@ class ApplyActivity : AppCompatActivity() {
|
|||||||
} else {
|
} else {
|
||||||
fileList[0]?.path?.let {
|
fileList[0]?.path?.let {
|
||||||
runOnUiThread {
|
runOnUiThread {
|
||||||
showInterAd(it)
|
showInterAd {
|
||||||
|
apply(it)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -237,21 +226,25 @@ class ApplyActivity : AppCompatActivity() {
|
|||||||
).show()
|
).show()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun showInterAd(path: String) {
|
private fun showInterAd(action:()->Unit) {
|
||||||
UnityAdManager.showUnityAd(this, UnityAdManager.unitAd3, object : onShowCallBack {
|
val readyUnitAd = UnityAdManager.getReadyUnitAd()
|
||||||
override fun onShowFail() {
|
if (readyUnitAd == null) {
|
||||||
apply(path)
|
action.invoke()
|
||||||
}
|
} else {
|
||||||
|
UnityAdManager.showUnityAd(this, readyUnitAd, object : onShowCallBack {
|
||||||
|
override fun onShowFail() {
|
||||||
|
action.invoke()
|
||||||
|
}
|
||||||
|
|
||||||
override fun onShowClose() {
|
override fun onShowClose() {
|
||||||
apply(path)
|
action.invoke()
|
||||||
UnityAdManager.loadAd(UnityAdManager.unitAd3)
|
}
|
||||||
|
|
||||||
}
|
override fun onShowSuccess() {
|
||||||
|
|
||||||
override fun onShowSuccess() {
|
}
|
||||||
|
|
||||||
}
|
})
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -22,16 +22,9 @@ class NovaActivity : AppCompatActivity() {
|
|||||||
private lateinit var progressBar: ProgressBar
|
private lateinit var progressBar: ProgressBar
|
||||||
private lateinit var novaIm: ImageView
|
private lateinit var novaIm: ImageView
|
||||||
private var countTime = 11000L
|
private var countTime = 11000L
|
||||||
private lateinit var myTimer: Timer
|
|
||||||
private lateinit var timer: CountDownTimer
|
private lateinit var timer: CountDownTimer
|
||||||
private var needShow = true
|
private var needShow = true
|
||||||
|
|
||||||
private var isShowInterAd = false
|
|
||||||
|
|
||||||
private var hasRecice = false
|
|
||||||
|
|
||||||
private var count = 0
|
|
||||||
|
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
@ -45,96 +38,80 @@ class NovaActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
private fun init() {
|
private fun init() {
|
||||||
|
|
||||||
if (KeyBoardApp.SDKOK) {
|
timer = object : CountDownTimer(countTime, 100) {
|
||||||
Log.d(UnityAdManager.TAG, "-----SDKOK")
|
override fun onTick(millisUntilFinished: Long) {
|
||||||
startShowAd(UnityAdManager.unitAd1) {
|
Log.d(
|
||||||
if (it) {
|
UnityAdManager.TAG,
|
||||||
toHome()
|
"--------millisUntilFinished=${millisUntilFinished} $needShow"
|
||||||
} else {
|
)
|
||||||
startShowAd(UnityAdManager.unitAd2) {
|
if (needShow) {
|
||||||
|
startShowAd({
|
||||||
toHome()
|
toHome()
|
||||||
}
|
}, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onFinish() {
|
||||||
|
Log.d(UnityAdManager.TAG, "--------onFinish needShow=${needShow}")
|
||||||
|
if (needShow) {
|
||||||
|
startShowAd({
|
||||||
|
toHome()
|
||||||
|
}, true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
if (KeyBoardApp.SDKOK) {
|
||||||
|
Log.d(UnityAdManager.TAG, "--------initSDK true")
|
||||||
|
timer.start()
|
||||||
} else {
|
} else {
|
||||||
Log.d(UnityAdManager.TAG, "-----SDKOK false")
|
|
||||||
LocalBroadcastManager.getInstance(this).registerReceiver(object : BroadcastReceiver() {
|
LocalBroadcastManager.getInstance(this).registerReceiver(object : BroadcastReceiver() {
|
||||||
override fun onReceive(context: Context?, intent: Intent?) {
|
override fun onReceive(context: Context?, intent: Intent?) {
|
||||||
Log.d(UnityAdManager.TAG, "-----onReceive")
|
Log.d(UnityAdManager.TAG, "--------onReceive")
|
||||||
count++
|
timer.start()
|
||||||
if (hasRecice) return
|
|
||||||
if (intent != null) {
|
|
||||||
val unitAd = intent.getStringExtra(UnityAdManager.KEY_UNIT)
|
|
||||||
val booleanExtra = intent.getBooleanExtra(UnityAdManager.KEY_OK, false)
|
|
||||||
if (unitAd != null && booleanExtra) {
|
|
||||||
hasRecice = true
|
|
||||||
startShowAd(unitAd) {
|
|
||||||
toHome()
|
|
||||||
Log.d(UnityAdManager.TAG, "-----111toHome")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (count == 3 && !hasRecice) {
|
|
||||||
Log.d(UnityAdManager.TAG, "-----2222toHome")
|
|
||||||
toHome()
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}, IntentFilter(UnityAdManager.ACTION))
|
|
||||||
|
}, IntentFilter(KeyBoardApp.AD_INIT_ACTION))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//
|
|
||||||
// timer = object : CountDownTimer(countTime, 500) {
|
|
||||||
// override fun onTick(millisUntilFinished: Long) {
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// override fun onFinish() {
|
|
||||||
// if (needShow) {
|
|
||||||
// startShowAd {
|
|
||||||
// toHome()
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
startAd()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun startAd() {
|
|
||||||
// timer.start()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun findView() {
|
private fun findView() {
|
||||||
novaIm = findViewById(R.id.nova_picture)
|
novaIm = findViewById(R.id.nova_picture)
|
||||||
progressBar = findViewById<ProgressBar>(R.id.nova_progress)
|
progressBar = findViewById<ProgressBar>(R.id.nova_progress)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun startShowAd(id: String, action: (isSHow: Boolean) -> Unit) {
|
|
||||||
UnityAdManager.showUnityAd(this, id, object : onShowCallBack {
|
private fun startShowAd(action: () -> Unit, isFinish: Boolean) {
|
||||||
override fun onShowFail() {
|
val readyUnitAd = UnityAdManager.getReadyUnitAd()
|
||||||
needShow = true
|
if (readyUnitAd != null) {
|
||||||
UnityAdManager.loadAd(UnityAdManager.unitAd1)
|
needShow = false
|
||||||
action.invoke(false)
|
UnityAdManager.showUnityAd(this@NovaActivity, readyUnitAd, object :
|
||||||
|
onShowCallBack {
|
||||||
|
override fun onShowFail() {
|
||||||
|
action.invoke()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onShowClose() {
|
||||||
|
action.invoke()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onShowSuccess() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
if (isFinish) {
|
||||||
|
action.invoke()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onShowClose() {
|
}
|
||||||
action.invoke(true)
|
|
||||||
UnityAdManager.loadAd(UnityAdManager.unitAd1)
|
|
||||||
UnityAdManager.loadAd(UnityAdManager.unitAd2)
|
|
||||||
UnityAdManager.loadAd(UnityAdManager.unitAd3)
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onShowSuccess() {
|
|
||||||
needShow = false
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private fun toHome() {
|
private fun toHome() {
|
||||||
startActivity(Intent(this, HomeActivity::class.java))
|
startActivity(Intent(this, HomeActivity::class.java))
|
||||||
finish()
|
finish()
|
||||||
@ -142,7 +119,7 @@ class NovaActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
// timer.cancel()
|
timer.cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user