@@ -1458,14 +1458,14 @@ func (_ ViewEvent) ImplementsEvent() {}
14581458var foregroundService struct {
14591459 intent C.jobject
14601460 mu sync.Mutex
1461- count int
1461+ stop map [ * int ] bool
14621462}
14631463
14641464// Start starts the foreground service
14651465func Start (title , text string ) (stop func (), err error ) {
14661466 foregroundService .mu .Lock ()
14671467 defer foregroundService .mu .Unlock ()
1468- if foregroundService .count == 0 {
1468+ if len ( foregroundService .stop ) == 0 {
14691469 runInJVM (javaVM (), func (env * C.JNIEnv ) {
14701470 foregroundService .intent , err = callStaticObjectMethod (env , android .gioCls ,
14711471 android .startForegroundService ,
@@ -1476,23 +1476,31 @@ func Start(title, text string) (stop func(), err error) {
14761476 if err == nil {
14771477 // get a reference across JNI sessions to the returned intent
14781478 foregroundService .intent = C .jni_NewGlobalRef (env , foregroundService .intent )
1479+
1480+ } else {
1481+ panic (err )
14791482 }
14801483 })
14811484 }
14821485 if err != nil {
14831486 return nil , err
14841487 }
1485- foregroundService .count ++
1488+ ref := new (int )
1489+ foregroundService .stop [ref ] = true
14861490 return func () {
14871491 foregroundService .mu .Lock ()
14881492 defer foregroundService .mu .Unlock ()
1489- if foregroundService .count == 1 {
1493+ delete (foregroundService .stop , ref )
1494+ if len (foregroundService .stop ) == 0 {
14901495 runInJVM (javaVM (), func (env * C.JNIEnv ) {
14911496 defer C .jni_DeleteGlobalRef (env , foregroundService .intent )
14921497 callVoidMethod (env , android .appCtx , android .stopService , jvalue (foregroundService .intent ))
14931498 })
14941499 }
1495- foregroundService .count --
1496- }, err
1500+ }, nil
1501+
1502+ }
14971503
1504+ func init () {
1505+ foregroundService .stop = make (map [* int ]bool )
14981506}
0 commit comments