Android 模拟位置提供程序示例应用无法正常工作

Android mock location provider sample app is not working

我正在尝试构建一个简单的 Android 模拟位置提供 (Android 7.0)。我在...上收到 Java.Lang.SecurityException 不允许执行 MockLocationException。我在清单文件中有 android.permission.ACCESS_MOCK_LOCATION。我也用 Xamarin 尝试过同样的方法,但得到了同样的错误。

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);

您需要在 设置 - 开发者选项设置中启用允许模拟位置您的设备,并在清单中添加 ACCESS_MOCK_LOCATION 权限。这将使您能够将模拟位置发送到您的应用