Skip to content

Commit

Permalink
avoid ANR with startForegroundService
Browse files Browse the repository at this point in the history
  • Loading branch information
tsynik committed May 11, 2024
1 parent 5ef8eeb commit 51dd4d7
Showing 1 changed file with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,17 @@ class TorrService : Service() {
return START_NOT_STICKY
}

private fun startServer(nfg: Boolean = false) {
private fun startServer(needForeground: Boolean = false) {
serviceScope.launch {
if (BuildConfig.DEBUG) Log.d("TorrService", "startServer(nfg:$nfg)")
if (BuildConfig.DEBUG) Log.d("TorrService", "startServer(needForeground:$needForeground)")

if (isLocal() && isAccessibilityOn() && !Accessibility.isEnabledService(App.context)) {
if (BuildConfig.DEBUG) Log.d("TorrService", "Try to enable AssessibilityService")
Accessibility.enableService(App.context, true)
}

if (serverFile.exists() && isLocal() && Api.echo().isEmpty()) {
if (nfg) { // fix start local server on boot
if (needForeground) { // fix start local server on boot
val builder = NotificationCompat.Builder(this@TorrService, NotificationTS().channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(getString(R.string.app_name))
Expand Down Expand Up @@ -131,7 +131,11 @@ class TorrService : Service() {
if (isLocal()) { // avoid ANR on remote
intent.putExtra(NEED_FOREGROUND_KEY, true)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent)
try {
context.startForegroundService(intent)
} catch (_: Exception) {
// need then call Service.startForeground() or ANR
}
}
}
}
Expand Down

0 comments on commit 51dd4d7

Please sign in to comment.