You need to keep track of location updates, and when you run the onLocationChanged(Location location) method, you can call location.getSpeed(); This will give you the correct speed if your phone really moves.
But if you test it on a Simulator and send the location to the emulator controller, it will always return 0.
Updated with Example
public class LocationService implements LocationListener { LocationService locationService; LocationManager locationManager; Location lastLocation; private final String TAG = "LocationService" ; private final String MOCK_LOCAION_PROVIDER = "FAKE_PROVIDER"; LocationService(Context ctx) { LocationManager locationManager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE); String mocLocationProvider = MOCK_LOCAION_PROVIDER; if (locationManager.getProvider(mocLocationProvider) != null) { locationManager.removeTestProvider(mocLocationProvider); } locationManager.addTestProvider(mocLocationProvider, false, false, false, false, true, true, true, 0, 5); locationManager.setTestProviderEnabled(mocLocationProvider, true); locationManager.requestLocationUpdates(mocLocationProvider, 0, 0, this); try { List<String> data = new ArrayList<String>(); InputStream is = ctx.getAssets().open("data.txt"); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String line = null; while ((line = reader.readLine()) != null) { data.add(line); }
The above code is actually a location service class, when you make an instance of this class, it registers a fake location service provider (except GPS and network) and enters some fake location parameters from a given file.
Below is the data.txt file, which has latitude,longitude,speed , the above class reads this data.txt file and enters fake lat, lon and speed at the location, as well as the start time for changing the location Thread.sleep() is also implemented.
Data.txt File
24.856449265609735,67.04308920288086,1.64 24.856749265609735,67.04408920288086,7.64 24.856949265609735,67.04508920288086,11.64 24.857649265609735,67.04716920288086,13.64 24.857949265609735,67.04736920288086,12.64 24.857949265609735,67.04742520288086,8.64 24.857949265609735,67.04747020288086,4.64 24.856749265609735,67.04408920288086,6.11 24.856949265609735,67.04508920288086,2.12 24.857249265609735,67.04608920288086,1.1 24.856949265609735,67.04508920288086,2.13 24.857249265609735,67.04608920288086,0.6 24.856949265609735,67.04508920288086,1.19 24.857249265609735,67.04608920288086,1.6 24.856949265609735,67.04508920288086,2.12 24.857249265609735,67.04608920288086,1.15 24.857849265609735,67.04729920288086,17.64 24.857949265609735,67.04736920288086,12.64 24.857949265609735,67.04739920288086,16.64 24.857949265609735,67.04742520288086,8.64 24.857949265609735,67.04747020288086,4.64 24.856749265609735,67.04408920288086,6.11 24.856949265609735,67.04508920288086,2.12 24.857249265609735,67.04608920288086,1.1 24.856949265609735,67.04508920288086,2.13 24.857249265609735,67.04608920288086,0.6 24.856949265609735,67.04508920288086,1.19 24.857249265609735,67.04608920288086,1.6 24.856949265609735,67.04508920288086,2.12 24.857249265609735,67.04608920288086,1.15 24.857849265609735,67.04729920288086,17.64 24.857949265609735,67.04736920288086,12.64 24.857949265609735,67.04739920288086,16.64 24.857949265609735,67.04742520288086,8.64 24.857949265609735,67.04747020288086,4.64 24.856749265609735,67.04408920288086,6.11 24.856949265609735,67.04508920288086,2.12 24.857249265609735,67.04608920288086,1.1 24.857849265609735,67.04729920288086,17.64 24.857949265609735,67.04736920288086,12.64 24.857949265609735,67.04739920288086,16.64 24.857949265609735,67.04742520288086,8.64 24.857949265609735,67.04747020288086,4.64 24.856749265609735,67.04408920288086,6.11 24.856949265609735,67.04508920288086,2.12 24.857249265609735,67.04608920288086,1.15 24.856949265609735,67.04508920288086,2.13 24.857249265609735,67.04608920288086,0.6 24.856949265609735,67.04508920288086,1.19 24.857249265609735,67.04608920288086,1.6 24.856949265609735,67.04508920288086,2.12 24.857249265609735,67.04608920288086,1.15 24.856949265609735,67.04508920288086,2.13 24.857249265609735,67.04608920288086,0.6 24.856949265609735,67.04508920288086,1.19 24.857249265609735,67.04608920288086,1.6 24.856949265609735,67.04508920288086,2.12 24.857249265609735,67.04608920288086,1.15