Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting an android Service from Xamarin forms

In Visual Studio I created a new class library (Xamarin Forms) :-

Xamarin class library

Here I added my service and gave it the most basic implementation possible for a started service (note - not a bound service. Expecting - toasts to be displayed when the application is started / stopped :-

namespace AndroidBatteryService
{
    public class AndroidBatteryService : Service
    {

        public AndroidBatteryService()
        {

        }

        [return: GeneratedEnum]
        public override StartCommandResult OnStartCommand(Intent intent,    [GeneratedEnum] StartCommandFlags flags, int startId)
        {
            Toast.MakeText(this, "The Battery Service was just started",  ToastLength.Long).Show();
            return base.OnStartCommand(intent, flags, startId);
        }

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

        public override void OnDestroy()
        {
            base.OnDestroy();
            Toast.MakeText(this, "The Battery Service has been stopped",     ToastLength.Long).Show();
        }

        public override IBinder OnBind(Intent intent)
        {
            //started service, NOT a binded service - return null...
            return null;
        }
   }
}

In another project I am invoking this service my running the following code :-

public void StartBatteryService()
{
    Intent intent = new Intent(Android.App.Application.Context, typeof(AndroidBatteryService.AndroidBatteryService));
    Android.App.Application.Context.StartService(intent);
}

The code runs (I can step over it), but the OnStartCommand method in the actual service is not run and I am expecting it to. Any ideas??

like image 590
Edward Milne Avatar asked Oct 28 '16 15:10

Edward Milne


People also ask

Is Xamarin being discontinued?

In May 2020, Microsoft announced that Xamarin. Forms, a major component of its mobile app development framework, would be deprecated in November 2021 in favour of a new . Net based product called MAUI - Multiform App User Interface.

Is Xamarin good for Android development?

Xamarin allows you to create flawless experiences using platform-specific UI elements. It's also possible to build cross-platform apps for iOS, Android, or Windows using Xamarin. Forms tool, which converts app UI components into the platform-specific interface elements at runtime. As the use of Xamarin.

Is Xamarin getting deprecated?

Xamarin. Forms will continue to receive service releases through November 2022. Attachments: Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total. Attachments: Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Is Microsoft replacing Xamarin?

NET MAUI (. NET Multi-platform App UI) is now a project type among the five Desktop & Mobile options in Visual Studio 2022 17.3 Preview 1, replacing the Xamarin-based option. . NET MAUI is described by Microsoft as the evolution of mobile-centric Xamarin.


1 Answers

I think the problem is that you need to decorate the service class with:-

[Service (Label = "AndroidBatteryService", Icon = "@drawable/Icon") ]

This causes Xamarin to put the following into the generated AndroidManifest.xml file during the build (you can find this in the \obj\Debug\android directory)

<service android:icon="@drawable/icon" android:label="AndroidBatteryService" android:name="md55...1a.AndroidBatteryService" />

I used a very similar piece of code in my app and it worked fine. (Although you've probably worked this out by now.)

like image 143
JasonB Avatar answered Oct 01 '22 22:10

JasonB