why can not I add service in AndroidManifest.xml ?

mc 5,366 Reputation points
2024-01-04T02:19:53.67+00:00

if I create service in AndroidManifest.xml it is not work.

<service android:name=".MirrorServiceO" android:foregroundServiceType="mediaProjection" android:exported="true" android:enabled="true"></service>

you can see I add the foregroundServiceType but it is not work I have to add the attribute ForegroundServiceType = Android.Content.PM.ForegroundService.TypeMediaProjection in the Service Class.

[Service(ForegroundServiceType = Android.Content.PM.ForegroundService.TypeMediaProjection)]
public class MyServiceO : Service
{
}

why?

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
4,117 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2024-01-05T06:28:02.61+00:00

    Hello,

    In the case of the AndroidManifest.xml file in MAUI, what really works is merging the generated files while the program is running.

    Every .NET Multi-platform App UI (.NET MAUI) app on Android has an AndroidManifest.xml file, located in the Platforms\Android folder, that describes essential information about your app to build tools, the Android operating system, and Google Play. The manifest file for your .NET MAUI Android app is generated as part of the .NET MAUI build process on Android. This build process takes the XML in the Platforms\Android\AndroidManifest.xml file, and merges it with any XML that's generated from specific attributes on your classes. The resulting manifest file can be found in the obj folder. For example, it can be found at obj\Debug\net8.0-android\AndroidManifest.xml for debug builds on .NET 8. Quated from Android app manifest

    Please check whether you set the service attribute in the XML file generated by the merge.

    As a supplement, in MAUI, it is generally recommended that you do not set the Service and Activity properties in the manifest file.

    Using ServiceAttributes to set up in C# code is more recommended.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.