我定位Google API 2.3.3并拥有以下测试类
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.provider.Settings;
import android.test.AndroidTestCase;
import android.util.Log;
public class MockLocationManagerTests extends AndroidTestCase implements LocationListener{
private static final String TAG = "MockLocationTests";
private String locationProvider = "TestProvider";
private LocationManager locationManager;
private Location lastLocation;
protected void setUp() throws Exception {
super.setUp();
locationManager = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
if (Settings.Secure.getInt(getContext().getContentResolver(), Settings.Secure.ALLOW_MOCK_LOCATION, 0) == 0) {
fail("Mock location must be enabled");
}
setupLocationManager();
}
private void setupLocationManager() {
if (locationManager.getProvider(locationProvider) != null) {
locationManager.removeTestProvider(locationProvider);
}
locationManager.addTestProvider(locationProvider,
false,
true,
false,
false,
false,
false,
false,
Criteria.POWER_LOW,
Criteria.ACCURACY_FINE);
locationManager.requestLocationUpdates(locationProvider, 0, 0, this);
locationManager.setTestProviderStatus(locationProvider, LocationProvider.AVAILABLE, null, System.currentTimeMillis());
locationManager.setTestProviderEnabled(locationProvider, true);
}
private Location createLocation(double lat, double lon) {
Location location = new Location(locationProvider);
location.setLatitude(lat);
location.setLongitude(lon);
location.setAltitude(0);
location.setTime(System.currentTimeMillis());
return location;
}
protected void tearDown() throws Exception {
super.tearDown();
}
public final void testCanObserveLocationChanging() throws InterruptedException {
Location l = createLocation(19,-2);
locationManager.setTestProviderLocation(locationProvider,l);
synchronized(this){
wait(5000);
}
Location l2 = createLocation(1,-10);
locationManager.setTestProviderLocation(locationProvider,l2);
synchronized(this){
wait(5000);
}
assertEquals(l,lastLocation);
}
public void onLocationChanged(Location location) {
Log.i(TAG,"location changed");
lastLocation = location;
}
public void onProviderDisabled(String provider) {
Log.i(TAG, provider+" disabled");
}
public void onProviderEnabled(String provider) {
Log.i(TAG, provider+" enabled");
}
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.i(TAG,"status changed: "+provider+" to "+status);
}
}
onLocationChanged没有被调用,我的google-fu和brain-fu让我失望了。任何建议都非常感谢
更新
没有捕获任何日志消息,所以onStatusChanged和onProviderEnabled都没有被触发?
如果我在调用getLastKnownLocation()
之间致电setTestProviderLocation()
,则locationManager会返回我设置的位置
另一个更新
如果我将使用此代码的类添加到我的应用程序中,则调用onLocationChanged()。 唯一的区别是我没有使用Mock_Location。 所以似乎setTestProviderLocation()似乎没有导致onLocationChanged()触发
答案 0 :(得分:1)
在Google API 19上采用相同的方式。
AndroidTestCase没有设置适当的Looper来处理更新事件。您必须将其作为测试活动ActivityInstrumentationTestCase2运行,或者将ThreadLooper添加到AndroidTestCase以处理更新消息。
Looper mLooper;
HandlerThread thread = new HandlerThread("LocationHandlerThread");
thread.start(); // starts the thread.
mLooper = thread.getLooper();
然后在requestLocationUpdates方法中使用新的Looper:
mLocationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 0, 0, mLocationListener, mLooper);
答案 1 :(得分:0)
您是否在手机或模拟器上启用了模拟位置? 在应用程序/开发/模拟位置有一个设置。
答案 2 :(得分:-1)
只需在eclipse的模拟器控制选项卡上单击“发送”即可将坐标发送到应用程序。