Search code examples
c#androidxamarinalarmmanager

How to get the AlarmManager system service in Xamarin.Android


I'm trying to set up an extremely simple Android application that will fire a toast message on a schedule (even if the application is no longer running). After doing some research it looks like the AlarmManager class can handle this functionality. The issue I'm having is that I simply can't get an instance of the AlarmManager class. I've looked at probably 30 different examples and they all get an instance of AlarmManager the same way. However, when I try to do the same I get the error "AlarmManager is a namespace but is used like a type".

I've tried creating an instance of the class about a dozen different ways and nothing seems to work. All the resources I've found show don't show any alternatives to what I've tried.

AlarmManager alarmManager = (AlarmManager) Context.GetSystemService (Context.AlarmService);

I expect to get an instance of AlarmManager but instead I get the error that it is a namespace being used like a type.


Solution

  • Not sure what your "context" is, but I am assuming you are trying to access it via a static property since you are also using Context.AlarmService in the same line of code and GetSystemService is available via an context instance.

    You can use an Application or Activity context to access GetSystemService:

    Example:

    var alarmManager = (AlarmManager) Android.App.Application.Context.GetSystemService (Context.AlarmService);