Skip to content

Commit 7b84119

Browse files
committed
Update
1 parent b464db6 commit 7b84119

2 files changed

Lines changed: 16 additions & 5 deletions

File tree

app/src/main/java/com/tool/tree/MainActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ class MainActivity : AppCompatActivity() {
199199
} else if (runnableNode.autoKill) {
200200
startService(Intent(this@MainActivity, WakeLockService::class.java).apply { action = WakeLockService.ACTION_END_WAKELOCK })
201201
finishAffinity()
202-
System.exit(0)
202+
// System.exit(0)
203203
}
204204
}
205205

app/src/main/java/com/tool/tree/WakeLockService.kt

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ class WakeLockService : Service() {
4848
if (!isWakeLockActive) {
4949
wakeLock?.acquire()
5050
isWakeLockActive = true
51-
startForeground(1, buildNotification())
51+
updateNotification()
5252
}
5353
}
5454

5555
private fun disableWakeLock() {
5656
if (isWakeLockActive) {
5757
wakeLock?.release()
5858
isWakeLockActive = false
59-
startForeground(1, buildNotification())
59+
updateNotification()
6060
}
6161
}
6262

@@ -76,11 +76,20 @@ class WakeLockService : Service() {
7676
appTasks.forEach { task ->
7777
task.finishAndRemoveTask()
7878
}
79-
System.exit(0)
79+
// System.exit(0)
80+
}
81+
82+
private fun updateNotification() {
83+
val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
84+
manager.notify(1, buildNotification())
8085
}
8186

8287
override fun onCreate() {
8388
super.onCreate()
89+
90+
if (isServiceRunning) return
91+
isServiceRunning = true
92+
8493
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
8594
createNotificationChannel()
8695
}
@@ -160,12 +169,13 @@ class WakeLockService : Service() {
160169
val flags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
161170
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
162171
} else PendingIntent.FLAG_UPDATE_CURRENT
163-
return PendingIntent.getService(this, 0, intent, flags)
172+
return PendingIntent.getService(this, action.hashCode(), intent, flags)
164173
}
165174

166175
override fun onDestroy() {
167176
wakeLock?.release()
168177
wakeLock = null
178+
isServiceRunning = false
169179
super.onDestroy()
170180
}
171181

@@ -187,6 +197,7 @@ class WakeLockService : Service() {
187197
const val ACTION_DISABLE_WAKELOCK = "com.tool.tree.action.DISABLE_WAKELOCK"
188198
const val ACTION_END_WAKELOCK = "com.tool.tree.action.END_WAKELOCK"
189199
const val ACTION_STOP_SERVICE = "com.tool.tree.action.STOP_SERVICE"
200+
private var isServiceRunning = false
190201

191202
fun startService(context: Context) {
192203
val intent = Intent(context, WakeLockService::class.java)

0 commit comments

Comments
 (0)