您需要跟踪位置更新,当方法onLocationChanged(Location location)
触发时,您可以调用location.getSpeed();
它将给您正确的速度,如果您的手机实际上正在移动。
但是,如果您在模拟器上进行测试,并通过模拟器控制器发送位置,则始终会返回0。
示例更新
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);
}
new MockLocationProvider(locationManager, mocLocationProvider, data).start();
} catch (IOException e) {
e.printStackTrace();
}
}
class MockLocationProvider extends Thread {
private List<String> data;
private LocationManager locationManager;
private String mocLocationProvider;
private String LOG_TAG = "MockLocationProvider";
public MockLocationProvider(LocationManager locationManager, String mocLocationProvider, List<String> 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();
}
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.setTime(System.currentTimeMillis());
locationManager.setTestProviderLocation(mocLocationProvider, location);
}
}
}
public void onLocationChanged(Location location) {
Log.e(TAG, "onLocationChanged");
Log.d(TAG, "Speed " +location.getSpeed());
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle arg2) {
}
}
上述代码实际上是一个定位服务类,当您创建此类的实例时,它会注册一个虚假的位置服务(除了GPS和网络),并通过给定的文件输入一些虚假的位置参数。
以下是data.txt文件,其中包含 纬度、经度、速度
,上述类读取此data.txt文件并在位置中输入虚假的纬度、经度和速度,同时通过调用 Thread.sleep()
来实现触发时间以更改位置。
data.txt 文件内容
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