Repository navigation
Atualização do foreground service #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6316689
e698150
2b8bca0
42a88ad
e55150b
d628c36
eea409e
b3d3d30
65868ab
33aa919
d487d59
82cd8a2
0f8fdd8
5ac0c73
dce8112
afc8c9e
bdf3cb0
e335c5b
25a2b7b
7909dba
1e7ec26
f773bf2
a8c0b59
1250c19
7c4a1f1
c7a6fc0
2760bc5
3e40a51
9a959f7
f2adde7
d163b53
e6d5bcb
f2acda8
c62d5a4
5aafaf1
9b26bc1
305c25d
1a5ee44
04ba95d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,7 +62,10 @@ class ForegroundServiceManager { | |
| ForegroundTaskOptions.updateData(context, argsMap) | ||
| ForegroundTaskData.updateData(context, argsMap) | ||
| NotificationContent.updateData(context, argsMap) | ||
| ContextCompat.startForegroundService(context, nIntent) | ||
| // Use startService instead of startForegroundService because the service | ||
| // is already running in the foreground and we only need to deliver the | ||
| // update command. This avoids the startForeground() contract requirement. | ||
|
Comment on lines
+65
to
+67
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Esse comentário é seu ou da atualização? pq voltou?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. É da atualização, e conferi que o contextCompat não tem startService, ele tem que ser usado para o startForegroundService. O startService existe desde a API nível 1 e nisso não precisa de nenhuma compatibilidade com outros Androids, inclusive o ContextCompat para Androids abaixo do SDK 26 redireciona pro startService()
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pelo que entendi da alteração do repositório original, em um update chamar o startService() permite atualizar o serviço evitando que o startForeground() tenha que ser chamado em 5 segundos |
||
| context.startService(nIntent) | ||
| } | ||
|
|
||
| /** Stop the foreground service. */ | ||
|
|
@@ -78,7 +81,10 @@ class ForegroundServiceManager { | |
| ForegroundTaskOptions.clearData(context) | ||
| ForegroundTaskData.clearData(context) | ||
| NotificationContent.clearData(context) | ||
| ContextCompat.startForegroundService(context, nIntent) | ||
| // Use startService instead of startForegroundService because the service | ||
| // is already running in the foreground and we only need to deliver the | ||
| // stop command. This avoids the startForeground() contract requirement. | ||
| context.startService(nIntent) | ||
| } | ||
|
|
||
| /** Send data to TaskHandler. */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -99,4 +99,4 @@ class RestartReceiver : BroadcastReceiver() { | |
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package com.pravera.flutter_foreground_task.utils | ||
|
|
||
| import android.app.* | ||
| import android.os.* | ||
|
|
||
| object TrackVisibilityUtils : Application.ActivityLifecycleCallbacks { | ||
| private var installed = false | ||
| private val resumed = HashSet<Int>() | ||
| private var onAllGone: (() -> Unit)? = null | ||
|
|
||
| fun install(application: Application, callback: () -> Unit) { | ||
| onAllGone = callback | ||
| if (!installed) { | ||
| installed = true | ||
| application.registerActivityLifecycleCallbacks(this) | ||
| } | ||
| } | ||
|
|
||
| override fun onActivityResumed(activity: Activity) { | ||
| resumed.add(System.identityHashCode(activity)) | ||
| } | ||
|
|
||
| override fun onActivityPaused(activity: Activity) { | ||
| resumed.remove(System.identityHashCode(activity)) | ||
| if (resumed.isEmpty()) { | ||
| onAllGone?.invoke() | ||
| } | ||
| } | ||
|
|
||
| override fun onActivityStarted(activity: Activity) {} | ||
| override fun onActivityStopped(activity: Activity) {} | ||
| override fun onActivityCreated(a: Activity, b: Bundle?) {} | ||
| override fun onActivitySaveInstanceState(a: Activity, b: Bundle) {} | ||
| override fun onActivityDestroyed(a: Activity) {} | ||
| } |

Uh oh!
There was an error while loading. Please reload this page.