Skip to content

Commit e42287a

Browse files
author
geyifeng
committed
优化代码
1 parent f4cd64d commit e42287a

File tree

9 files changed

+260
-202
lines changed

9 files changed

+260
-202
lines changed

cactus/src/main/java/com/gyf/cactus/Cactus.kt

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,14 @@ import android.app.PendingIntent
66
import android.content.Context
77
import android.graphics.Bitmap
88
import android.os.Build
9-
import android.os.Process
109
import android.widget.RemoteViews
1110
import com.gyf.cactus.callback.CactusBackgroundCallback
1211
import com.gyf.cactus.callback.CactusCallback
1312
import com.gyf.cactus.entity.CactusConfig
1413
import com.gyf.cactus.entity.Constant
1514
import com.gyf.cactus.entity.DefaultConfig
1615
import com.gyf.cactus.entity.NotificationConfig
17-
import com.gyf.cactus.ext.isServiceRunning
18-
import com.gyf.cactus.ext.register
19-
import com.gyf.cactus.ext.restart
20-
import com.gyf.cactus.ext.unregister
16+
import com.gyf.cactus.ext.*
2117

2218
/**
2319
* Cactus保活方案,Cactus有两种形式处理回调事件,
@@ -47,30 +43,28 @@ class Cactus private constructor() {
4743
* 运行时回调广播ACTION
4844
*/
4945
@JvmField
50-
val CACTUS_WORK = "work".field
46+
val CACTUS_WORK = "work".fieldByPid
5147
/**
5248
* 停止时回调广播ACTION
5349
*/
5450
@JvmField
55-
val CACTUS_STOP = "stop".field
51+
val CACTUS_STOP = "stop".fieldByPid
5652
/**
5753
* 后台回调广播ACTION
5854
*/
5955
@JvmField
60-
val CACTUS_BACKGROUND = "background".field
56+
val CACTUS_BACKGROUND = "background".fieldByPid
6157
/**
6258
* 前台后调广播ACTION
6359
*/
6460
@JvmField
65-
val CACTUS_FOREGROUND = "foreground".field
61+
val CACTUS_FOREGROUND = "foreground".fieldByPid
6662
/**
6763
* key,通过广播形式获取启动次数
6864
*/
6965
const val CACTUS_TIMES = "times"
7066
@JvmStatic
7167
val instance by lazy { Cactus() }
72-
73-
private val String.field get() = "${Constant.CACTUS_PACKAGE}.${this}.${Process.myPid()}"
7468
}
7569

7670
/**
@@ -375,5 +369,5 @@ class Cactus private constructor() {
375369
/**
376370
* 是否在运行
377371
*/
378-
fun isRunning(context: Context) = context.isServiceRunning
372+
fun isRunning(context: Context) = context.isCactusRunning
379373
}

cactus/src/main/java/com/gyf/cactus/callback/AppBackgroundCallback.kt

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ class AppBackgroundCallback @JvmOverloads constructor(
3838
* 是否是第一次发送前后台广播
3939
*/
4040
private var mIsFirst = true
41+
/**
42+
* 是否使用监听回调
43+
*/
44+
private var mUseCallback = true
4145

4246
companion object {
4347
private const val FIRST_TIME = 1000L
@@ -83,23 +87,29 @@ class AppBackgroundCallback @JvmOverloads constructor(
8387
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
8488
}
8589

90+
internal fun useCallback(useCallback: Boolean) {
91+
mUseCallback = useCallback
92+
}
93+
8694
/**
8795
* 处理广播
8896
*/
8997
private fun post() {
9098
(mContext?.get() ?: context)?.apply {
91-
if (mFrontActivityCount == 0) {
92-
mIsSend = false
93-
sMainHandler.postDelayed {
94-
sendBroadcast(Intent().setAction(Cactus.CACTUS_BACKGROUND))
95-
block?.let { it(true) }
96-
}
97-
} else {
98-
if (!mIsSend) {
99-
mIsSend = true
99+
if (mUseCallback) {
100+
if (mFrontActivityCount == 0) {
101+
mIsSend = false
100102
sMainHandler.postDelayed {
101-
sendBroadcast(Intent().setAction(Cactus.CACTUS_FOREGROUND))
102-
block?.let { it(false) }
103+
sendBroadcast(Intent().setAction(Cactus.CACTUS_BACKGROUND))
104+
block?.let { it(true) }
105+
}
106+
} else {
107+
if (!mIsSend) {
108+
mIsSend = true
109+
sMainHandler.postDelayed {
110+
sendBroadcast(Intent().setAction(Cactus.CACTUS_FOREGROUND))
111+
block?.let { it(false) }
112+
}
103113
}
104114
}
105115
}

cactus/src/main/java/com/gyf/cactus/entity/Constant.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ internal object Constant {
3232
* 服务ID key
3333
*/
3434
internal const val CACTUS_SERVICE_ID = "serviceId"
35+
/**
36+
* JobID key
37+
*/
38+
internal const val CACTUS_JOB_ID = "jobId"
39+
/**
40+
* 进程名字
41+
*/
42+
internal const val CACTUS_EMOTE_SERVICE = "cactusRemoteService"
3543
/**
3644
* 回调集合
3745
*/

0 commit comments

Comments
 (0)