Skip to content

Commit

Permalink
Enhance service lifecycle and exit strategy (#2039)
Browse files Browse the repository at this point in the history
* Enhance service lifecycle and exit strategy

- Updated `AndroidManifest.xml` to improve `CommunityToolkit.Maui.Media.Services` behavior. Added `android:stopWithTask=true` and `android:foregroundServiceType=mediaPlayback` attributes to ensure the service stops with the task and properly manages media playback in the foreground.
- Added `OnDestroy()` method in `MediaControlsService.android.cs` for a more aggressive shutdown approach. This includes logging service destruction, stopping and removing the service from the foreground, finishing and removing the task, exiting the application, and ensuring base class cleanup with `base.OnDestroy()`. This aims at a cleaner exit and resource management when the service is destroyed.

* Fix Merge error

---------

Co-authored-by: Brandon Minnick <[email protected]>
Co-authored-by: Gerald Versluis <[email protected]>
  • Loading branch information
3 people authored Aug 16, 2024
1 parent 816e8a7 commit 440e4e2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:enableOnBackInvokedCallback="true"
android:supportsRtl="true">
<service android:name="communityToolkit.maui.media.services" android:exported="false" android:enabled="true"
<service android:name="communityToolkit.maui.media.services" android:stopWithTask="true" android:exported="false" android:enabled="true"
android:foregroundServiceType="mediaPlayback">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,15 @@ void OnSetIntents()
notification?.AddAction(actionNext);
}

public override void OnDestroy()
{
Platform.CurrentActivity?.StopService(new Intent(Platform.AppContext, typeof(MediaControlsService)));
System.Diagnostics.Trace.TraceInformation("MediaControlsService destroyed.");
Platform.CurrentActivity?.FinishAndRemoveTask();
System.Environment.Exit(0);
System.Diagnostics.Trace.TraceInformation("Application exiting.");
base.OnDestroy();
}
static void BroadcastUpdate(string receiver, string action)
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.Tiramisu)
Expand Down

0 comments on commit 440e4e2

Please sign in to comment.