Search code examples
androidxamarinandroid-activityxamarin.androidandroid-service

Activity not required for Android app running in the background


The error occurred within Visual studio when running an Xamarin Android app that doesn't require Activity and runs in background.

No Launchable Activity: This project does not contain any activities marked MainLauncher. It has been deployed to the device, but no activity will be launched.

To mark an activity as launchable, add the [Activity] attribute to it with MainLauncher = true:

[Activity (MainLauncher = true)] public class MyActivity : Activity

Below is my code

[Application(Label = "@string/app_name")]
    public class Application : Android.App.Application
    {
        public Application(IntPtr javaReference, JniHandleOwnership transfer)
           : base(javaReference, transfer)
        {
        }

        public override void OnCreate()
        {
            base.OnCreate();

            Xamarin.Essentials.Platform.Init(this);      
            Intent oaServiceIntent = new Intent(this, typeof(MainApplicationService));
            StartService(oaServiceIntent);
        }
}

Do I have to create an Activity even it is not required?


Solution

  • In order for the user to launch your app, you must have at least one Activity and it must have an entry in the manifest with ACTION=MAIN and CATEGORY=LAUNCHER so that it will show up on the HOME screen.

    If the user cannot manually launch your app then it cannot be started and no background components (Service, BroadcastReceiver, etc.) will run.

    The Application instance is only created when Android creates an OS process to host your app and this will only happen if the user has manually launched your app at least once after installation.