Skip to content

Commit

Permalink
Don't use Context#startForegroundService() because we may get killed
Browse files Browse the repository at this point in the history
If the foreground service doesn't have anything to do and terminates quickly, the system will kill us, even though the service had called startForeground(). To prevent this, we don't promise that our service will be a foreground service. We can still be a foreground service, but escape the punishment if we are too quick.
  • Loading branch information
grote committed Aug 22, 2024
1 parent 639947b commit 0548b98
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ internal class AppDataRestoreManager(
RestoreBackupResult(context.getString(R.string.restore_set_error))
)
} else {
context.startForegroundService(foregroundServiceIntent)
// don't use startForeground(), because we may stop it sooner than the system likes
context.startService(foregroundServiceIntent)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ internal class ApkRestore(
val i = Intent(context, RestoreService::class.java)
val autoRestore = backupStateManager.isAutoRestoreEnabled
try {
context.startForegroundService(i)
// don't use startForeground(), because we may stop it sooner than the system likes
context.startService(i)
// disable auto-restore before installing apps, if it was enabled before
if (autoRestore) backupManager.setAutoRestore(false)
reInstallApps(backup, packages.asIterable().reversed())
Expand Down

0 comments on commit 0548b98

Please sign in to comment.