teamclouday / AndroidMic

Use your Android phone as a mic to Windows PC
MIT License
113 stars 9 forks source link

Handle lifecycle of Foreground service #15

Closed wiiznokes closed 1 year ago

wiiznokes commented 1 year ago

The pull request fix this issue #14. After some test, I saw that the service run forever, so I added a single notification with an action to stop the service. BroadcastReceiver in AndroidMicApp will be triggered and will unbound the service. Unbound the service is the only option to stop it. There is on corner case, if we stop the service when the app is visible, then the UI will not be updated.

onServiceDisconnected

is never trigger so we can't use that. So we have the BroadcastReceiver but we can't just use startActivity I think because we don't always want to open the app. We need a flag to notify the activity only if it has been started.

Anyway, it's a detail because the app will recover itself after one click.

teamclouday commented 1 year ago

Hey thanks for the update!

Lifecycle by notification is good approach πŸ‘. I think the main purpose of keeping service alive is to keep sending audio data in the background.\ So how about we start this notification that keeps foreground service alive only after a connection is established?\ Otherwise it does not make much sense to keep it alive if no connection.

So the idea is to start a foreground notification to keep service alive whenever connected a pc. If connection is closed, remove the foreground notification and let service die with activity.

Please let me know your opinion!πŸ™‚

wiiznokes commented 1 year ago

I think you right, the previous approach was to much complicated for nothing, and was using a Broadcast listener so not optimized too.

So now, we make sure to use startService(intent) so the service will stop if we explicitly call stopSelf.

We start and bind the service onStart of MainActivity We unbind the service onStop of MainActivity

Inside onUnbind:

 if(!states.isAudioStarted.get() && !states.isStreamStarted.get()) 
         stopService()

And the notification is only show when the record audio is started.

and let service die with activity

The service have to be unBind to die, so I think in the previous version, the service was running for everπŸ˜….

teamclouday commented 1 year ago

We start and bind the service onStart of MainActivity We unbind the service onStop of MainActivity

I like this new approach!

And the notification is only show when the record audio is started.

Make sense to me. If stream started but no audio, it's not necessary to keep the service.

The service have to be unBind to die

Aha, that's rightπŸ˜‚

And thank you for helping to improve this! Previously I used a compromised solution that keeps app screen always on. I think this solution is far better!

wiiznokes commented 1 year ago

Make sense to me. If stream started but no audio, it's not necessary to keep the service.

It's not that but the notification is used because we need a notification to startForeground() and we need that to use mic in foreground.

And thank you for helping to improve this!

You're welcome, I love doing that and thank to you for the app. I learned a lot from you!

Previously I used a compromised solution that keeps app screen always on. I think this solution is far better!

I think so too. I kinda broke your app with my new UI so I have to fix that πŸ˜‚.

teamclouday commented 1 year ago

Merged πŸ‘