androidmock-location

Android mock location provider sample app is not working


I am trying to build a simple Android mock location provide (Android 7.0). I am getting a Java.Lang.SecurityException on ... not allowed to perform MockLocationException. I have android.permission.ACCESS_MOCK_LOCATION in manifest file. I have also tried the same with Xamarin and got the same error.

Location location = new Location(LocationManager.GpsProvider);

location.Latitude = 20.0;
location.Longitude = 20.0;
location.Accuracy = 0;
location.Time = DateTime.Now.Ticks;
location.ElapsedRealtimeNanos = 100;
location.Speed = 0.0f;
location.Altitude = 1.0;
location.Bearing = 0.0f;

LocationManager locationManager = GetSystemService(LocationService) as 
LocationManager;

locationManager.AddTestProvider(LocationManager.GpsProvider, false, 
       false, false, false, false, false, false, Power.Low, 
Android.Hardware.SensorStatus.AccuracyHigh);
// getting exception on the above line

locationManager.SetTestProviderLocation(LocationManager.GpsProvider, 
           location);
locationManager.SetTestProviderEnabled(LocationManager.GpsProvider, true);

Solution

  • You need to enable Allow mock locations within the Settings - Developer options settings on your device, and add the ACCESS_MOCK_LOCATION permission within your manifest. This will enable you to send mock locations to your app