Search code examples
c#androidxamarinmvvmcross

How to access GetSystemService(AudioService) in the UI layer


I am developing android App in Xamarin. I want to create an interface and its implementing class in the UI layer. As you see in the below code, I am getting an error when ever, I use

this.GetSystemService(AudioService);

it is never recognized. please have a look at the imports below. please let me know how to get it working.

code:

 public class ImplClass :   
 InterfaceFile
 {
    public bool IsAllowed(Context ctx)
    {
        AudioManager audioMgr = 
 (AudioManager)this.GetSystemService(AudioService);
     }
    }

import:

using System;
using System.Runtime.Remoting.Contexts;
using Android.Media;
using MvvmCross.Platform;
using Android.Content.PM;

Solution

  • You can use the "Application Context".

    Example:

    public class ImplClass : InterfaceFile
    {
        public bool IsAllowed(Context ctx)
        {
            AudioManager audioMgr = (AudioManager)Application.Context.GetSystemService(AudioService);
        }
    }