Android Question Mock Location With inline Java code

hanyelmehy

Active Member
Licensed User
Longtime User
Any idea how to use this code directly without library with inline java code
B4X:
public class MockLocationProvider {
  String providerName;
  Context ctx;
  public MockLocationProvider(String name, Context ctx) {
    this.providerName = name;
    this.ctx = ctx;
    LocationManager lm = (LocationManager) ctx.getSystemService(
      Context.LOCATION_SERVICE);
    lm.addTestProvider(providerName, false, false, false, false, false,
      true, true, 0, 5);
    lm.setTestProviderEnabled(providerName, true);
  }
  public void pushLocation(double lat, double lon) {
    LocationManager lm = (LocationManager) ctx.getSystemService(
      Context.LOCATION_SERVICE);
    Location mockLocation = new Location(providerName);
    mockLocation.setLatitude(lat);
    mockLocation.setLongitude(lon);
    mockLocation.setAltitude(0);
    mockLocation.setTime(System.currentTimeMillis());
    lm.setTestProviderLocation(providerName, mockLocation);
  }
  public void shutdown() {
    LocationManager lm = (LocationManager) ctx.getSystemService(
      Context.LOCATION_SERVICE);
    lm.removeTestProvider(providerName);
  }
}
Class used in this way
B4X:
MockLocationProvider mock;
mock = new MockLocationProvider(LocationManager.GPS_PROVIDER, this);
mock.pushLocation(-12.34, 23.45);
 
Top