简体   繁体   中英

Allow mock Locations #Android

I have been trying to use this feature available under the developer options on a android device. i am a tester so trying to fake a location on android device. I have already given permission ACCESS_MOCK_LOCATION under android manifest permissions. Not sure what and where should i put my fake longitude and latitude in my code. does anyone have tried it before? I am new to code and a tester so don't have idea what code should i write for fake long and lat and where should i put it. I have the source code for my test app and its a health app with maps on the home screen.

After instantiating your LocationClient you can call locationclient.setMockMode(true);

After that you can have your code generate Location objects like so

public Location createLocation(double lat, double lng, float accuracy) {
    // Create a new Location
    Location newLocation = new Location(PROVIDER);
    newLocation.setLatitude(lat);
    newLocation.setLongitude(lng);
    newLocation.setAccuracy(accuracy);
    return newLocation;
}

After that you could do something like

Location testLocation = createLocation(12.34, 45.679, 9.0f);

and

locationClient.setMockLocation(testLocation);

(This is taken from here )

This blog describes how to use mock locations when debugging and turning them off when not debugging.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM