I tried to find out the speed of User, while user is moving with device. Here, I followed one link with sample code. i.e: Here SpeedDemo.
The problem is, by using location.getSpeed()
method, we are finding the speed. So, for that I changed the location values in device, but every time the value of the location.getspeed()
return '0' only. Why it happen, even changing the location itself.
Can any one known about this?
You have to track location Updates, and when the method onLocationChanged(Location location)
triggers, you can call location.getSpeed();
it will give you correct speed, if your phone is actually moving.
But if you are testing it on Simulator, and sending location by 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 data = new ArrayList(); 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); } // Log.e(TAG, data.size() + " lines"); new MockLocationProvider(locationManager, mocLocationProvider, data).start(); } catch (IOException e) { e.printStackTrace(); } } class MockLocationProvider extends Thread { private List data; private LocationManager locationManager; private String mocLocationProvider; private String LOG_TAG = "MockLocationProvider"; public MockLocationProvider(LocationManager locationManager, String mocLocationProvider, List data) throws IOException { this.locationManager = locationManager; this.mocLocationProvider = mocLocationProvider; this.data = data; } @Override public void run() { for (String str : data) { try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } // Set one position String[] parts = str.split(","); Double latitude = Double.valueOf(parts[0]); Double longitude = Double.valueOf(parts[1]); float speed = Float.valueOf(parts[2]); Location location = new Location(mocLocationProvider); location.setLatitude(latitude); location.setLongitude(longitude); location.setSpeed(speed); // location.setAltitude(altitude); // Log.d(LOG_TAG, location.toString()); // set the time in the location. If the time on this location // matches the time on the one in the previous set call, it will // be // ignored location.setTime(System.currentTimeMillis()); locationManager.setTestProviderLocation(mocLocationProvider, location); } } } public void onLocationChanged(Location location) { // TODO Auto-generated method stub Log.e(TAG, "onLocationChanged"); // Get Location Speed Here Log.d(TAG, "Speed " +location.getSpeed()); } public void onProviderDisabled(String provider) { // TODO Auto-generated method stub // Log.e(TAG, "onProviderDisabled : "+provider); } public void onProviderEnabled(String provider) { // TODO Auto-generated method stub // Log.e(TAG, "onProviderEnabled : "+provider); } public void onStatusChanged(String provider, int status, Bundle arg2) { // TODO Auto-generated method stub // Log.e(TAG, "onStatusChanged : "+status); } }
The Above code is actually a location service class, when you make an instance of this class, it register a fake Location Service(other than GPS, and Network) provider, and input some fake location parameters by a given file.
Below is the data.txt file which has latitude,longitude,speed
the above class read this data.txt file and input fake lat,lon, and speed in the location, as well as trigger time to change location is also implemented by Thread.sleep()
call.
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
First we need to check whether the Gps Enabled or not using location manager.
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
Then , you need to implement GpsStatus.Listener to get the Gps updates. It will return the Gps location updates.
GPSManager.locationListener = new LocationListener() { @Override public void onLocationChanged(final Location location) { if (GPSManager.gpsCallback != null) { GPSManager.gpsCallback.onGPSUpdate(location); } } @Override public void onProviderDisabled(final String provider) { } @Override public void onProviderEnabled(final String provider) { } @Override public void onStatusChanged(final String provider, final int status, final Bundle extras) { } };
From that location you can able to get the current speed.
speed = location.getSpeed();
Refer to my website here for further details : http://velmm.com/get-current-user-speed-using-gps-android/