mirror of
https://github.com/greenart7c3/Amber.git
synced 2026-10-06 11:28:22 +00:00
Fix crash when starting service in background
This commit is contained in:
@@ -5,6 +5,7 @@ import android.app.Application
|
||||
import android.app.PendingIntent
|
||||
import android.content.Intent
|
||||
import android.net.NetworkCapabilities
|
||||
import android.os.Build
|
||||
import android.util.Log
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.work.Data
|
||||
@@ -87,25 +88,29 @@ class Amber : Application() {
|
||||
return changedNetwork
|
||||
}
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
|
||||
private fun startCleanLogsAlarm() {
|
||||
val alarmManager = this.getSystemService(ALARM_SERVICE) as AlarmManager
|
||||
val intent = Intent(this, BootReceiver::class.java)
|
||||
intent.action = BootReceiver.CLEAR_LOGS_ACTION
|
||||
val pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT + PendingIntent.FLAG_MUTABLE)
|
||||
|
||||
// Set up the alarm to trigger every 24 hours
|
||||
val interval: Long = 24 * 60 * 60 * 1000 // 24 hours in milliseconds
|
||||
val triggerAtMillis = System.currentTimeMillis()
|
||||
|
||||
// Set a repeating alarm
|
||||
alarmManager.setRepeating(
|
||||
AlarmManager.RTC_WAKEUP,
|
||||
triggerAtMillis,
|
||||
interval,
|
||||
pendingIntent,
|
||||
)
|
||||
}
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
|
||||
Log.d("NostrSigner", "onCreate Amber")
|
||||
|
||||
startCleanLogsAlarm()
|
||||
|
||||
HttpClientManager.setDefaultUserAgent("Amber/${BuildConfig.VERSION_NAME}")
|
||||
_instance = this
|
||||
@@ -139,13 +144,28 @@ class Amber : Application() {
|
||||
settings = LocalPreferences.loadSettingsFromEncryptedStorage()
|
||||
}
|
||||
|
||||
startService()
|
||||
}
|
||||
|
||||
fun startService() {
|
||||
try {
|
||||
this.startForegroundService(
|
||||
Intent(
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
||||
val operation = PendingIntent.getForegroundService(
|
||||
this,
|
||||
ConnectivityService::class.java,
|
||||
),
|
||||
)
|
||||
10,
|
||||
Intent(this, ConnectivityService::class.java),
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE,
|
||||
)
|
||||
val alarmManager = this.getSystemService(ALARM_SERVICE) as AlarmManager
|
||||
alarmManager.set(AlarmManager.RTC_WAKEUP, 1000, operation)
|
||||
} else {
|
||||
this.startForegroundService(
|
||||
Intent(
|
||||
this,
|
||||
ConnectivityService::class.java,
|
||||
),
|
||||
)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e("NostrSigner", "Failed to start ConnectivityService", e)
|
||||
}
|
||||
|
||||
@@ -22,45 +22,19 @@ class BootReceiver : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
@Suppress("KotlinConstantConditions")
|
||||
if (BuildConfig.FLAVOR == "offline") return
|
||||
Log.d("BootReceiver", "Received intent: ${intent.action}")
|
||||
|
||||
when (intent.action) {
|
||||
Intent.ACTION_PACKAGE_REPLACED -> {
|
||||
if (intent.dataString?.contains("com.greenart7c3.nostrsigner") == true && Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
|
||||
try {
|
||||
context.startForegroundService(
|
||||
Intent(
|
||||
context,
|
||||
ConnectivityService::class.java,
|
||||
),
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
Log.e("NostrSigner", "Failed to start ConnectivityService", e)
|
||||
}
|
||||
Amber.instance.startService()
|
||||
}
|
||||
}
|
||||
Intent.ACTION_MY_PACKAGE_REPLACED -> {
|
||||
try {
|
||||
context.startForegroundService(
|
||||
Intent(
|
||||
context,
|
||||
ConnectivityService::class.java,
|
||||
),
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
Log.e("NostrSigner", "Failed to start ConnectivityService", e)
|
||||
}
|
||||
Amber.instance.startService()
|
||||
}
|
||||
Intent.ACTION_BOOT_COMPLETED -> {
|
||||
try {
|
||||
context.startForegroundService(
|
||||
Intent(
|
||||
context,
|
||||
ConnectivityService::class.java,
|
||||
),
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
Log.e("NostrSigner", "Failed to start ConnectivityService", e)
|
||||
}
|
||||
Amber.instance.startService()
|
||||
}
|
||||
CLEAR_LOGS_ACTION -> {
|
||||
Amber.instance.applicationIOScope.launch(Dispatchers.IO) {
|
||||
|
||||
@@ -90,7 +90,7 @@ class ConnectivityService : Service() {
|
||||
override fun onCreate() {
|
||||
if (isStarted) return
|
||||
|
||||
Log.d("ConnectivityService", "onCreate")
|
||||
Log.d("ConnectivityService", "onCreate ConnectivityService")
|
||||
|
||||
isStarted = true
|
||||
|
||||
@@ -145,6 +145,7 @@ class ConnectivityService : Service() {
|
||||
@Suppress("KotlinConstantConditions")
|
||||
if (BuildConfig.FLAVOR != "offline") {
|
||||
try {
|
||||
Log.d("ConnectivityService", "unregisterNetworkCallback")
|
||||
val connectivityManager =
|
||||
(getSystemService(ConnectivityManager::class.java) as ConnectivityManager)
|
||||
connectivityManager.unregisterNetworkCallback(networkCallback)
|
||||
|
||||
Reference in New Issue
Block a user