0

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);
Abhinav Gupta
  • 2,225
  • 1
  • 14
  • 30
user2148707
  • 93
  • 12
  • pls indicate if this solved the problem... – X-Black... Apr 17 '19 at 11:01
  • The problem I see is, I am not able to select my app as the mock location provider (In Android setting). I could see my app name in the list of Mock location providers though. But there are some other applications as well in the list and they are selectable as well. – user2148707 Apr 17 '19 at 12:18
  • scroll through this...https://forum.xda-developers.com/nexus-6/help/select-mock-location-app-stuck-t3423268 – X-Black... Apr 18 '19 at 13:38

1 Answers1

1

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

X-Black...
  • 1,376
  • 2
  • 20
  • 28