setTestProviderLocation() 不会触发 onLocationChanged() 的调用 [英] setTestProviderLocation() does not trigger calling of onLocationChanged()

查看:24
本文介绍了setTestProviderLocation() 不会触发 onLocationChanged() 的调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让这段代码工作:

I'm trying to get this bit of code to work:

在 Android 中测试 GPS

问题是,当我运行测试时,从未调用过 onLocationChanged():

The problem is, when I run the test, onLocationChanged() is never called:

public class LocationTest0 extends AndroidTestCase implements LocationListener {
private Location received = null;
public void testExample() {
    LocationManager lm = (LocationManager)this.getContext().getSystemService(Context.LOCATION_SERVICE);
    String testProvider = "Test";

    if (null == lm.getProvider(testProvider)){
        lm.addTestProvider(testProvider, false, false, false, false, false, false, false, Criteria.POWER_LOW, Criteria.ACCURACY_FINE);
    }

    lm.setTestProviderEnabled(testProvider, true);
    lm.requestLocationUpdates(testProvider, 0, 0, this);
    lm.setTestProviderStatus(testProvider, LocationProvider.AVAILABLE, null, System.currentTimeMillis());

    Location location = new Location(testProvider);
    location.setLatitude(1.0);
    location.setLongitude(2.0);
    location.setTime(System.currentTimeMillis());
    lm.setTestProviderLocation(testProvider, location);

    Assert.assertFalse("Received Location is null", received == null );

    lm.removeTestProvider(testProvider);
}
@Override
public void onLocationChanged(Location location) {
    received = location;
    Log.d("LocationTest0", "onLocationChanged CALLED");
    // Never gets called
}

推荐答案

这对我有用

locationManager.addTestProvider(mocLocationProvider, false, false,
                    false, false, true, true, true, 0, 5);
locationManager.setTestProviderEnabled(mocLocationProvider, true);

.

Location mockLocation = new Location(mocLocationProvider); // a string
mockLocation.setLatitude(location.getLatitude());  // double 
mockLocation.setLongitude(location.getLongitude()); 
mockLocation.setAltitude(location.getAltitude()); 
mockLocation.setTime(System.currentTimeMillis()); 
locationManager.setTestProviderLocation( mocLocationProvider, mockLocation); 

.

<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION">

同样在安卓手机设置中,确保你勾选了允许模拟位置"复选框

Also in the android phone settings make sure you have the "Allow mock locations" checkbox ticked

这篇关于setTestProviderLocation() 不会触发 onLocationChanged() 的调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆