Skip to content

Fix Android Service not being disabled when set#3104

Merged
TheCodeTraveler merged 6 commits intoCommunityToolkit:mainfrom
ne0rrmatrix:fixService
Feb 18, 2026
Merged

Fix Android Service not being disabled when set#3104
TheCodeTraveler merged 6 commits intoCommunityToolkit:mainfrom
ne0rrmatrix:fixService

Conversation

@ne0rrmatrix
Copy link
Copy Markdown
Member

Android MediaSessionService was not being disabled when setting IsAndroidForegroundServiceEnabled to false. This fixes it and removes setting the default value that was being set erroneously. It should never have been set. This is why the analyzer was created to verify that it was set and cause a build error message otherwise.

Description of Change

Refactored MediaElementOptions to remove the default true value for IsAndroidForegroundServiceEnabled, making it default to false. Removed the UpdateIsAndroidForegroundServiceEnabledWithUseMauiCommunityToolkitMediaElementParameter method and now use SetIsAndroidForegroundServiceEnabled directly. Updated AppBuilderExtensions and unit tests to reflect these changes and ensure correct behavior with the simplified property logic.

Linked Issues

PR Checklist

Additional information

Please test and verify this works for more than just me :)

Refactored MediaElementOptions to remove the default true value for IsAndroidForegroundServiceEnabled, making it default to false. Removed the UpdateIsAndroidForegroundServiceEnabledWithUseMauiCommunityToolkitMediaElementParameter method and now use SetIsAndroidForegroundServiceEnabled directly. Updated AppBuilderExtensions and unit tests to reflect these changes and ensure correct behavior with the simplified property logic.
Copilot AI review requested due to automatic review settings February 18, 2026 13:39
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a critical bug where setting IsAndroidForegroundServiceEnabled to false in MediaElement would still result in the Android Foreground Service being enabled. The root cause was twofold: the property defaulted to true, and the method that updated it used a logical OR assignment (|=) that prevented it from ever being set to false.

Changes:

  • Removed erroneous default value (= true) from IsAndroidForegroundServiceEnabled, allowing it to default to false
  • Removed UpdateIsAndroidForegroundServiceEnabledWithUseMauiCommunityToolkitMediaElementParameter method that used |= operator
  • Changed SetIsAndroidForegroundServiceEnabled visibility from public to internal to prevent external misconfiguration
  • Simplified AppBuilderExtensions to call SetIsAndroidForegroundServiceEnabled directly instead of using the removed method
  • Updated unit tests to reflect the new behavior

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
MediaElementOptions.shared.cs Removed default true initialization and the problematic UpdateIsAndroidForegroundServiceEnabledWithUseMauiCommunityToolkitMediaElementParameter method; changed SetIsAndroidForegroundServiceEnabled to internal
AppBuilderExtensions.shared.cs Simplified to call SetIsAndroidForegroundServiceEnabled directly with the parameter value
MediaElementOptionsTests.cs Updated tests to verify the corrected behavior, removed tests for the deleted method

Comment thread src/CommunityToolkit.Maui.MediaElement/MediaElementOptions.shared.cs Outdated
@ne0rrmatrix ne0rrmatrix requested a review from a team February 18, 2026 18:01
pictos
pictos previously approved these changes Feb 18, 2026
Copilot AI review requested due to automatic review settings February 18, 2026 19:08
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 11 comments.

Comments suppressed due to low confidence (1)

src/CommunityToolkit.Maui.UnitTests/Extensions/AppBuilderExtensionsTests.cs:269

  • Test logic appears incorrect. The test calls SetIsAndroidForegroundServiceEnabled(false) on line 266, but then asserts that IsAndroidForegroundServiceEnabled should be true on line 269. Based on the PR description stating that SetIsAndroidForegroundServiceEnabled should directly set the value, this assertion should be false, or the method call on line 266 should be SetIsAndroidForegroundServiceEnabled(true) if the intention is to verify that the options callback overrides the parameter value.

Comment thread src/CommunityToolkit.Maui.UnitTests/Extensions/AppBuilderExtensionsTests.cs Outdated
Comment thread src/CommunityToolkit.Maui.UnitTests/Extensions/AppBuilderExtensionsTests.cs Outdated
Comment thread src/CommunityToolkit.Maui.UnitTests/Extensions/AppBuilderExtensionsTests.cs Outdated
Comment thread src/CommunityToolkit.Maui.UnitTests/Extensions/AppBuilderExtensionsTests.cs Outdated
@TheCodeTraveler TheCodeTraveler merged commit b4a2e88 into CommunityToolkit:main Feb 18, 2026
8 of 10 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Feb 20, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] isAndroidForegroundServiceEnabled set to false results in java.lang.SecurityException: Permission Denial

4 participants