Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Sign in / Register
Toggle navigation
A
appzxhy
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wanglei
appzxhy
Commits
bbe11103
Commit
bbe11103
authored
Jul 11, 2025
by
wanglei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[AI重构]AI重构MyApplication
parent
70df2429
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
72 additions
and
144 deletions
+72
-144
MyApplication.kt
app/src/main/java/com/simplecleaner/app/MyApplication.kt
+72
-144
No files found.
app/src/main/java/com/simplecleaner/app/MyApplication.kt
View file @
bbe11103
...
@@ -42,80 +42,41 @@ import kotlinx.coroutines.Dispatchers
...
@@ -42,80 +42,41 @@ import kotlinx.coroutines.Dispatchers
import
kotlinx.coroutines.MainScope
import
kotlinx.coroutines.MainScope
import
kotlinx.coroutines.launch
import
kotlinx.coroutines.launch
class
MyApplication
:
Application
()
{
class
MyApplication
:
Application
()
{
private
val
TAG
=
"MyApplication"
private
val
TAG
=
"MyApplication"
var
uuid
=
""
var
uuid
=
""
companion
object
{
companion
object
{
lateinit
var
appContext
:
MyApplication
lateinit
var
appContext
:
MyApplication
@JvmField
var
PAUSED_VALUE
=
0
@JvmField
var
PAUSED_VALUE
=
0
val
noLoadingActivities
=
listOf
(
val
noLoadingActivities
=
listOf
(
"full"
,
// 过滤全屏广告
"full"
,
"adActivity"
,
"AdActivity"
,
"AppLovinFullscreenActivity"
,
SplashActivity
::
class
.
java
.
simpleName
"adActivity"
,
"AdActivity"
,
"AppLovinFullscreenActivity"
,
SplashActivity
::
class
.
java
.
simpleName
,
// 返回前台时不跳转启动页的 activity
)
)
}
}
private
var
lastTimePause
=
0L
private
var
lastTimeResume
=
0L
override
fun
onCreate
()
{
override
fun
onCreate
()
{
super
.
onCreate
()
super
.
onCreate
()
appContext
=
this
appContext
=
this
initUuidIfNeeded
()
initGidAsync
()
initUUid
()
initAll
()
initGid
()
registerLifecycle
()
initApp
()
initLifeListener
()
initLanguage
()
initLanguage
()
}
}
private
fun
initLanguage
()
{
// 统一初始化入口
private
fun
initAll
()
{
// 初始化语种切换框架
AdsMgr
.
init
(
appContext
)
MultiLanguages
.
init
(
this
)
initPushAndAlarm
()
initAppConfigAndInstall
()
// 设置语种变化监听器
initFacebookAndSolar
()
MultiLanguages
.
setOnLanguageListener
(
object
:
OnLanguageListener
{
override
fun
onAppLocaleChange
(
oldLocale
:
Locale
,
newLocale
:
Locale
)
{
appLanguageSp
=
newLocale
.
language
appLanguageCountrySp
=
newLocale
.
country
Log
.
i
(
"MultiLanguages"
,
"监听到应用切换了语种,旧语种:$oldLocale,新语种:$newLocale"
)
}
override
fun
onSystemLocaleChange
(
oldLocale
:
Locale
,
newLocale
:
Locale
)
{
Log
.
i
(
"MultiLanguages"
,
"监听到系统切换了语种,旧语种:"
+
oldLocale
+
",新语种:"
+
newLocale
+
",是否跟随系统:"
+
MultiLanguages
.
isSystemLanguage
(
this
@MyApplication
)
)
}
})
}
private
fun
initGid
()
=
MainScope
().
launch
(
Dispatchers
.
IO
)
{
try
{
val
info
:
AdvertisingIdClient
.
Info
=
AdvertisingIdClient
.
getAdvertisingIdInfo
(
applicationContext
)
val
advertisingId
=
info
.
id
?:
"null"
SpConstObject
.
gid
=
advertisingId
}
catch
(
e
:
Exception
)
{
EventUtils
.
event
(
"gid_exception"
)
}
}
override
fun
attachBaseContext
(
base
:
Context
?)
{
super
.
attachBaseContext
(
MultiLanguages
.
attach
(
base
))
}
}
private
fun
initUUid
()
{
// UUID初始化
private
fun
initUuidIfNeeded
()
{
uuid
=
appUuid
uuid
=
appUuid
if
(
TextUtils
.
isEmpty
(
uuid
))
{
if
(
TextUtils
.
isEmpty
(
uuid
))
{
uuid
=
UUID
.
randomUUID
().
toString
()
+
System
.
currentTimeMillis
()
uuid
=
UUID
.
randomUUID
().
toString
()
+
System
.
currentTimeMillis
()
...
@@ -125,140 +86,107 @@ class MyApplication : Application() {
...
@@ -125,140 +86,107 @@ class MyApplication : Application() {
LogEx
.
logDebug
(
TAG
,
"uuid=${AppPreferences.getInstance().getString("
uuid
", "")}"
)
LogEx
.
logDebug
(
TAG
,
"uuid=${AppPreferences.getInstance().getString("
uuid
", "")}"
)
}
}
fun
initApp
()
{
// GID异步初始化
private
fun
initGidAsync
()
=
MainScope
().
launch
(
Dispatchers
.
IO
)
{
//初始化广告相关业务
try
{
AdsMgr
.
init
(
appContext
)
val
info
=
AdvertisingIdClient
.
getAdvertisingIdInfo
(
applicationContext
)
SpConstObject
.
gid
=
info
.
id
?:
"null"
initPush
()
}
catch
(
e
:
Exception
)
{
EventUtils
.
event
(
"gid_exception"
)
initAppConfig
()
initFacebook
()
initSolar
()
}
private
fun
initSolar
()
{
SolarEngineManager
.
getInstance
().
preInit
(
this
,
solarkey
)
appContext
.
initSolarEngine
(
true
)
}
private
fun
initFacebook
()
{
//初始化facebook sdk
FacebookSdk
.
sdkInitialize
(
applicationContext
)
AppEventsLogger
.
activateApp
(
this
)
}
private
fun
initPush
()
{
kotlin
.
runCatching
{
startStayJobService
()
}
}
}
// 推送、定时、闹钟等初始化
private
fun
initPushAndAlarm
()
{
kotlin
.
runCatching
{
startStayJobService
()
}
val
topic
=
GlobalConfig
.
PACKAGE_NAME
+
"_push"
val
topic
=
GlobalConfig
.
PACKAGE_NAME
+
"_push"
FCMManager
.
initFirebase
(
this
)
FCMManager
.
initFirebase
(
this
)
FCMManager
.
subscribeToTopic
(
topic
)
FCMManager
.
subscribeToTopic
(
topic
)
changeTimer
()
changeTimer
()
ScreenStatusReceiver
.
registerScreenStatusReceiver
(
this
)
ScreenStatusReceiver
.
registerScreenStatusReceiver
(
this
)
PackageReceiver
.
register
(
this
)
PackageReceiver
.
register
(
this
)
registerBatteryReceiver
(
this
)
registerBatteryReceiver
(
this
)
//workManager
schedulePeriodicWork
(
appContext
)
schedulePeriodicWork
(
appContext
)
listOf
(
24
,
48
,
72
).
forEach
{
startAlarm
(
appContext
,
it
)
}
startAlarm
(
appContext
,
24
)
startAlarm
(
appContext
,
48
)
startAlarm
(
appContext
,
72
)
//开启通知队列
MyNotificationManager
.
startNotificationQueue
()
MyNotificationManager
.
startNotificationQueue
()
}
}
private
fun
initAppConfig
()
{
// 配置和安装来源初始化
private
fun
initAppConfigAndInstall
()
{
//初始化sp的配置
NewComUtils
.
parseConfigBean
(
spConfig
)
NewComUtils
.
parseConfigBean
(
spConfig
)
NewComUtils
.
requestCfgCallBackMap
[
"changeTimer"
]
=
{
NewComUtils
.
requestCfgCallBackMap
.
put
(
"changeTimer"
)
{
LogEx
.
logDebug
(
TAG
,
"requestCfgCallBackMap changeTimer"
)
LogEx
.
logDebug
(
TAG
,
"requestCfgCallBackMap changeTimer"
)
changeTimer
()
changeTimer
()
}
}
InstallHelps
.
init
{
NewComUtils
.
requestCfg
()
}
}
InstallHelps
.
init
{
// Facebook和Solar初始化
NewComUtils
.
requestCfg
()
private
fun
initFacebookAndSolar
()
{
}
FacebookSdk
.
sdkInitialize
(
applicationContext
)
AppEventsLogger
.
activateApp
(
this
)
SolarEngineManager
.
getInstance
().
preInit
(
this
,
solarkey
)
appContext
.
initSolarEngine
(
true
)
}
}
private
var
lastTimePause
=
0L
// 多语言初始化
private
var
lastTimeResume
=
0L
private
fun
initLanguage
()
{
private
fun
isHotLaunch
():
Boolean
{
MultiLanguages
.
init
(
this
)
if
((
lastTimeResume
-
lastTimePause
)
>
3500
)
{
MultiLanguages
.
setOnLanguageListener
(
object
:
OnLanguageListener
{
return
true
override
fun
onAppLocaleChange
(
oldLocale
:
Locale
,
newLocale
:
Locale
)
{
}
appLanguageSp
=
newLocale
.
language
return
false
appLanguageCountrySp
=
newLocale
.
country
Log
.
i
(
"MultiLanguages"
,
"监听到应用切换了语种,旧语种:$oldLocale,新语种:$newLocale"
)
}
override
fun
onSystemLocaleChange
(
oldLocale
:
Locale
,
newLocale
:
Locale
)
{
Log
.
i
(
"MultiLanguages"
,
"监听到系统切换了语种,旧语种:"
+
oldLocale
+
",新语种:"
+
newLocale
+
",是否跟随系统:"
+
MultiLanguages
.
isSystemLanguage
(
this
@MyApplication
)
)
}
})
}
}
private
fun
initLifeListener
()
{
// 生命周期监听注册
private
fun
registerLifecycle
()
{
registerActivityLifecycleCallbacks
(
object
:
ActivityLifecycleCallbacks
{
registerActivityLifecycleCallbacks
(
object
:
ActivityLifecycleCallbacks
{
private
var
count
=
0
private
var
count
=
0
override
fun
onActivityCreated
(
activity
:
Activity
,
savedInstanceState
:
Bundle
?)
{
override
fun
onActivityCreated
(
activity
:
Activity
,
savedInstanceState
:
Bundle
?)
{
ActivityManagerUtils
.
getInstance
().
addActivity
(
activity
)
ActivityManagerUtils
.
getInstance
().
addActivity
(
activity
)
}
}
override
fun
onActivityStarted
(
activity
:
Activity
)
{
override
fun
onActivityStarted
(
activity
:
Activity
)
{
count
++
count
++
lastTimeResume
=
System
.
currentTimeMillis
()
lastTimeResume
=
System
.
currentTimeMillis
()
if
(
count
==
1
&&
isHotLaunch
())
{
if
(
count
==
1
&&
isHotLaunch
())
{
val
topActivity
=
ActivityManagerUtils
.
getInstance
().
topActivity
val
topActivity
:
Activity
?
=
ActivityManagerUtils
.
getInstance
().
topActivity
val
flag
=
topActivity
==
null
||
noLoadingActivities
.
all
{
!
topActivity
.
localClassName
.
contains
(
it
,
true
)
}
LogEx
.
logDebug
(
TAG
,
"flag=$flag activity:"
+
activity
.
localClassName
+
" topActivity=${topActivity?.localClassName}"
)
val
flag
=
if
(
topActivity
==
null
)
{
true
}
else
{
noLoadingActivities
.
all
{
!
topActivity
.
localClassName
.
contains
(
it
,
true
)
}
}
LogEx
.
logDebug
(
TAG
,
"flag=$flag"
+
" activity:"
+
activity
.
localClassName
+
" topActivity=${topActivity?.localClassName}"
)
if
(
flag
)
{
if
(
flag
)
{
topActivity
?.
startActivity
(
topActivity
?.
startActivity
(
Intent
(
topActivity
,
SplashActivity
::
class
.
java
).
apply
{
Intent
(
putExtra
(
"isHotLaunch"
,
true
)
topActivity
,
SplashActivity
::
class
.
java
putExtra
(
"type"
,
-
1
)
).
apply
{
})
putExtra
(
"isHotLaunch"
,
true
)
putExtra
(
"type"
,
-
1
)
})
}
}
}
}
lastTimeResume
=
0
lastTimeResume
=
0
}
}
override
fun
onActivityResumed
(
activity
:
Activity
)
{
PAUSED_VALUE
=
1
}
override
fun
onActivityResumed
(
activity
:
Activity
)
{
PAUSED_VALUE
=
1
}
override
fun
onActivityPaused
(
activity
:
Activity
)
{
override
fun
onActivityPaused
(
activity
:
Activity
)
{
PAUSED_VALUE
=
2
PAUSED_VALUE
=
2
lastTimePause
=
System
.
currentTimeMillis
()
lastTimePause
=
System
.
currentTimeMillis
()
}
}
override
fun
onActivityStopped
(
activity
:
Activity
)
{
count
--
}
override
fun
onActivityStopped
(
activity
:
Activity
)
{
count
--
}
override
fun
onActivitySaveInstanceState
(
activity
:
Activity
,
outState
:
Bundle
)
{}
override
fun
onActivitySaveInstanceState
(
activity
:
Activity
,
outState
:
Bundle
)
{}
override
fun
onActivityDestroyed
(
activity
:
Activity
)
{
override
fun
onActivityDestroyed
(
activity
:
Activity
)
{
ActivityManagerUtils
.
getInstance
().
removeActivity
(
activity
)
ActivityManagerUtils
.
getInstance
().
removeActivity
(
activity
)
}
}
})
})
}
}
// 热启动判断
private
fun
isHotLaunch
():
Boolean
=
(
lastTimeResume
-
lastTimePause
)
>
3500
override
fun
attachBaseContext
(
base
:
Context
?)
{
super
.
attachBaseContext
(
MultiLanguages
.
attach
(
base
))
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment