Skip to content

Commit 440e4e2

Browse files
ne0rrmatrixTheCodeTravelerjfversluis
authored
Enhance service lifecycle and exit strategy (#2039)
* 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]>
1 parent 816e8a7 commit 440e4e2

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

samples/CommunityToolkit.Maui.Sample/Platforms/Android/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
33
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:enableOnBackInvokedCallback="true"
44
android:supportsRtl="true">
5-
<service android:name="communityToolkit.maui.media.services" android:exported="false" android:enabled="true"
5+
<service android:name="communityToolkit.maui.media.services" android:stopWithTask="true" android:exported="false" android:enabled="true"
66
android:foregroundServiceType="mediaPlayback">
77
<intent-filter>
88
<action android:name="android.intent.action.MEDIA_BUTTON"/>

src/CommunityToolkit.Maui.MediaElement/Services/MediaControlsService.android.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,15 @@ void OnSetIntents()
217217
notification?.AddAction(actionNext);
218218
}
219219

220+
public override void OnDestroy()
221+
{
222+
Platform.CurrentActivity?.StopService(new Intent(Platform.AppContext, typeof(MediaControlsService)));
223+
System.Diagnostics.Trace.TraceInformation("MediaControlsService destroyed.");
224+
Platform.CurrentActivity?.FinishAndRemoveTask();
225+
System.Environment.Exit(0);
226+
System.Diagnostics.Trace.TraceInformation("Application exiting.");
227+
base.OnDestroy();
228+
}
220229
static void BroadcastUpdate(string receiver, string action)
221230
{
222231
if (Build.VERSION.SdkInt >= BuildVersionCodes.Tiramisu)

0 commit comments

Comments
 (0)