Passing/requiring Context
is very common in Android.
Usually that means you have to pass reference to current activity. That will be this
inside any activity, this.Context
inside fragment.
If you want to use this code inside some service that has no clue about UI/activities, common suggestion is to pass context to the service initialization and then store a reference to this context. Be careful as this might lead to memory leaks (you will be storing a reference to an activity and will be keeping it alive).
Alternatively, you can use so called current context: Android.App.Application.Context
.
In your case, if this is a part of Activity code, you have to change it to:
public void TestGPS()
{
var locationManager = this.GetSystemService(Context.LocationService) as LocationManager;
locationManager.AddTestProvider("Test", false, false, false, false, false, false, false, Power.Low, Android.Hardware.SensorStatus.AccuracyHigh);
locationManager.SetTestProviderEnabled("Test", true);
}
If this is a part of a service (non-UI), then:
public void TestGPS()
{
var context = Android.App.Application.Context;
var locationManager = context.GetSystemService(Context.LocationService) as LocationManager;
locationManager.AddTestProvider("Test", false, false, false, false, false, false, false, Power.Low, Android.Hardware.SensorStatus.AccuracyHigh);
locationManager.SetTestProviderEnabled("Test", true);
}
Some good articles on the topic:
Xamarin: Android Activities, Context, Intents and Views
What is Context on Android