Commit de4562fd authored by mengcuiguang's avatar mengcuiguang

代码优化

parent e4fdc1e3
......@@ -93,6 +93,10 @@
android:name=".ui.activitys.SplashAdActivity"
android:exported="false"
android:screenOrientation="portrait" />
<activity
android:name=".ui.activitys.SplashForeAdActivity"
android:exported="false"
android:screenOrientation="portrait" />
<activity
android:name=".ui.activitys.MobileLoginActivity"
android:configChanges="orientation|screenSize|keyboardHidden"
......
package com.duben.miaoquplaylet.ui.activitys
import android.os.Handler
import android.os.Looper
import android.view.KeyEvent
import com.duben.miaoquplaylet.R
import com.duben.miaoquplaylet.ad.AdStatusListener
import com.duben.miaoquplaylet.ad.splash.SplashManager
import com.duben.miaoquplaylet.ui.activitys.base.BaseActivity
import com.duben.miaoquplaylet.ui.widgets.countdowntimer.CountDownTimerSupport
import com.duben.miaoquplaylet.ui.widgets.countdowntimer.OnCountDownTimerListener
import kotlinx.android.synthetic.main.activity_gromore.*
/**
* 描述:gromore后台返回广告
* 作者:孟崔广
* 时间:2023/8/04 13:52
*/
class SplashForeAdActivity : BaseActivity() {
//是否强制跳转到主页面
private var onPaused = false
var mTimer: CountDownTimerSupport? = null
private val mMainHandler = Handler(Looper.getMainLooper())
override fun getContentViewLayoutID() = R.layout.activity_gromore_fore
override fun isApplyKitKatTranslucency() = false
override fun initViewsAndEvents() {
var timeout = 200L
mMainHandler.postDelayed({
SplashManager.showAd(
this@SplashForeAdActivity,
fl_gromore_root,
object : AdStatusListener {
override fun adSuccess() {
}
override fun adFail() {
goToMainActivity()
}
override fun adClose(vo: HashMap<String, Any>?) {
goToMainActivity()
}
})
// 防止广告展示异常,界面卡住
startTime()
}, timeout)
}
override fun onResume() {
super.onResume()
//判断是否该跳转到主页面
if (onPaused) {
goToMainActivity()
}
}
override fun onPause() {
super.onPause()
onPaused = true
}
override fun onDestroy() {
super.onDestroy()
onDestroyTimer()
}
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
return if (keyCode == KeyEvent.KEYCODE_BACK) {
true
} else super.onKeyDown(keyCode, event)
}
/**
* 跳转到主页面
*/
private fun goToMainActivity() {
finish()
}
/**
* 防止广告展示异常,界面卡住
*/
private fun startTime() {
onDestroyTimer()
mTimer = CountDownTimerSupport(3000L, 1000L)
mTimer?.setOnCountDownTimerListener(object : OnCountDownTimerListener {
override fun onTick(millisUntilFinished: Long) {
}
override fun onFinish() {
if (!isFinishing) {
// 3秒广告都没有展示 则进入首页
if (!SplashManager.getAdStatus()) {
onDestroyTimer()
goToMainActivity()
}
}
}
})
mTimer?.start()
}
private fun onDestroyTimer() {
mTimer?.stop()
mTimer = null
}
}
......@@ -6,10 +6,12 @@ import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import com.duben.miaoquplaylet.ad.splash.SplashManager;
import com.duben.miaoquplaylet.common.AppConfig;
import com.duben.miaoquplaylet.manager.ActivityPageManager;
import com.duben.miaoquplaylet.manager.TrackManager;
import com.duben.miaoquplaylet.mvp.presenters.VersionUpdatePresenter;
import com.duben.miaoquplaylet.ui.activitys.SplashForeAdActivity;
import java.lang.ref.WeakReference;
......@@ -19,7 +21,8 @@ import java.lang.ref.WeakReference;
*/
public class ForegroundOrBackground implements Application.ActivityLifecycleCallbacks {
private static ForegroundOrBackground instance;
public int count = 0;
private int count = 0;
private long activityStoppedTime = 0;
//获取当前最上层的activity
private static WeakReference<Activity> app_activity;
......@@ -66,11 +69,41 @@ public class ForegroundOrBackground implements Application.ActivityLifecycleCall
@Override
public void onActivityStarted(Activity activity) {
if (activity == null)
return;
String simpleName = activity.getClass().getSimpleName();
if (TextUtils.equals(simpleName, "SplashActivity") ||
TextUtils.equals(simpleName, "SplashAdActivity") ||
TextUtils.equals(simpleName, "SplashForeAdActivity") ||
TextUtils.equals(simpleName, "VipActivity")) {
count++;
return;
}
if (count == 0) {
long currentTimeMillis = System.currentTimeMillis();
long diff = currentTimeMillis - activityStoppedTime;
long diffInSeconds = diff / 1000;
System.out.println("mcg -->>>>>:: diffInSeconds=" + diffInSeconds + " adSuccessStatus=" + SplashManager.INSTANCE.isSuccessStatus());
if (diffInSeconds >= 10 && SplashManager.INSTANCE.isSuccessStatus()) {
activity.startActivity(new Intent(activity, SplashForeAdActivity.class));
}
}
count++;
}
@Override
public void onActivityStopped(Activity activity) {
count--;
if (count < 0) {
count = 0;
}
if (count == 0) {
System.out.println("mcg -->>>>>:: onActivityStopped");
activityStoppedTime = System.currentTimeMillis();
SplashManager.INSTANCE.preLoadAd(activity);
}
}
@Override
......
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fl_gromore_root"
android:layout_width="match_parent"
android:background="@color/graya"
android:layout_height="match_parent">
</FrameLayout>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment