fix: crash while opening terminal (#658)

Fixes #639
This commit is contained in:
lollipopkit🏳️‍⚧️
2024-12-14 21:06:37 +08:00
committed by GitHub
parent 831efa833b
commit 2f6db2961f
12 changed files with 125 additions and 85 deletions

View File

@@ -4,41 +4,58 @@ import android.app.*
import android.content.Intent
import android.os.Build
import android.os.IBinder
import android.util.Log
class ForegroundService : Service() {
private val chanId = "ForegroundServiceChannel"
override fun onCreate() {
super.onCreate()
Log.d("ForegroundService", "Service onCreate")
createNotificationChannel()
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
when (intent?.action) {
if (intent == null) {
Log.w("ForegroundService", "onStartCommand called with null intent")
stopForegroundService()
return START_NOT_STICKY
}
val action = intent.action
Log.d("ForegroundService", "onStartCommand action=$action")
return when (action) {
"ACTION_STOP_FOREGROUND" -> {
stopForegroundService()
return START_NOT_STICKY
START_NOT_STICKY
}
else -> {
val notification = createNotification()
startForeground(1, notification)
return START_STICKY
START_STICKY
}
}
}
override fun onBind(intent: Intent): IBinder? {
override fun onBind(intent: Intent?): IBinder? {
return null
}
private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val manager = getSystemService(NotificationManager::class.java)
if (manager == null) {
Log.e("ForegroundService", "Failed to get NotificationManager")
return
}
val serviceChannel = NotificationChannel(
chanId,
chanId,
"ForegroundServiceChannel",
NotificationManager.IMPORTANCE_DEFAULT
)
val manager = getSystemService(NotificationManager::class.java)
).apply {
description = "For foreground service"
}
manager.createNotificationChannel(serviceChannel)
}
}
@@ -62,27 +79,33 @@ class ForegroundService : Service() {
PendingIntent.FLAG_IMMUTABLE
)
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val builder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Notification.Builder(this, chanId)
.setContentTitle("Server Box")
.setContentText("Open the app")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent)
.addAction(android.R.drawable.ic_delete, "Stop", deletePendingIntent)
.build()
} else {
Notification.Builder(this)
.setContentTitle("Server Box")
.setContentText("Open the app")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent)
.addAction(android.R.drawable.ic_delete, "Stop", deletePendingIntent)
.build()
}
return builder
.setContentTitle("Server Box")
.setContentText("Running in background")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent)
.addAction(android.R.drawable.ic_delete, "Stop", deletePendingIntent)
.build()
}
fun stopForegroundService() {
stopForeground(true)
private fun stopForegroundService() {
try {
stopForeground(true)
} catch (e: Exception) {
Log.e("ForegroundService", "Error stopping foreground: ${e.message}")
}
stopSelf()
Log.d("ForegroundService", "ForegroundService stopped")
}
override fun onDestroy() {
super.onDestroy()
Log.d("ForegroundService", "Service onDestroy")
}
}

View File

@@ -56,3 +56,4 @@ class MainActivity: FlutterFragmentActivity() {
}
}
}